Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Cannot Run Code To Use Webcam capture and analysis

Hello everyone, Im using Visual Studio environment with OpenCV to run an analaysis on mosquito behaviours suing openCV for my final year thesis project.

I tried running the code files but a few errors showed up. could you guide me where i might be going wrong? thanks.

below are the errors.

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 122

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 123

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 124

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 126

Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source? projet file c:\users\owner\source\repos\projet file\projet file\projet file.cpp 285

======================================================================

These are the codes for your reference. I think its a small issue i need to fix but im unsure because im really new to this environment. hope you guys could guide me. thanks alot .

  #include <opencv2\highgui.hpp>
    #include <opencv2\videoio.hpp>
    #include <opencv2\imgcodecs.hpp>
    #include "opencv2/imgproc/imgproc.hpp"
    #include <opencv2/opencv.hpp>
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>

#include <math.h>
#include <ctime>
#include <opencv2\core\ocl.hpp>
#include <fstream>
#include <direct.h>
#include <shlobj.h>
#include <stdarg.h>
#include <ctype.h>


//ADD this to disable OpenCL explicitly
using namespace cv;
using namespace std;
void Detector(Mat Img, int x_, int y_)
{
    circle(Img, Point(x_, y_), 2, Scalar(0, 255, 0), 2, 8, 0);
}
void AvgDetector(Mat Img, int x_, int y_)
{
    circle(Img, Point(x_, y_), 2, Scalar(0, 0, 255), 2, 8, 0);
}
void Alert(Mat Img){
    putText(Img, "Detected", Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);
}
const string currentDateTime() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

    return buf;
}
void GetDesktopResolution(int& horizontal, int& vertical)
{
    RECT desktop;
    // Get a handle to the desktop window
    const HWND hDesktop = GetDesktopWindow();
    // Get the size of screen to the variable desktop
    GetWindowRect(hDesktop, &desktop);
    // The top left corner will have coordinates (0,0)
    // and the bottom right corner will have coordinates
    // (horizontal, vertical)
    horizontal = desktop.right;
    vertical = desktop.bottom;
}
string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileNameA(NULL, buffer, MAX_PATH);
    string::size_type pos = string(buffer).find_last_of("\\/");
    return string(buffer).substr(0, pos);
}


