Detection line: Cvhoughlines,cvhoughlines2
Detection Circle: Cvhoughcircles
Detection rectangle: There is no corresponding function in the OPENCV, the following section of code can detect the rectangle, by first looking for a straight line, and then find the line parallel and vertical four lines.
To detect the line code:
/* This is a standalone program. Pass an image name as a-parameter of the program.
Switch between Standard and probabilistic Hough transform by changing ' #if 1 ' to ' #if 0 ' and back * *
#include <cv.h>
#include
#include <math.h>
int main (int argc, char** argv)
{
Const char* filename = argc >= 2? ARGV[1]: "Pic1.png";
iplimage* src = cvloadimage (filename, 0);
iplimage* DST;
Iplimage* COLOR_DST;
cvmemstorage* storage = cvcreatememstorage (0);
cvseq* lines = 0;
int i;
if (!SRC)
return-1;
DST = cvcreateimage (cvgetsize (SRC), 8, 1);
COLOR_DST = Cvcreateimage (cvgetsize (SRC), 8, 3);
Cvcanny (SRC, DST, 50, 200, 3);
Cvcvtcolor (DST, COLOR_DST, CV_GRAY2BGR);
#if 0
Lines = CvHoughLines2 (DST, storage, Cv_hough_standard, 1, cv_pi/180, 100, 0, 0);
for (i = 0; i < MIN (lines->total,100); i++)
{
float* line = (float*) Cvgetseqelem (lines,i);
Float rho = line[0];
Float theta = line[1];
Cvpoint pt1, pt2;
Double A = cos (theta), B = sin (theta);
Double x0 = a*rho, y0 = B*rho;
pt1.x = Cvround (x0 + 1000* (-B));
Pt1.y = Cvround (y0 + 1000* (a));
pt2.x = Cvround (x0-1000* (-B));
Pt2.y = Cvround (y0-1000* (a));
Cvline (COLOR_DST, pt1, Pt2, Cv_rgb (255,0,0), 3, CV_AA, 0);
}
#else
Lines = CvHoughLines2 (DST, storage, cv_hough_probabilistic, 1, cv_pi/180, 50, 50, 10);
for (i = 0; i < lines->total; i++)
{
cvpoint* line = (cvpoint*) Cvgetseqelem (lines,i);
Cvline (COLOR_DST, line[0], line[1], Cv_rgb (255,0,0), 3, CV_AA, 0);
}
#endif
Cvnamedwindow ("Source", 1);
Cvshowimage ("Source", SRC);
Cvnamedwindow ("Hough", 1);
Cvshowimage ("Hough", COLOR_DST);
Cvwaitkey (0);
return 0;
}
To detect a circle code:
#include <cv.h>
#include
#include <math.h>
int main (int argc, char** argv)
{
iplimage* img;
if (argc = = 2 && (img=cvloadimage (argv[1), 1))!= 0)
{
iplimage* Gray = cvcreateimage (Cvgetsize (IMG), 8, 1);
cvmemstorage* storage = cvcreatememstorage (0);
Cvcvtcolor (IMG, Gray, Cv_bgr2gray);
Cvsmooth (Gray, Gray, Cv_gaussian, 9, 9); Smooth it, otherwise a lot of false circles may is detected
cvseq* circles = cvhoughcircles (gray, storage, cv_hough_gradient, 2, GRAY->HEIGHT/4, 200, 100);
int i;
for (i = 0; i < circles->total; i++)
{
float* p = (float*) Cvgetseqelem (circles, i);
Cvcircle (IMG, Cvpoint (Cvround (p[0)), Cvround (p[1)), 3, Cv_rgb (0,255,0),-1, 8, 0);
Cvcircle (IMG, Cvpoint (Cvround (p[0)), Cvround (p[1)), Cvround (p[2)), Cv_rgb (255,0,0), 3, 8, 0);
}
Cvnamedwindow ("Circles", 1);
Cvshowimage ("Circles", IMG);
}
return 0;
}
To detect rectangular code:
/*
Find the rectangle in the program
*/
#ifdef _ch_
#pragma package <opencv>
#endif
#ifndef _eic
#include "Cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#endif
int thresh = 50;
iplimage* img = 0;
iplimage* img0 = 0;
cvmemstorage* storage = 0;
Cvpoint Pt[4];
Const char* Wndname = "Square detection Demo";
Helper function:
Finds a cosine of angle between vectors
Double Angle (cvpoint* pt1, cvpoint* pt2, cvpoint* pt0)
{
Double dx1 = pt1->x-pt0->x;
Double dy1 = pt1->y-pt0->y;
Double dx2 = pt2->x-pt0->x;
Double Dy2 = pt2->y-pt0->y;
Return (DX1*DX2 + dy1*dy2)/sqrt ((dx1*dx1 + dy1*dy1) * (dx2*dx2 + dy2*dy2) + 1e-10);
}
Returns sequence of squares detected on the image.
The sequence is stored in the specified memory storage
cvseq* findSquares4 (iplimage* img, cvmemstorage* storage)
{
cvseq* contours;
int I, C, l, N = 11;
Cvsize sz = cvsize (Img->width &-2, Img->height &-2);
iplimage* timg = Cvcloneimage (IMG); Make a copy of input image
iplimage* Pyr = Cvcreateimage (Cvsize (SZ.WIDTH/2, SZ.HEIGHT/2), 8, 3);
Iplimage* Tgray;
cvseq* result;
Double S, t;
Create empty sequence that would contain points-
4 points per square (the square ' s vertices)