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 line and angle
cvLine(img,cvPoint(x1,y1),cvPoint(cvGetSize(img)[0],y1),cvScalar(100,100,100,100),4,CV_AA);
x1=float(x1);
y1=float(y1);
x2=float(x2);
y2=float(y2);
//double angle = int(math.atan((y1-y2)/(x2-x1))*180/math.pi);
char angle = int(atan((y1-y2)/(x2-x1))*180/3.14);
cvPutText(img, &angle ,cvPoint(int(x1)+50,(int(y2)+int(y1))/2),font,CV_RGB(0,255,0));
}
//#cvWriteFrame(writer,img)
//#display frames to users
cvShowImage("Target",img);
cvShowImage("Threshold1",threshold_img1);
cvShowImage("Threshold2",threshold_img2);
cvShowImage("hsv",hsv_img);
}
// Listen for ESC or ENTER key
waitKey(0);
destroyAllWindows();
return(0);
}
thanks in advance.