int main()
{
    string Location, Avg_Position, Datapath, Maxfreq, Version;
    ofstream myfile;
    stringstream DataDir, datafilename, Position;
    bool CreateDir = true;

    int StringElement = 0;
    string s = ExePath();
    std::string delimiter = "\\";
    size_t pos = 0;
    std::string token;
    while ((pos = s.find(delimiter)) != std::string::npos) {
        StringElement++;
        token = s.substr(0, pos);
        if (StringElement == 7){
            Version = token;
            break;
        }
        s.erase(0, pos + delimiter.length());
    }

    cout << CV_VERSION << "\n";
    cout << Version << "\n\n";
    cout << "Please key-in the location of your testing site: \n";
    cout << "Location:\t";
    getline(cin, Location);
    cout << "\nPlease key-in the path for the data files to be saved: \n";
    cout << "Path:\t";
    getline(cin, Datapath);
    cout << "\nPlease select start once the system is ready. \n\n";


    // Disable OpenCL explicitly here
    ocl::setUseOpenCL(false);
    VideoCapture capture(2);
    Mat current_frame, Gray_frame, Black_White, Black, Test;



    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Trackbar region   /////////////////////////////////////////////////////////////////////////////////////////////////////
    //// For cropper
    int a = 0, b = 0, c = 0, d = 0;
    int blobcounter = 0;
    int x_counter = 0;
    int y_counter = 0;

    int buffer, counter = 0;
    int threshold_value = 0;
    int threshold_type = 3;
    int min_freq = 0;
    int state = 0;
    int const max_value = 255;
    int const max_type = 4;
    int const max_BINARY_value = 255;
    char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
    char* trackbar_value = "Value";
    char* freq_value = "Frequency";

    char* Activator = "Start";
    void Threshold_Demo(int, void*);

    //For Finding Blobs
    ////////////////////////////////////////////////////////////
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    //////////////////////////////////////////////////////////////////////////////////////
    // To set the positon and initialize window parameters ///////////////////////////////
    int horizontal = 0;
    int vertical = 0;
    GetDesktopResolution(horizontal, vertical);
    HWND console = GetConsoleWindow();

    //////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////


    while (1) {

#pragma region
        capture >> current_frame;
        if (current_frame.empty()) break;

        createTrackbar("Start", "Black & White", &state, 1);
        createTrackbar(freq_value, "Black & White", &min_freq, 1000);
        createTrackbar(trackbar_type, "Black & White", &threshold_type, max_type);
        createTrackbar(trackbar_value, "Black & White", &threshold_value, max_value);

        ////// For cropper
        createTrackbar("x-Origin", "Normal", &a, 640);
        createTrackbar("y-Origin", "Normal", &b, 480);
        createTrackbar("Width", "Normal", &c, 640);
        createTrackbar("Height", "Normal", &d, 480);

        // Convert color to Grayscale
        cvtColor(current_frame, Gray_frame, CV_RGB2GRAY);
        threshold(Gray_frame, Black_White, threshold_value, max_value, threshold_type);
        Mat Black_Green(current_frame.size(), CV_8UC3, Scalar(0, 0, 0));
        Mat Black(current_frame.size(), CV_8UC3, Scalar(0, 0, 0));

        Black_Green.setTo(Scalar(0, 255, 0), Black_White);
        findContours(Black_White, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); //find all blobs

#pragma endregion

        blobcounter = 0, x_counter = 0, y_counter = 0;
        for (int i = 0; i < contours.size(); i++){
            vector<Point> contour_poly;
            approxPolyDP(Mat(contours[i]), contour_poly, 3, true);

            //Simplifying the whole "contours[i].at(0)" thing
            int x = contours[i].at(0).x, y = contours[i].at(0).y;
            //if (contourArea(contour_poly) > 20 && contourArea(contour_poly) < 30){
            //if (20 < contourArea(contour_poly) < 50){
            if (x > a && x < c && y > b && y < d){
                Detector(Black, x, y);

                circle(current_frame, Point(x, y), 3, Scalar(0, 255, 0), 1, 8, 0);

                blobcounter++;
                x_counter = x_counter + x;
                y_counter = y_counter + y;

            }


        }

        rectangle(current_frame, Point(a, b), Point(a + c, b + d), Scalar(0, 0, 255), 2, 8, 0);
        rectangle(Black, Point(a, b), Point(a + c, b + d), Scalar(0, 0, 255), 2, 8, 0);


        if (blobcounter == 0){ putText(Black, "No mosquito detected", Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8); }
        if (blobcounter > 0){
            Avg_Position = to_string(x_counter / blobcounter) + "," + to_string(y_counter / blobcounter);
            AvgDetector(Black, x_counter / blobcounter, y_counter / blobcounter);
            putText(Black, Avg_Position, Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);
        }


        if (state == 1){

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Create folder path for saving data
            ////////////////////////////////////////////////////////////////////////////////////////////////
            if (CreateDir == true){
                _mkdir((Datapath + "/Output Files").c_str());
                datafilename << Datapath + "/Output Files/" << ctime << " - Output.txt";
                myfile.open(datafilename.str());
                myfile << "Version \t" << Version << "\n";
                myfile << "Location \t" << Location << "\n";
                myfile << "x-coordinate Boundary \t" << a << "\n";
                myfile << "y-coordinate Boundary \t" << b << "\n";
                myfile << "Threshold Value \t" << threshold_value << "\n";
                myfile << "Maximum Setup Frequency \t" << Maxfreq << "\n"; // Max frequency
                myfile << "Chamber width\n";
                myfile << "Chamber height\n";
                myfile << "Timestamp" << "\t" << "Unit Frequency" << "\t" << "x Avg." << "\t" << "y Avg." << "\n\n";
                CreateDir = false;
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////////////////////////////////////////

            //Loop to make sure the display of the data is timed
            if (counter == 20){

                blobcounter = 0, x_counter = 0, y_counter = 0;
                for (int i = 0; i < contours.size(); i++){
                    vector<Point> contour_poly;
                    approxPolyDP(Mat(contours[i]), contour_poly, 3, true);
                    //Simplifying the whole "contours[i].at(0)" thing
                    int x = contours[i].at(0).x, y = contours[i].at(0).y;
                    if (x > a && x < c && y > b && y < d){

                        x_counter = x_counter + x;
                        y_counter = y_counter + y;
                        Position << "M" << blobcounter << ": " << x << "," << y << "\t";
                        blobcounter++;
                    }
                }

                string Rowdata;
                if (blobcounter>0){ Rowdata = currentDateTime() + "\t" + to_string(min_freq) + "\t" + to_string(x_counter / blobcounter) + "\t" + to_string(y_counter / blobcounter) + "\t" + Position.str() + "\n"; }
                if (blobcounter == 0){ Rowdata = currentDateTime() + "\t" + to_string(min_freq) + "\t" + "n/a" + "\t" + "n/a" + "\n"; }

                cout << Rowdata + "\n";
                myfile << Rowdata;
                counter = 0;
                Position.str("");
            }
            counter++;
        }

#pragma region
        resize(current_frame, current_frame, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);
        resize(Black, Black, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);
        resize(Black_White, Black_White, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);

        cvMoveWindow("Normal", 0, 0);
        cvMoveWindow("Black & White", horizontal * 0.31, 0);
        cvMoveWindow("Detection Map", horizontal * 0.62, vertical * 0.216);
        MoveWindow(console, horizontal * 0.62, 0, horizontal * 0.308, vertical * 0.21, TRUE);

        imshow("Normal", current_frame);
        imshow("Black & White", Black_White);
        imshow("Detection Map", Black);
        int key = waitKey(33) && 0xFF;
        if (key == 27) break;
#pragma endregion



    }

    return 0;

}

Cannot Run Code To Use Webcam capture and analysis

Hello everyone, Im using Visual Studio environment with OpenCV to run an analaysis on mosquito behaviours suing openCV for my final year thesis project.

I tried running the code files but a few errors showed up. could you guide me where i might be going wrong? thanks.

below are the errors.

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 122

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 123

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 124

Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" projet file C:\Users\Owner\source\repos\projet file\projet file\projet file.cpp 126 error.

Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source? projet file c:\users\owner\source\repos\projet file\projet file\projet file.cpp 285

======================================================================

These are the codes for your reference. I think its a small issue i need to fix but im unsure because im really new to this environment. hope you guys could guide me. thanks alot .

  #include <opencv2\highgui.hpp>
    #include <opencv2\videoio.hpp>
    #include <opencv2\imgcodecs.hpp>
    #include "opencv2/imgproc/imgproc.hpp"
    #include <opencv2/opencv.hpp>
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>

#include <math.h>
#include <ctime>
#include <opencv2\core\ocl.hpp>
#include <fstream>
#include <direct.h>
#include <shlobj.h>
#include <stdarg.h>
#include <ctype.h>


//ADD this to disable OpenCL explicitly
using namespace cv;
using namespace std;
void Detector(Mat Img, int x_, int y_)
{
    circle(Img, Point(x_, y_), 2, Scalar(0, 255, 0), 2, 8, 0);
}
void AvgDetector(Mat Img, int x_, int y_)
{
    circle(Img, Point(x_, y_), 2, Scalar(0, 0, 255), 2, 8, 0);
}
void Alert(Mat Img){
    putText(Img, "Detected", Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);
}
const string currentDateTime() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

    return buf;
}
void GetDesktopResolution(int& horizontal, int& vertical)
{
    RECT desktop;
    // Get a handle to the desktop window
    const HWND hDesktop = GetDesktopWindow();
    // Get the size of screen to the variable desktop
    GetWindowRect(hDesktop, &desktop);
    // The top left corner will have coordinates (0,0)
    // and the bottom right corner will have coordinates
    // (horizontal, vertical)
    horizontal = desktop.right;
    vertical = desktop.bottom;
}
string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileNameA(NULL, buffer, MAX_PATH);
    string::size_type pos = string(buffer).find_last_of("\\/");
    return string(buffer).substr(0, pos);
}


