Ask Your Question
0

Circle Detection using live vision camera

asked 2014-01-05 12:48:58 -0600

vibek4989 gravatar image

updated 2014-01-07 04:02:15 -0600

Siegfried gravatar image
#include<iostream>
#include<cv.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<stdio.h>
#include<math.h>

using namespace cv;
using namespace std;
#define CLS()

//printf("\033 [H\033[2J");
//fflush(stdout);
typedef struct {

   Point sum;
   Point sum_abs;
   Point av_sum;
   Point av_abs;
   float av_angle;
   int n;
} LineStruct;

float calcAngle(Point delta)
{
   float angle;
     if(delta.y !=0)
      angle =(float)atan(((double)delta.x/ -(double)delta.y));
        else
            angle =CV_PI/2;
return angle;
}
void setLabel(Mat& dst, string label, vector& contour)
{ 
int fontface = FONT_HERSHEY_PLAIN;
double scale =0.5;
int thickness =1;
int baseline =0;
//char text[50];
Size text = getTextSize(label, fontface, scale, thickness, &baseline);
Rect r = boundingRect(contour); 
//putText(im, label, fontFace, fontScale, CV_RGB(0,0,0), thickness, 8);//char screenshot[50];
//int screenshot_counter =0;
//putText(cdst, text, label, fontface, scale, Scalar(0,255,0), thickness, 8);
}
int main(int agrc, char * argv[1])
{
CvCapture* capture =0;
capture =cvCreateFileCapture(argv[1]);
if (!capture)
{
  return -1;
}
IplImage *bgr_frame=cvQueryFrame(capture);
CvSize size =cvSize((int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT));
IplImage* pgm_frame= cvCreateImage(size,IPL_DEPTH_8U,1);
int frame_count=0;
int frames =(int) cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);

//char text[50];
//int fontFace = FONT_HERSHEY_PLAIN;
//double fontScale =1;
//int thickness =1;
char screenshot[50];
int screenshot_counter =0;
 while( (bgr_frame=cvQueryFrame(capture)) !=NULL)
{
frame_count++;
if(frame_count==frames-1)
{
  cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,0);
frame_count=0;
}
cvConvertImage(bgr_frame, pgm_frame,0);
Mat gray=pgm_frame;
Mat dst = bgr_frame;
GaussianBlur( gray, gray,Size(9,9), 2,2);
Canny (gray, gray, 50, 200,3);
vector<vector<Point> >contours;
findContours(gray.clone(),contours,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
vector<Point>approx;
//Mat dst = bgr_frame.clone();
for (int i=0;i< contours.size(); i++)
{
    approxPolyDP(Mat(contours[i]),approx, arcLength(cv::Mat(contours[i]),true)* 0.02,true);
     if(fabs(contourArea(contours[i]))<100 || ! isContourConvex(approx))
        //{
           continue;
//}

if(approx.size() >= 4 && approx.size() >6)
{

   printf("circle Detection\n");
double area =contourArea(contours[i]);
Rect  r= boundingRect(contours[i]);
int radius =r.width/2;
if (abs(1-((double)r.width/r.height))<= 0.2 && abs(1-(area/(CV_PI * pow(radius,2))))<= 0.2)
{
       setLabel(dst, "CIR", contours[i]);
}
}
}

Point circle_center;
circle(dst, Point(size.width/2,size.height/2),4,Scalar(255,0,0),-1,8,0);
CLS();
if ((!pgm_frame)||(!bgr_frame))
 break;
cvShowImage("Edges",pgm_frame);
cvShowImage("Result",bgr_frame);
imshow("Detected Lines", dst);
char c= cvWaitKey(33);
if(c=='p')

{ 
c=0;
while(1)
{ 
c =cvWaitKey(33);
if (c=='p') break;
if (c==27) break;
if (c=='s')
{
sprintf(screenshot,"screenshot %d.jpg", screenshot_counter);
imwrite(screenshot, dst);
screenshot_counter++;
}
}
}
if (c==27)
break;
if(c=='s')
{
 sprintf(screenshot,"Screenshot%d.jpg", screenshot_counter);
 imwrite(screenshot,dst);
screenshot_counter++;
}
}
cvReleaseImage(&bgr_frame);
cvReleaseImage(&pgm_frame);
cvReleaseCapture(&capture);
cvDestroyWindow("Edges");
cvDestroyWindow("Result");
cvDestroyWindow("Detected Lines");
return 0;
}

But it showing some error, i tried a lot to find out according Opencv Tutorial, but couldn't. The error are as follows,

circle_detect.cpp:34:39: error: ‘vector’ is not a type circle_detect.cpp: In function ‘int main(int, char**)’: circle_detect.cpp:101:40: error: invalid initialization of reference ... (more)

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
2

answered 2014-01-07 01:58:40 -0600

Nghia gravatar image

Add

#include <vector>

to the top

edit flag offensive delete link more
0

answered 2014-06-11 11:56:09 -0600

Bunty gravatar image

Hi, I am beginner to opencv. I am learning above code but on compiled, i got following error,

  1. vector is not type ( include<vector.h> is included )
  2. invalid markers at Rect r= boundingRect(contour);

what is best way to understand the codes means like in java we used to look at API.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-01-05 12:48:58 -0600

Seen: 1,582 times

Last updated: Jun 11 '14