Problem in Colored Object Tracking

IDE: CodeBLocks
Opencv Version:2.4.6
Language: C++

Following is the code for yellow color detection, which i was able to successfully perform.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	#include "opencv2/imgproc/imgproc.hpp"
	#include "opencv2/highgui/highgui.hpp"
	#include <iostream>

	using namespace cv;
	using namespace std;

	
	Mat GetThresholdedImage(Mat image_here)
	{
	Mat image_here1=image_here;
	cvtColor(image_here,image_here1,CV_BGR2HSV);
	inRange(image_here1, Scalar(20, 100, 100), Scalar(30, 255, 255), image_here1);
	return image_here1;
	}



	int main(int argc, char* argv[])
	{
		VideoCapture cap(0); // open the video camera no. 0

		if (!cap.isOpened())  // if not success, exit program
		{
			cout << "Cannot open the video cam" << endl;
			return -1;
		}

	   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //width of frames of ideo
	double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //height of frames of the video
		Mat imgtrack(dWidth,dHeight,CV_8UC3);
		imgtrack.setTo(0);

		cout << "Frame size : " << dWidth << " x " << dHeight << endl;

	namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

		while (1)
		{
		Mat frame;

		bool bSuccess = cap.read(frame); // read a new frame from video
		//imshow("MyVideo", frame);
		if (!bSuccess) //if not success, break loop
		{
			cout << "Cannot read a frame from video stream" << endl;
			break;
		}


		 Mat imgYellowThresh=GetThresholdedImage(frame);




		imshow("MyVideo", imgYellowThresh);



	   cout<<endl<<"moving to imgproc";
		cout<<"end";
		if (waitKey(30) == 27)
		{
		cout << "esc key is pressed by user" << endl;
		break;
		}
	}

	return 0;

	}


Following is my code for performing **tracking** of a yellow coloured object. The problem is that it compiles and runs successfully however the output window in which the final video output should be shown is not displayed and the program ends abruptly without any errors or exceptions etc.
I have already looked at quite a few other codes and pages, but could not find a solution.

I used the opencv tutorial to build my code. Since the code mentioned here is C style so i made some of my own changes to build a c++ code.
http://opencv-srf.blogspot.in/2010/09/object-detection-using-color-seperation.html

I also added a bunch of cout's like "starting thresholding" etc to check the problem. The statements upto the one `cout<<"\n area if starting \n";` are being printed but the rest are not.
Moreover i am getting `warning: unknown escape sequence: '\040'` at the line with the statement `cout"\n about to add\n";`.
Since i am new to both image processing and opencv, i cant figure out how to work this.

Please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include <iostream>

    using namespace cv;
    using namespace std;

    Mat imgtrack;
    int lastX=-1;
    int lastY=-1;

    Mat GetThresholdedImage(Mat image_here)
    {
    Mat image_here1=image_here;
    cvtColor(image_here,image_here1,CV_BGR2HSV);
    inRange(image_here1, Scalar(20, 100, 100), Scalar(30, 255, 255), image_here1);
    return image_here1;
    }

    

    int main(int argc, char* argv[])
    {
        VideoCapture cap(0); // open the video camera no. 0
    
        if (!cap.isOpened())  // if not success, exit program
        {
            cout << "Cannot open the video cam" << endl;
            return -1;
        }

        double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //width of frames of ideo
        double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //height of frames of the video
        Mat imgtrack(dWidth,dHeight,CV_8UC3);
        imgtrack.setTo(0);

        cout << "Frame size : " << dWidth << " x " << dHeight << endl;

        namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

        while (1)
        {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video
        //imshow("MyVideo", frame);
        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }
        

        cout<<"\nstarting thresholding\n";
         Mat imgYellowThresh=GetThresholdedImage(frame);
        cout<<"\nDone Thresholding\n";



        Moments m=moments(imgYellowThresh,true);
        double moment10=m.m10;
        double moment01=m.m01;
        double area=m.m00;
        
        cout<<"\n area if starting\n";

        if(area>1000)
        {
        cout<<"\n inside if\n";  //not printing any cout from here on
        int posX=moment10/area;
        int posY=moment01/area;
        if(lastX>=0 && lastY>=0 && posX>=0 && posY>=0)
        {
        line(imgtrack,Point(posX,posY),Point(lastX,lastY),Scalar(255,0,0),4);
        }
        lastX=posX;
        lastY=posY;

        imshow("MyVideo", frame);


        add(frame,imgtrack,frame);

      
        cout<<"end";
        if (waitKey(30) == 27) 
        {
        cout << "esc key is pressed by user" << endl;
        break;
        }
    }

    return 0;

    }
    }
Topic archived. No new replies allowed.