Skip to content

Commit cc93cde

Browse files
authored
Create Exercises_11-1-2-5-6-7.cpp
1 parent df34c2f commit cc93cde

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

Exercises_11-1-2-5-6-7.cpp

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//Exercises at end of Chapter 11
2+
// 1、2、5、6 and 7
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 Exercises at the end of Chapter 11(without 3、4 and 8)\n"
12+
<< "Call:\n"
13+
<< argv[0] << " <path/image_name>\n\n"
14+
<< "For example: " << argv[0] << " /AverageMaleFace.jpg\n"
15+
<< endl;
16+
}
17+
18+
// Get the coordinates of the points after the rotation
19+
Point2f getPointAffinedPos(const Point2f src, const Point2f center, double angle)
20+
{
21+
Point dst;
22+
int x = src.x - center.x;
23+
int y = src.y - center.y;
24+
25+
dst.x = x * cos(angle) + y * sin(angle) + center.x;
26+
dst.y = -x * sin(angle) + y * cos(angle) + center.y;
27+
return dst;
28+
}
29+
30+
int main( int argc, const char** argv )
31+
{
32+
help(argv);
33+
if(argc < 2) {
34+
cout << "\nERROR: You had too few parameters.\n" << endl;
35+
return -1;
36+
}
37+
/************************************************************************/
38+
/* 1. Find and load a picture of a face where the face is frontal, has eyes open, and
39+
takes up most or all of the image area. Write code to find the pupils of the eyes. */
40+
/************************************************************************/
41+
Mat matFrontFace = imread(argv[1]);
42+
Mat gray;Mat temp;
43+
double minPixelValue, maxPixelValue;
44+
Point minPixelPoint,maxPixelPoint;
45+
cvtColor(matFrontFace,gray,COLOR_BGR2GRAY);
46+
// Laplacian pyramid
47+
pyrDown(gray,temp);
48+
pyrUp(temp,temp);
49+
temp = gray - temp;
50+
// find and circle the result
51+
minMaxLoc(temp,&minPixelValue,&maxPixelValue,&minPixelPoint,&maxPixelPoint);
52+
circle(matFrontFace,maxPixelPoint,10,Scalar(255,255,255),2);
53+
/************************************************************************/
54+
/* 2. Look at the diagrams of how the log-polar function transforms a square into a
55+
wavy line.
56+
a. Draw the log-polar results if the log-polar center point were sitting on one of
57+
the corners of the square.
58+
b. What would a circle look like in a log-polar transform if the center point were
59+
inside the circle and close to the edge?
60+
c. Draw what the transform would look like if the center point were sitting just
61+
outside of the circle. */
62+
/************************************************************************/
63+
// draw a circle,white on black.
64+
Mat matLogPolar = Mat(512,512,CV_8U,Scalar(0));
65+
circle(matLogPolar,Point(256,256),100,Scalar(255),3);
66+
//a
67+
logPolar(matLogPolar,temp,Point(0,0),40,INTER_CUBIC);
68+
//b
69+
logPolar(matLogPolar,temp,Point(256-101,256),40,INTER_CUBIC);
70+
//c the result like two rings
71+
logPolar(matLogPolar,temp,Point(256-101-3,256),40,INTER_CUBIC);
72+
/************************************************************************/
73+
/* 5. Load an image, take a perspective transform, and then rotate it. Can this trans‐
74+
form be done in one step? */
75+
/************************************************************************/
76+
Mat matE5_1 = imread(argv[1]);
77+
Mat matE5_2 = imread(argv[1]);
78+
double angle = 45;
79+
//perspective matrix
80+
Point2f src_vertices[4];
81+
src_vertices[0] = Point(0, 0);
82+
src_vertices[1] = Point(matE5_1.cols, 0);
83+
src_vertices[2] = Point(matE5_1.cols, matE5_1.rows);
84+
src_vertices[3] = Point(0, matE5_1.rows);
85+
Point2f dst_vertices[4];
86+
dst_vertices[0] = Point(100, 0);
87+
dst_vertices[1] = Point(matE5_1.cols - 100, 0);
88+
dst_vertices[2] = Point(matE5_1.cols, matE5_1.rows);
89+
dst_vertices[3] = Point(0, matE5_1.rows);
90+
Mat perspectiveMatrix = getPerspectiveTransform(src_vertices, dst_vertices);
91+
//roate matrix
92+
Mat affineMatrix = getRotationMatrix2D(Point(matE5_1.cols/2,matE5_1.rows/2), angle, 1.0 );
93+
// perspective -> rotate
94+
warpPerspective(
95+
matE5_1,
96+
matE5_1,
97+
perspectiveMatrix,
98+
matE5_1.size(),
99+
INTER_LINEAR,
100+
BORDER_CONSTANT,
101+
Scalar());
102+
warpAffine(
103+
matE5_1,
104+
matE5_1,
105+
affineMatrix,
106+
matE5_1.size(),
107+
INTER_LINEAR,
108+
BORDER_CONSTANT,
109+
Scalar()
110+
);
111+
// do it in one step
112+
Point center = Point(matE5_2.cols/2,matE5_2.rows/2);
113+
dst_vertices[0] = getPointAffinedPos(dst_vertices[0],center,angle);
114+
dst_vertices[1] = getPointAffinedPos(dst_vertices[1],center,angle);
115+
dst_vertices[2] = getPointAffinedPos(dst_vertices[2],center,angle);
116+
dst_vertices[3] = getPointAffinedPos(dst_vertices[3],center,angle);
117+
perspectiveMatrix = getPerspectiveTransform(src_vertices, dst_vertices);
118+
warpPerspective(
119+
matE5_2,
120+
matE5_2,
121+
perspectiveMatrix,
122+
matE5_2.size(),
123+
INTER_LINEAR,
124+
BORDER_CONSTANT,
125+
Scalar());
126+
// the result is almost the same
127+
/************************************************************************/
128+
/* 6. Inpainting works pretty well for the repair of writing over textured regions. What
129+
would happen if the writing obscured a real object edge in a picture? Try it. */
130+
/************************************************************************/
131+
Mat matInpaint = imread(argv[1]);
132+
Mat matInpaintMask = Mat(matInpaint.size(),CV_8UC1,Scalar(0));//the same size,all in black(oh yeah!)
133+
//draw the same circle on the matInpaint and matInpaintMask
134+
circle(matInpaint,Point(255,255),100,Scalar(255),10);
135+
circle(matInpaintMask,Point(255,255),100,Scalar(255),10);
136+
imshow("befor inpaint",matInpaint);
137+
inpaint(matInpaint,matInpaintMask,matInpaint,10,CV_INPAINT_TELEA);
138+
imshow("after inpaint",matInpaint);
139+
//the result is :if "the writing obscured a real object edge in a picture",the result is bad
140+
/************************************************************************/
141+
/* 7. Practice histogram equalization on images that you load in, and report the
142+
results. */
143+
/************************************************************************/
144+
Mat matSrc = imread(argv[1]);
145+
vector<Mat> planes;
146+
split(matSrc,planes);
147+
imshow("b",planes[0]);
148+
imshow("g",planes[1]);
149+
imshow("r",planes[2]);
150+
cv::equalizeHist(planes[0],planes[0]);
151+
cv::equalizeHist(planes[1],planes[1]);
152+
cv::equalizeHist(planes[2],planes[2]);
153+
imshow("bh",planes[0]);
154+
imshow("gh",planes[1]);
155+
imshow("rh",planes[2]);
156+
waitKey();
157+
return 0;
158+
159+
}

0 commit comments

Comments
 (0)