Skip to content

Commit 2585d09

Browse files
authored
Create Exercises_13_1-2-11.cpp
1 parent ae800a5 commit 2585d09

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

Exercises_13_1-2-11.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//Exercises_9_4.cpp Exercises at end of Chapter 9
2+
3+
#include <opencv2/opencv.hpp>
4+
#include <iostream>
5+
6+
using namespace cv;
7+
using namespace std;
8+
9+
void help(const char **argv) {
10+
cout << "\n\n"
11+
<< "This program solves the Exercise 4、5 at the end of Chapter 9 \n"
12+
<< "Call:\n"
13+
<< argv[0] << " <path/image_name1>" << " <path/image_name2>\n\n"
14+
<< "For example: ./" << argv[0] << " ../left.jpg "<< " ../left.jpg\n"
15+
<< endl;
16+
}
17+
18+
int main( int argc, const char** argv )
19+
{
20+
help(argv);
21+
if(argc < 3) {
22+
cout << "\nERROR: You had too few parameters.\n" << endl;
23+
return -1;
24+
}
25+
26+
/************************************************************************/
27+
/* 1.In this exercise, we learn to experiment with parameters by setting good low
28+
Thresh and highThresh values in cv::Canny(). Load an image with suitably
29+
interesting line structures. We’ll use three different high:low threshold settings of
30+
1.5:1, 2.75:1, and 4:1.
31+
a. Report what you see with a high setting of less than 50.
32+
b. Report what you see with high settings between 50 and 100.
33+
c. Report what you see with high settings between 100 and 150.
34+
d. Report what you see with high settings between 150 and 200.
35+
e. Report what you see with high settings between 200 and 250.
36+
f. Summarize your results and explain what happens as best you can. */
37+
/************************************************************************/
38+
Mat src = imread(argv[1],IMREAD_GRAYSCALE);
39+
Mat dst;
40+
41+
int iHighThresh = 50;
42+
Canny(src,dst,static_cast<int>(iHighThresh/1.5),iHighThresh);
43+
imshow("iHighThresh is 50 and high:low is 1.5:1",dst);
44+
Canny(src,dst,static_cast<int>(iHighThresh/2.75),iHighThresh);
45+
imshow("iHighThresh is 50 and high:low is 2.75:1",dst);
46+
Canny(src,dst,static_cast<int>(iHighThresh/4),iHighThresh);
47+
imshow("iHighThresh is 50 and high:low is 4:1",dst);
48+
49+
iHighThresh = (50+100)/2;
50+
Canny(src,dst,static_cast<int>(iHighThresh/1.5),iHighThresh);
51+
imshow("iHighThresh is (50+100)/2 and high:low is 1.5:1",dst);
52+
Canny(src,dst,static_cast<int>(iHighThresh/2.75),iHighThresh);
53+
imshow("iHighThresh is (50+100)/2 and high:low is 2.75:1",dst);
54+
Canny(src,dst,static_cast<int>(iHighThresh/4),iHighThresh);
55+
imshow("iHighThresh is (50+100)/2 and high:low is 4:1",dst);
56+
57+
iHighThresh = (100+150)/2;
58+
Canny(src,dst,static_cast<int>(iHighThresh/1.5),iHighThresh);
59+
imshow("iHighThresh is (100+150)/2 and high:low is 1.5:1",dst);
60+
Canny(src,dst,static_cast<int>(iHighThresh/2.75),iHighThresh);
61+
imshow("iHighThresh is (100+150)/2 and high:low is 2.75:1",dst);
62+
Canny(src,dst,static_cast<int>(iHighThresh/4),iHighThresh);
63+
imshow("iHighThresh is (100+150)/2 and high:low is 4:1",dst);
64+
65+
iHighThresh = (150+200)/2;
66+
Canny(src,dst,static_cast<int>(iHighThresh/1.5),iHighThresh);
67+
imshow("iHighThresh is (150+200)/2 and high:low is 1.5:1",dst);
68+
Canny(src,dst,static_cast<int>(iHighThresh/2.75),iHighThresh);
69+
imshow("iHighThresh is (150+200)/2 and high:low is 2.75:1",dst);
70+
Canny(src,dst,static_cast<int>(iHighThresh/4),iHighThresh);
71+
imshow("iHighThresh is (150+200)/2 and high:low is 4:1",dst);
72+
73+
iHighThresh = (200+250)/2;
74+
Canny(src,dst,static_cast<int>(iHighThresh/1.5),iHighThresh);
75+
imshow("iHighThresh is (200+250)/2 and high:low is 1.5:1",dst);
76+
Canny(src,dst,static_cast<int>(iHighThresh/2.75),iHighThresh);
77+
imshow("iHighThresh is (200+250)/2 and high:low is 2.75:1",dst);
78+
Canny(src,dst,static_cast<int>(iHighThresh/4),iHighThresh);
79+
imshow("iHighThresh is (200+250)/2 and high:low is 4:1",dst);
80+
81+
/************************************************************************/
82+
/* 2. Load an image containing clear lines and circles such as a side view of a bicycle.
83+
Use the Hough line and Hough circle calls and see how they respond to your
84+
image. */
85+
/************************************************************************/
86+
Mat src = imread(argv[1],IMREAD_GRAYSCALE);//a bike in gray
87+
GaussianBlur( src, src, Size(9, 9), 2, 2 );
88+
Mat temp;
89+
vector<Vec4i> linesP;
90+
vector<Vec3f> circles;
91+
//first find the canny edge
92+
Canny(src,temp,50,200);
93+
//find lines
94+
HoughLinesP(temp,linesP,1,CV_PI/180,80,50,10);
95+
//find circles
96+
HoughCircles( src, circles, CV_HOUGH_GRADIENT, 1, src.rows/8, 200, 100, 0, 0 );
97+
//draw lines and circles in the source image
98+
for (int i = 0;i<linesP.size();i++)
99+
{
100+
Vec4i l = linesP[i];
101+
line(src,Point(l[0],l[1]),Point(l[2],l[3]),Scalar(255));
102+
}
103+
104+
for( size_t i = 0; i < circles.size(); i++ )
105+
{
106+
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
107+
int radius = cvRound(circles[i][2]);
108+
// circle center
109+
circle( src, center, 3, Scalar(255,255,255), -1, 8, 0 );
110+
// circle outline
111+
circle( src, center, radius, Scalar(255,255,255), 1, 8, 0 );
112+
}
113+
114+
/*11. Create an image with a 20 × 20 square in it. Rotate it to an arbitrary angle. Take a
115+
distance transform of this image. Create a 20 × 20 square shape. Use the distance
116+
transform image to algorithmically overlay the shape onto the rotated square in
117+
the image you made.*/
118+
// create a 20 × 20 square
119+
Mat matBoard = Mat(500,500,CV_8UC3,Scalar(0));
120+
rectangle(matBoard,Rect(250,250,20,20),Scalar(255,255,255),-1);
121+
// create an arbitary angle
122+
cv::RNG rng = cv::theRNG();
123+
float fDegree = rng.uniform(0,360);
124+
Mat rotMatS = getRotationMatrix2D(Point(250,250), fDegree, 1.0);
125+
warpAffine(matBoard, matBoard, rotMatS, matBoard.size(), 1, 0, Scalar(0,0,0));
126+
//solove the problem
127+
Mat matDistance;
128+
cvtColor(matBoard,gray,COLOR_BGR2GRAY);
129+
threshold(gray,gray,100,255,CV_THRESH_BINARY);
130+
distanceTransform(gray,matDistance,CV_DIST_L2,3);
131+
//find the center corners
132+
double minVal;
133+
double maxVal2;
134+
Point minLoc,maxLoc;
135+
minMaxLoc( matDistance, &minVal, &maxVal2, &minLoc, &maxLoc, matDistance>0);
136+
//draw the result
137+
circle(matBoard,maxLoc,1,Scalar(0),1);
138+
circle(matBoard,minLoc,2,Scalar(255),2);
139+
rectangle(matBoard,Rect(maxLoc.x - 10,maxLoc.y - 10,20,20),Scalar(0,0,255),-1);
140+
return 0;
141+
}
142+

0 commit comments

Comments
 (0)