Ask Your Question

Help me to track the mistake for measuring angles in opencv

asked 2013-06-24 01:26:32 -0600

jigarsen18 gravatar image

Hi all,

Here i have put my code which is in c++. I am new in OpenCV. So Please Help me.

I found this code in Python and try to convert it in C++ and thanks to chris loughnane who is original author of this here for more detail.

enter code here
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <math.h>
#include <string.h>

using namespace cv;
using namespace std;

int main()
    CvCapture *capture  = cvCaptureFromCAM(0);
    cvNamedWindow("Target", 1);

    //#initiate font
    CvFont *font;
    cvInitFont(font,CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8);
    //instantiate images
    IplImage* hsv_img = cvCreateImage(cvGetSize(cvQueryFrame(capture)),8,3);
    IplImage* threshold_img1 = cvCreateImage(cvGetSize(hsv_img),8,1);
    IplImage* threshold_img1a = cvCreateImage(cvGetSize(hsv_img),8,1);
    IplImage* threshold_img2 = cvCreateImage(cvGetSize(hsv_img),8,1);

    int i=0;
    CvCapture * writer;  // = cvCreateVideoWriter("angle_tracking.avi",CV_FOURCC('M','J','P','G'),30,cvGetSize(hsv_img),1);

    while (true)
       // capture the image from the cam
        IplImage *img = cvQueryFrame(capture);

        //convert the image to HSV

        //threshold the image to isolate two colors

        cvInRangeS(hsv_img,cvScalar(165,145,100),cvScalar(250,210,160),threshold_img1);     //#red
        cvInRangeS(hsv_img,cvScalar(0,145,100),cvScalar(10,210,160),threshold_img1a);       //#red again
        cvAdd(threshold_img1,threshold_img1a,threshold_img1);                               //#this is combining the two limits for red
        cvInRangeS(hsv_img,cvScalar(105,180,40),cvScalar(120,260,100),threshold_img2);      //#blue

      //  #determine the moments of the two objects
        CvMat stub;
        CvMat *threshold_img1 = cvGetMat(threshold_img1,&stub);
        CvMat *threshold_img2 = cvGetMat(threshold_img2,&stub);

        CvMoments *moments1,*moments2;

        double area1 = cvGetCentralMoment(moments1,0,0);
        double area2 = cvGetCentralMoment(moments2,0,0);

        //initialize x and y
        float x1,y1,x2,y2=(1,2,3,4);
        float coord_list=(x1,y1,x2,y2);

        for (i=1; i < 4; i++)
        int x=0;
        //there can be noise in the video so ignore objects with small areas

        if (area1 >200000)
            //x and y coordinates of the center of the object is found by dividing the 1,0 and 0,1 moments by the area

            //draw circle

            //write x and y position
            cvPutText(img,char (str(x1)+","+str(y1)),cvPoint(x1,y1+20),font, CV_RGB(255,0,0)); //#Draw the text

        if (area2 >100000)
            //x and y coordinates of the center of the object is found by dividing the 1,0 and 0,1 moments by the area

            //draw circle

            //cvPutText(img,str(x2)+","+str(y2),cvPoint(x2,y2+20),font, cvScalar(255)); //#Draw the text
            //#draw ...
edit retag flag offensive close merge delete


Do you have a specific question? What happens, what should happen?

FooBar gravatar imageFooBar ( 2014-09-11 05:32:42 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-09-11 04:35:40 -0600

coco gravatar image

Hey dude...did you find answer? Let me know if you would

edit flag offensive delete link more


coco, dare to ask your own question, please.

berak gravatar imageberak ( 2014-09-11 04:38:39 -0600 )edit

berak I can't find any answer or question.

I am stuck on one problem about how to find any pixels coordinates from contour.

I already use findcontour , drawcontour. I want any specific pixels from it. Can you help me

coco gravatar imagecoco ( 2014-10-16 01:41:36 -0600 )edit

again, ask your question

i will delete this soon.

berak gravatar imageberak ( 2014-10-16 02:01:39 -0600 )edit

Question Tools


Asked: 2013-06-24 01:26:32 -0600

Seen: 1,364 times

Last updated: Sep 11 '14