Ask Your Question
0

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 thing.click here for more detail.

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

using namespace cv;
using namespace std;

int main()
{
    CvCapture *capture  = cvCaptureFromCAM(0);
    cvNamedWindow("Target", 1);
    cvNamedWindow("Threshold1",1);
    cvNamedWindow("Threshold2",1);
    cvNamedWindow("hsv",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);
    cvCreateVideoWriter("d:/1/1.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
        cvCvtColor(img,hsv_img,CV_BGR2HSV);

        //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;
        cvMoments(threshold_img1,moments1);
        cvMoments(threshold_img2,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
            x1=int(cvGetSpatialMoment(moments1,1,0)/area1);
            y1=int(cvGetSpatialMoment(moments1,0,1)/area1);

            //draw circle
            cvCircle(img,cvPoint(x1,y1),2,CV_RGB(0,255,0),20);

            //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
            x2=int(cvGetSpatialMoment(moments2,1,0)/area2);
            y2=int(cvGetSpatialMoment(moments2,0,1)/area2);

            //draw circle
            cvCircle(img,cvPoint(x2,y2),2,CV_RGB(0,255,0),20);

            //cvPutText(img,str(x2)+","+str(y2),cvPoint(x2,y2+20),font, cvScalar(255)); //#Draw the text
            cvLine(img,cvPoint(x1,y1),cvPoint(x2,y2),CV_RGB(0,255,0),4,CV_AA);
            //#draw ...
(more)
edit retag flag offensive close merge delete

Comments

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
-1

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

Comments

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
Login/Signup to Answer

Question Tools

Stats

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

Seen: 758 times

Last updated: Sep 11 '14