int main()
{
    string Location, Avg_Position, Datapath, Maxfreq, Version;
    ofstream myfile;
    stringstream DataDir, datafilename, Position;
    bool CreateDir = true;

    int StringElement = 0;
    string s = ExePath();
    std::string delimiter = "\\";
    size_t pos = 0;
    std::string token;
    while ((pos = s.find(delimiter)) != std::string::npos) {
        StringElement++;
        token = s.substr(0, pos);
        if (StringElement == 7){
            Version = token;
            break;
        }
        s.erase(0, pos + delimiter.length());
    }

    cout << CV_VERSION << "\n";
    cout << Version << "\n\n";
    cout << "Please key-in the location of your testing site: \n";
    cout << "Location:\t";
    getline(cin, Location);
    cout << "\nPlease key-in the path for the data files to be saved: \n";
    cout << "Path:\t";
    getline(cin, Datapath);
    cout << "\nPlease select start once the system is ready. \n\n";


    // Disable OpenCL explicitly here
    ocl::setUseOpenCL(false);
    VideoCapture capture(2);
    Mat current_frame, Gray_frame, Black_White, Black, Test;



    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Trackbar region   /////////////////////////////////////////////////////////////////////////////////////////////////////
    //// For cropper
    int a = 0, b = 0, c = 0, d = 0;
    int blobcounter = 0;
    int x_counter = 0;
    int y_counter = 0;

    int buffer, counter = 0;
    int threshold_value = 0;
    int threshold_type = 3;
    int min_freq = 0;
    int state = 0;
    int const max_value = 255;
    int const max_type = 4;
    int const max_BINARY_value = 255;
    const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
    const char* trackbar_value = "Value";
    const char* freq_value = "Frequency";

    const char* Activator = "Start";
    void Threshold_Demo(int, void*);

    //For Finding Blobs
    ////////////////////////////////////////////////////////////
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    //////////////////////////////////////////////////////////////////////////////////////
    // To set the positon and initialize window parameters ///////////////////////////////
    int horizontal = 0;
    int vertical = 0;
    GetDesktopResolution(horizontal, vertical);
    HWND console = GetConsoleWindow();

    //////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////


    while (1) {

#pragma region
        capture >> current_frame;
        if (current_frame.empty()) break;

        createTrackbar("Start", "Black & White", &state, 1);
        createTrackbar(freq_value, "Black & White", &min_freq, 1000);
        createTrackbar(trackbar_type, "Black & White", &threshold_type, max_type);
        createTrackbar(trackbar_value, "Black & White", &threshold_value, max_value);

        ////// For cropper
        createTrackbar("x-Origin", "Normal", &a, 640);
        createTrackbar("y-Origin", "Normal", &b, 480);
        createTrackbar("Width", "Normal", &c, 640);
        createTrackbar("Height", "Normal", &d, 480);

        // Convert color to Grayscale
        cvtColor(current_frame, Gray_frame, CV_RGB2GRAY);
        threshold(Gray_frame, Black_White, threshold_value, max_value, threshold_type);
        Mat Black_Green(current_frame.size(), CV_8UC3, Scalar(0, 0, 0));
        Mat Black(current_frame.size(), CV_8UC3, Scalar(0, 0, 0));

        Black_Green.setTo(Scalar(0, 255, 0), Black_White);
        findContours(Black_White, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); //find all blobs

#pragma endregion

        blobcounter = 0, x_counter = 0, y_counter = 0;
        for (int i = 0; i < contours.size(); i++){
            vector<Point> contour_poly;
            approxPolyDP(Mat(contours[i]), contour_poly, 3, true);

            //Simplifying the whole "contours[i].at(0)" thing
            int x = contours[i].at(0).x, y = contours[i].at(0).y;
            //if (contourArea(contour_poly) > 20 && contourArea(contour_poly) < 30){
            //if (20 < contourArea(contour_poly) < 50){
            if (x > a && x < c && y > b && y < d){
                Detector(Black, x, y);

                circle(current_frame, Point(x, y), 3, Scalar(0, 255, 0), 1, 8, 0);

                blobcounter++;
                x_counter = x_counter + x;
                y_counter = y_counter + y;

            }


        }

        rectangle(current_frame, Point(a, b), Point(a + c, b + d), Scalar(0, 0, 255), 2, 8, 0);
        rectangle(Black, Point(a, b), Point(a + c, b + d), Scalar(0, 0, 255), 2, 8, 0);


        if (blobcounter == 0){ putText(Black, "No mosquito detected", Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8); }
        if (blobcounter > 0){
            Avg_Position = to_string(x_counter / blobcounter) + "," + to_string(y_counter / blobcounter);
            AvgDetector(Black, x_counter / blobcounter, y_counter / blobcounter);
            putText(Black, Avg_Position, Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);
        }


        if (state == 1){

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Create folder path for saving data
            ////////////////////////////////////////////////////////////////////////////////////////////////
            if (CreateDir == true){
                _mkdir((Datapath + "/Output Files").c_str());
                datafilename << Datapath + "/Output Files/" << ctime << " - Output.txt";
                myfile.open(datafilename.str());
                myfile << "Version \t" << Version << "\n";
                myfile << "Location \t" << Location << "\n";
                myfile << "x-coordinate Boundary \t" << a << "\n";
                myfile << "y-coordinate Boundary \t" << b << "\n";
                myfile << "Threshold Value \t" << threshold_value << "\n";
                myfile << "Maximum Setup Frequency \t" << Maxfreq << "\n"; // Max frequency
                myfile << "Chamber width\n";
                myfile << "Chamber height\n";
                myfile << "Timestamp" << "\t" << "Unit Frequency" << "\t" << "x Avg." << "\t" << "y Avg." << "\n\n";
                CreateDir = false;
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////////////////////////////////////////

            //Loop to make sure the display of the data is timed
            if (counter == 20){

                blobcounter = 0, x_counter = 0, y_counter = 0;
                for (int i = 0; i < contours.size(); i++){
                    vector<Point> contour_poly;
                    approxPolyDP(Mat(contours[i]), contour_poly, 3, true);
                    //Simplifying the whole "contours[i].at(0)" thing
                    int x = contours[i].at(0).x, y = contours[i].at(0).y;
                    if (x > a && x < c && y > b && y < d){

                        x_counter = x_counter + x;
                        y_counter = y_counter + y;
                        Position << "M" << blobcounter << ": " << x << "," << y << "\t";
                        blobcounter++;
                    }
                }

                string Rowdata;
                if (blobcounter>0){ Rowdata = currentDateTime() + "\t" + to_string(min_freq) + "\t" + to_string(x_counter / blobcounter) + "\t" + to_string(y_counter / blobcounter) + "\t" + Position.str() + "\n"; }
                if (blobcounter == 0){ Rowdata = currentDateTime() + "\t" + to_string(min_freq) + "\t" + "n/a" + "\t" + "n/a" + "\n"; }

                cout << Rowdata + "\n";
                myfile << Rowdata;
                counter = 0;
                Position.str("");
            }
            counter++;
        }

#pragma region
        resize(current_frame, current_frame, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);
        resize(Black, Black, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);
        resize(Black_White, Black_White, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);

        cvMoveWindow("Normal", 0, 0);
        cvMoveWindow("Black & White", horizontal * 0.31, 0);
        cvMoveWindow("Detection Map", horizontal * 0.62, vertical * 0.216);
        MoveWindow(console, horizontal * 0.62, 0, horizontal * 0.308, vertical * 0.21, TRUE);

        imshow("Normal", current_frame);
        imshow("Black & White", Black_White);
        imshow("Detection Map", Black);
        int key = waitKey(33) && 0xFF;
        if (key == 27) break;
#pragma endregion



    }

    return 0;

}

Cannot Run Code To Use Webcam capture and analysis

Hello everyone, Im using Visual Studio environment with OpenCV to run an analaysis on mosquito behaviours suing openCV for my final year thesis project.

I tried running the code files but a few errors showed up. could you guide me where i might be going wrong? thanks.

below are the error.

Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source? projet file c:\users\owner\source\repos\projet file\projet file\projet file.cpp 285

======================================================================

These are the codes for your reference. I think its a small issue i need to fix but im unsure because im really new to this environment. hope you guys could guide me. thanks alot .

  #include <opencv2\highgui.hpp>
    #include <opencv2\videoio.hpp>
    #include <opencv2\imgcodecs.hpp>
    #include "opencv2/imgproc/imgproc.hpp"
    #include <opencv2/opencv.hpp>
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>

#include <math.h>
#include <ctime>
#include <opencv2\core\ocl.hpp>
#include <fstream>
#include <direct.h>
#include <shlobj.h>
#include <stdarg.h>
#include <ctype.h>


//ADD this to disable OpenCL explicitly
using namespace cv;
using namespace std;
void Detector(Mat Img, int x_, int y_)
{
    circle(Img, Point(x_, y_), 2, Scalar(0, 255, 0), 2, 8, 0);
}
void AvgDetector(Mat Img, int x_, int y_)
{
    circle(Img, Point(x_, y_), 2, Scalar(0, 0, 255), 2, 8, 0);
}
void Alert(Mat Img){
    putText(Img, "Detected", Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);
}
const string currentDateTime() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

    return buf;
}
void GetDesktopResolution(int& horizontal, int& vertical)
{
    RECT desktop;
    // Get a handle to the desktop window
    const HWND hDesktop = GetDesktopWindow();
    // Get the size of screen to the variable desktop
    GetWindowRect(hDesktop, &desktop);
    // The top left corner will have coordinates (0,0)
    // and the bottom right corner will have coordinates
    // (horizontal, vertical)
    horizontal = desktop.right;
    vertical = desktop.bottom;
}
string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileNameA(NULL, buffer, MAX_PATH);
    string::size_type pos = string(buffer).find_last_of("\\/");
    return string(buffer).substr(0, pos);
}


