-
Notifications
You must be signed in to change notification settings - Fork 0
/
hough transform.cpp
146 lines (87 loc) · 2.92 KB
/
hough transform.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<stdio.h>
#include<iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
/*int main(int argc,char**argv) {
char inputimage[] = "C:/Users/sumaira/mscs/Documents/Visual Studio 2015/Projects/COMPUTER VISION USING OPENCV/obtaininghist/obtaininghist/image030.png";
Mat src = imread(inputimage,0);
if (!src.data) {
cout << "image is empty" << endl;
exit(1);
}
Mat dst, cdst;
Canny(src,dst,50,200,3);
cvtColor(dst,cdst,CV_GRAY2BGR);
vector<Vec2f>lines;
HoughLines(dst, lines, 1, CV_PI / 180, 200, 0, 0); //vector to store the detection lines
for (size_t i = 0; i < lines.size(); i++) { //code to paint the lines in the image
float rho = lines[i][0], theta = lines[i][1];
Point 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));
line(cdst, pt1, pt2, Scalar(0, 255),3, CV_AA);
}
imshow("source", src);
resizeWindow("source", 512, 512);
imshow("detected lines", cdst);
resizeWindow("detected lines", 512, 512);
cvWaitKey(0);
return 0;
}
*/
/*
int main(int argc,char**argv) {
char inputimage[] = "C:/Users/sumaira/mscs/Documents/Visual Studio 2015/Projects/COMPUTER VISION USING OPENCV/obtaininghist/obtaininghist/image030.png";
Mat src = imread(inputimage,0);
if (!src.data) {
cout << "image is empty" << endl;
exit(1);
}
Mat dst, cdst;
Canny(src,dst,50,200,3);
cvtColor(dst,cdst,CV_GRAY2BGR);
vector<Vec4i>lines;
HoughLinesP(dst, lines, 1, CV_PI / 180, 50, 50, 10); //vector to store the detection lines
for (size_t i = 0; i < lines.size(); i++) {
Vec4i l =lines[i];
line(cdst, Point(l[0],l[1]), Point(l[2],l[3]), Scalar(0,0, 255),3, CV_AA);
}
imshow("source",src);
resizeWindow("source",512,512);
imshow("detected lines", cdst);
cvResizeWindow("detected lines",512,512);
waitKey(0);
return 0;
}
*/
int main(int argc,char**argv) {
Mat src,src_gray;
//char inputimage[] = "C:/Users/sumaira/mscs/Documents/Visual Studio 2015/Projects/COMPUTER VISION USING OPENCV/obtaininghist/obtaininghist/image030.png";
char inputimage[] = "TIRE.JPG";
src = imread(inputimage,1);
if (!src.data) {
cout << "image is empty" << endl;
exit(1);
}
cvtColor(src,src_gray,CV_BGR2GRAY);
GaussianBlur(src_gray,src_gray,Size(9,9),2,2);
vector<Vec3f>circles;
HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT,1, src_gray.rows /6, 100, 75, 0,0); //vector to store the detection lines
for (size_t i = 0; i < circles.size(); i++) {
Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
int radius =(cvRound(circles[i][2]));
circle(src,center,3,Scalar(0,255,0),-1,8,0);
circle(src,center,radius,Scalar(255,0,0),3,8,0);
}
namedWindow("hough transform demo",CV_WINDOW_AUTOSIZE);
imshow("hough transform demo",src);
waitKey(0);
return 0;
}