-
Notifications
You must be signed in to change notification settings - Fork 0
/
houghlines.cpp
100 lines (89 loc) · 2.06 KB
/
houghlines.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "opencv2/opencv.hpp"
using namespace std;
#define PIX(img,i,j,k) (((uchar*)img->imageData)[i*img->widthStep+j*img->nChannels+k])
void converttobin(IplImage *img)
{
int i, j,ht, wd;
ht=img->height;
wd=img->width;
for(i=0;i<ht;i++)
for(j=0;j<wd;j++)
PIX(img, i,j,0)=(PIX(img,i,j,0)==255)?0:255;
}
void plotpoint(IplImage* res, int y, int x)
{
int t;
for(t=0; t<500; t++)
{
float theta = (6.28/500)*t;
int rad =(int)((x*cos(theta) + y*sin(theta)));
if(rad >= res->height || rad < 0 || t >= res->width || t < 0)
{
//printf("error\n");
continue;
}
PIX(res, rad, t, 0)++;
}
}
void detectcircles(IplImage *res,IplImage* res2, IplImage* bin)
{
int i,j,ht,wd,max;
wd=bin->width;
ht=bin->height;
for(i=0;i<ht;i++)
{
for(j=0;j<wd;j++)
{
if(PIX(bin,i,j,0)==255)
plotpoint(res,i,j);
}
}
max=PIX(res,0,0,0);
for(i=0;i<700;i++)
for(j=0;j<500;j++)
{
if(max<PIX(res,i,j,0))
max=PIX(res,i,j,0);
}
// printf("max = %d\n", max);
cout<<"max = "<<max<<endl;
//scaling the ditribution;
for(i=0;i<700;i++)
for(j=0;j<500;j++)
{
PIX(res2,i,j,0)=(PIX(res,i,j,0)/(float)max)*255;
}
}
int main()
{
//CvCapture *capture;
//capture=cvCreateCameraCapture(-1);
IplImage *frame,*grey,*res,*res2;
int i,j;
frame=cvLoadImage("lines.png",1);
grey=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
res=cvCreateImage(cvSize(500,700),32,1);
res2=cvCreateImage(cvSize(500,700),IPL_DEPTH_8U,1);
for(i=0;i<700;i++)
for(j=0;j<500;j++)
{
PIX(res,i,j,0)=0;
PIX(res2,i,j,0)=0;
}
char win[]="binarymatrix";
cvNamedWindow(win,CV_WINDOW_AUTOSIZE);
//frame=cvQueryFrame(capture);
cvCvtColor(frame, grey,CV_BGR2GRAY);
converttobin(grey); //this function also inverts image along with conversion to binary
cvNamedWindow("grey",CV_WINDOW_AUTOSIZE);
cvShowImage("grey", grey);
detectcircles(res,res2,grey); // grey has already been converted to binary and inverted
cvShowImage(win, res2);
cvWaitKey(0);
cvReleaseImage(&frame);
cvReleaseImage(&grey);
cvReleaseImage(&res);
cvReleaseImage(&res2);
cvDestroyWindow(win);
return 0;
}