int main()
{
    string Location, Avg_Position, Datapath, Maxfreq, Version;
    ofstream myfile;
    stringstream DataDir, datafilename, Position;
    bool CreateDir = true;

    int StringElement = 0;
    string s = ExePath();
    std::string delimiter = "\\";
    size_t pos = 0;
    std::string token;
    while ((pos = s.find(delimiter)) != std::string::npos) {
        StringElement++;
        token = s.substr(0, pos);
        if (StringElement == 7){
            Version = token;
            break;
        }
        s.erase(0, pos + delimiter.length());
    }

    cout << CV_VERSION << "\n";
    cout << Version << "\n\n";
    cout << "Please key-in the location of your testing site: \n";
    cout << "Location:\t";
    getline(cin, Location);
    cout << "\nPlease key-in the path for the data files to be saved: \n";
    cout << "Path:\t";
    getline(cin, Datapath);
    cout << "\nPlease select start once the system is ready. \n\n";


    // Disable OpenCL explicitly here
    ocl::setUseOpenCL(false);
    VideoCapture capture(2);
    Mat current_frame, Gray_frame, Black_White, Black, Test;



    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Trackbar region   /////////////////////////////////////////////////////////////////////////////////////////////////////
    //// For cropper
    int a = 0, b = 0, c = 0, d = 0;
    int blobcounter = 0;
    int x_counter = 0;
    int y_counter = 0;

    int buffer, counter = 0;
    int threshold_value = 0;
    int threshold_type = 3;
    int min_freq = 0;
    int state = 0;
    int const max_value = 255;
    int const max_type = 4;
    int const max_BINARY_value = 255;
    const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
    const char* trackbar_value = "Value";
    const char* freq_value = "Frequency";

    const char* Activator = "Start";
    void Threshold_Demo(int, void*);

    //For Finding Blobs
    ////////////////////////////////////////////////////////////
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    //////////////////////////////////////////////////////////////////////////////////////
    // To set the positon and initialize window parameters ///////////////////////////////
    int horizontal = 0;
    int vertical = 0;
    GetDesktopResolution(horizontal, vertical);
    HWND console = GetConsoleWindow();

    //////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////


    while (1) {

#pragma region
        capture >> current_frame;
        if (current_frame.empty()) break;

        createTrackbar("Start", "Black & White", &state, 1);
        createTrackbar(freq_value, "Black & White", &min_freq, 1000);
        createTrackbar(trackbar_type, "Black & White", &threshold_type, max_type);
        createTrackbar(trackbar_value, "Black & White", &threshold_value, max_value);

        ////// For cropper
        createTrackbar("x-Origin", "Normal", &a, 640);
        createTrackbar("y-Origin", "Normal", &b, 480);
        createTrackbar("Width", "Normal", &c, 640);
        createTrackbar("Height", "Normal", &d, 480);

        // Convert color to Grayscale
        cvtColor(current_frame, Gray_frame, CV_RGB2GRAY);
        threshold(Gray_frame, Black_White, threshold_value, max_value, threshold_type);
        Mat Black_Green(current_frame.size(), CV_8UC3, Scalar(0, 0, 0));
        Mat Black(current_frame.size(), CV_8UC3, Scalar(0, 0, 0));

        Black_Green.setTo(Scalar(0, 255, 0), Black_White);
        findContours(Black_White, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); //find all blobs

#pragma endregion

        blobcounter = 0, x_counter = 0, y_counter = 0;
        for (int i = 0; i < contours.size(); i++){
            vector<Point> contour_poly;
            approxPolyDP(Mat(contours[i]), contour_poly, 3, true);

            //Simplifying the whole "contours[i].at(0)" thing
            int x = contours[i].at(0).x, y = contours[i].at(0).y;
            //if (contourArea(contour_poly) > 20 && contourArea(contour_poly) < 30){
            //if (20 < contourArea(contour_poly) < 50){
            if (x > a && x < c && y > b && y < d){
                Detector(Black, x, y);

                circle(current_frame, Point(x, y), 3, Scalar(0, 255, 0), 1, 8, 0);

                blobcounter++;
                x_counter = x_counter + x;
                y_counter = y_counter + y;

            }


        }

        rectangle(current_frame, Point(a, b), Point(a + c, b + d), Scalar(0, 0, 255), 2, 8, 0);
        rectangle(Black, Point(a, b), Point(a + c, b + d), Scalar(0, 0, 255), 2, 8, 0);


        if (blobcounter == 0){ putText(Black, "No mosquito detected", Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8); }
        if (blobcounter > 0){
            Avg_Position = to_string(x_counter / blobcounter) + "," + to_string(y_counter / blobcounter);
            AvgDetector(Black, x_counter / blobcounter, y_counter / blobcounter);
            putText(Black, Avg_Position, Point(20, 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);
        }


        if (state == 1){

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Create folder path for saving data
            ////////////////////////////////////////////////////////////////////////////////////////////////
            if (CreateDir == true){
                _mkdir((Datapath + "/Output Files").c_str());
                datafilename << Datapath + "/Output Files/" << ctime << " - Output.txt";
                myfile.open(datafilename.str());
                myfile << "Version \t" << Version << "\n";
                myfile << "Location \t" << Location << "\n";
                myfile << "x-coordinate Boundary \t" << a << "\n";
                myfile << "y-coordinate Boundary \t" << b << "\n";
                myfile << "Threshold Value \t" << threshold_value << "\n";
                myfile << "Maximum Setup Frequency \t" << Maxfreq << "\n"; // Max frequency
                myfile << "Chamber width\n";
                myfile << "Chamber height\n";
                myfile << "Timestamp" << "\t" << "Unit Frequency" << "\t" << "x Avg." << "\t" << "y Avg." << "\n\n";
                CreateDir = false;
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////
            ////////////////////////////////////////////////////////////////////////////////////////////////

            //Loop to make sure the display of the data is timed
            if (counter == 20){

                blobcounter = 0, x_counter = 0, y_counter = 0;
                for (int i = 0; i < contours.size(); i++){
                    vector<Point> contour_poly;
                    approxPolyDP(Mat(contours[i]), contour_poly, 3, true);
                    //Simplifying the whole "contours[i].at(0)" thing
                    int x = contours[i].at(0).x, y = contours[i].at(0).y;
                    if (x > a && x < c && y > b && y < d){

                        x_counter = x_counter + x;
                        y_counter = y_counter + y;
                        Position << "M" << blobcounter << ": " << x << "," << y << "\t";
                        blobcounter++;
                    }
                }

                string Rowdata;
                if (blobcounter>0){ Rowdata = currentDateTime() + "\t" + to_string(min_freq) + "\t" + to_string(x_counter / blobcounter) + "\t" + to_string(y_counter / blobcounter) + "\t" + Position.str() + "\n"; }
                if (blobcounter == 0){ Rowdata = currentDateTime() + "\t" + to_string(min_freq) + "\t" + "n/a" + "\t" + "n/a" + "\n"; }

                cout << Rowdata + "\n";
                myfile << Rowdata;
                counter = 0;
                Position.str("");
            }
            counter++;
        }

#pragma region
        resize(current_frame, current_frame, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);
        resize(Black, Black, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);
        resize(Black_White, Black_White, Size(horizontal * 0.3, vertical * 0.4), 0, 0, INTER_CUBIC);

        cvMoveWindow("Normal", 0, 0);
        cvMoveWindow("Black & White", horizontal * 0.31, 0);
        cvMoveWindow("Detection Map", horizontal * 0.62, vertical * 0.216);
        MoveWindow(console, horizontal * 0.62, 0, horizontal * 0.308, vertical * 0.21, TRUE);

        imshow("Normal", current_frame);
        imshow("Black & White", Black_White);
        imshow("Detection Map", Black);
        int key = waitKey(33) && 0xFF;
        if (key == 27) break;
#pragma endregion



    }

    return 0;

}