#include #include "cv.h" #include "highgui.h" #include #define PI 3.14159265f usingnamespacecv; intmain() { intmax_r; //Maximum magnitude of r intmax_theta=179; //Maximum value of theta. int threshold=60; //Mimimum number of votes required by(theata,r) to form a straight line. intimg_width; //Width of the image intimg_height; //Height of the image intnum_theta=180; //Number of values of theta (0 - 179 degrees) intnum_r; //Number of values of r. int *accumulator; //accumulator to collect votes. longacc_size; //size of the accumulator in bytes. float Sin[num_theta]; //Array to store pre-calculated vales of sin(theta) float Cos[num_theta]; //Array to store pre-calculated vales of cos(theta) uchar *img_data; //pointer to image data for efficient access. inti,j; Mat src; Mat dst; /*Read and Displaytheimage*/ Mat image=imread("poly.png"); namedWindow("Polygon", 1 ); imshow("Polygon", image); //convert colortograyscaleimage cvtColor(image, src, CV_BGR2GRAY); /*Initializations*/ img_width = src.cols; img_height = src.rows; //calculating maximum valueofr.Rounditofftonearestinteger. max_r = round(sqrt( (img_width*img_width) + (img_height*img_height))); //calculating the numbervalesrcantake.-max_r<=r<=max_r num_r = (max_r *2) +1; //pre-compute the valuesofsin(theta)andcos(theta). for(i=0;i<=max_theta;i++) { Sin[i] = sin(i * (PI/180)); Cos[i] = cos(i * (PI/180)); } //Initializing the accumulator.Conceptuallyitisa2-Dmatrixwithdimensionrxtheta accumulator = new int[num_theta * num_r]; //calculating size ofaccumulatorinbytes. acc_size = sizeof(int)*num_theta * num_r; //Initializing elements ofaccumulatortozero. memset(accumulator,0,acc_size); //extracting the edges. Canny(src,dst,50,200,3); //Getting the imagedatafromMatdst img_data = dst.data; //Loop through allthepixels.Eachpixelisrepresentedby1byte. for(i=0;i0) { //if pixel is notblackdothefollowing. //For that pixelfindthethevaluesofrforcorrespondingvalueoftheta. //Value of rcanbenegative.(See the graph) //Minimum value ofris-max_r. //Conceptually the arraylookslikethis // 0 1 2 3 4 5 6 .. 178 179<---degrees // -max_r | | | | | | | | | | | // -max_r+1| | | | | | | | | | | // -max_r+2| | | | | | | | | | | // ... | | | | | | | | | | | // 0 | | | | | | | | | | | // 1 | | | | | | | | | | | // 2 | | | | | | | | | | | // ... | | | | | | | | | | | // max_r| | | | | | | | | | | // for(int t=0;t<=max_theta;t++) { //calculating the values of r for theta= t , x= j and y=i; int_r=round(j*Cos[t] + i*Sin[t]); //calculating the rowindexof_rintheaccumulator. intr_index=(max_r+_r); //Registering the votebyincrementingthevalueofaccumulator[r][theta] accumulator[r_index*num_theta + t]++; } } } } //Looping through eachelementintheaccumulator for(intr_index=0;r_indexthreshold) { //if votes receive isgreaterthanthethreshold //getting the valueoftheta int_theta=j; //getting the valueofr int_r=r_index-max_r; //Calculating points todrawtheline. Point pt1, pt2; pt1.x =0; pt1.y =round((_r - pt1.x*Cos[_theta])/Sin[_theta]); pt2.x =img_width; pt2.y =round((_r - pt2.x*Cos[_theta])/Sin[_theta]); //Drawing the line. line( image, pt1, pt2, Scalar(0,255,0), 3, CV_AA); } } } namedWindow("Detected Lines", 1 ); imshow("Detected Lines",image); //Free the memoryallocatedtoaccumulator. delete[] accumulator; waitKey(0); return 0; }