Skip to content

Commit 151484b

Browse files
authored
Merge pull request #64 from oreillymedia/chap12extras
Added new examples to chapter 12
2 parents db4cadd + 2bb8aa5 commit 151484b

File tree

5 files changed

+343
-76
lines changed

5 files changed

+343
-76
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ add_executable( example_11-02 example_11-02.cpp )
5555
add_executable( example_11-03 example_11-03.cpp )
5656
add_executable( example_12-01 example_12-01.cpp )
5757
add_executable( example_12-02 example_12-02.cpp )
58+
add_executable( example_12-03 example_12-03.cpp )
59+
add_executable( example_12-04 example_12-04.cpp )
5860
add_executable( example_13-01 example_13-01.cpp )
5961
add_executable( example_13-02 example_13-02.cpp )
6062
add_executable( example_13-03 example_13-03.cpp )
@@ -133,6 +135,8 @@ target_link_libraries( example_11-02 ${OpenCV_LIBS} )
133135
target_link_libraries( example_11-03 ${OpenCV_LIBS} )
134136
target_link_libraries( example_12-01 ${OpenCV_LIBS} )
135137
target_link_libraries( example_12-02 ${OpenCV_LIBS} )
138+
target_link_libraries( example_12-03 ${OpenCV_LIBS} )
139+
target_link_libraries( example_12-04 ${OpenCV_LIBS} )
136140
target_link_libraries( example_13-01 ${OpenCV_LIBS} )
137141
target_link_libraries( example_13-02 ${OpenCV_LIBS} )
138142
target_link_libraries( example_13-03 ${OpenCV_LIBS} )

example_12-01.cpp

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,64 @@
1-
// Example 12-1. Using cv::dft() and cv::idft() to accelerate the computation of
2-
// convolutions
1+
// Example 12-1. Using cv::dft() and cv::idft() to accelerate
2+
// the computation of convolutions
33

4-
5-
#include <opencv2/opencv.hpp>
64
#include <iostream>
5+
#include <opencv2/opencv.hpp>
76

8-
using namespace std;
7+
using std::cout;
8+
using std::endl;
99

1010
int main(int argc, char** argv) {
11+
if (argc != 2) {
12+
cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the"
13+
<< "\n computation of convolutions"
14+
<< "\nFourier Transform\nUsage: "
15+
<< argv[0] << " <path/imagename>\n" << endl;
16+
return -1;
17+
}
1118

12-
if(argc != 2) {
13-
cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the"
14-
<< "\n computation of convolutions"
15-
<< "\nFourier Transform\nUsage: " <<argv[0] <<" <path/imagename>\n" << endl;
16-
return -1;
17-
}
18-
19-
cv::Mat A = cv::imread(argv[1],0);
19+
cv::Mat A = cv::imread(argv[1], 0);
2020

21-
if( A.empty() ) { cout << "Cannot load " << argv[1] << endl; return -1; }
21+
if (A.empty()) {
22+
cout << "Cannot load " << argv[1] << endl;
23+
return -1;
24+
}
2225

23-
cv::Size patchSize( 100, 100 );
24-
cv::Point topleft( A.cols/2, A.rows/2 );
25-
cv::Rect roi( topleft.x, topleft.y, patchSize.width, patchSize.height );
26-
cv::Mat B = A( roi );
26+
cv::Size patchSize(100, 100);
27+
cv::Point topleft(A.cols / 2, A.rows /2);
28+
cv::Rect roi(topleft.x, topleft.y, patchSize.width, patchSize.height);
29+
cv::Mat B = A(roi);
2730

28-
int dft_M = cv::getOptimalDFTSize( A.rows+B.rows-1 );
29-
int dft_N = cv::getOptimalDFTSize( A.cols+B.cols-1 );
31+
int dft_M = cv::getOptimalDFTSize(A.rows + B.rows - 1);
32+
int dft_N = cv::getOptimalDFTSize(A.cols + B.cols - 1);
3033

31-
cv::Mat dft_A = cv::Mat::zeros( dft_M, dft_N, CV_32F );
32-
cv::Mat dft_B = cv::Mat::zeros( dft_M, dft_N, CV_32F );
34+
cv::Mat dft_A = cv::Mat::zeros(dft_M, dft_N, CV_32F);
35+
cv::Mat dft_B = cv::Mat::zeros(dft_M, dft_N, CV_32F);
3336

34-
cv::Mat dft_A_part = dft_A( cv::Rect(0, 0, A.cols,A.rows) );
35-
cv::Mat dft_B_part = dft_B( cv::Rect(0, 0, B.cols,B.rows) );
37+
cv::Mat dft_A_part = dft_A(cv::Rect(0, 0, A.cols, A.rows));
38+
cv::Mat dft_B_part = dft_B(cv::Rect(0, 0, B.cols, B.rows));
3639

37-
A.convertTo( dft_A_part, dft_A_part.type(), 1, -mean(A)[0] );
38-
B.convertTo( dft_B_part, dft_B_part.type(), 1, -mean(B)[0] );
40+
A.convertTo(dft_A_part, dft_A_part.type(), 1, -mean(A)[0]);
41+
B.convertTo(dft_B_part, dft_B_part.type(), 1, -mean(B)[0]);
3942

40-
cv::dft( dft_A, dft_A, 0, A.rows );
41-
cv::dft( dft_B, dft_B, 0, B.rows );
43+
cv::dft(dft_A, dft_A, 0, A.rows);
44+
cv::dft(dft_B, dft_B, 0, B.rows);
4245

43-
// set the last parameter to false to compute convolution instead of correlation
44-
//
45-
cv::mulSpectrums( dft_A, dft_B, dft_A, 0, true );
46-
cv::idft( dft_A, dft_A, cv::DFT_SCALE, A.rows + B.rows - 1 );
46+
// set the last parameter to false to compute convolution instead of correlation
47+
//
48+
cv::mulSpectrums(dft_A, dft_B, dft_A, 0, true);
49+
cv::idft(dft_A, dft_A, cv::DFT_SCALE, A.rows + B.rows - 1);
4750

48-
cv::Mat corr = dft_A( cv::Rect(0, 0, A.cols + B.cols - 1, A.rows + B.rows - 1) );
49-
cv::normalize( corr, corr, 0, 1, cv::NORM_MINMAX, corr.type() );
50-
cv::pow( corr, 3., corr );
51+
cv::Mat corr = dft_A(cv::Rect(0, 0, A.cols + B.cols - 1, A.rows + B.rows - 1));
52+
cv::normalize(corr, corr, 0, 1, cv::NORM_MINMAX, corr.type());
53+
cv::pow(corr, 3.0, corr);
5154

52-
B ^= cv::Scalar::all( 255 );
55+
B ^= cv::Scalar::all(255);
5356

54-
cv::imshow( "Image", A );
55-
cv::imshow( "ROI", B );
57+
cv::imshow("Image", A);
58+
cv::imshow("ROI", B);
5659

57-
cv::imshow( "Correlation", corr );
58-
cv::waitKey();
60+
cv::imshow("Correlation", corr);
61+
cv::waitKey();
5962

60-
return 0;
63+
return 0;
6164
}

example_12-02.cpp

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,54 @@
11
// Example 12-2. Using cv::HoughCircles() to return a sequence of circles found in a
22
// grayscale image
33

4-
#include <opencv2/opencv.hpp>
5-
#include <iostream>
64
#include <math.h>
75

8-
using namespace cv;
9-
using namespace std;
10-
11-
int main(int argc, char** argv) {
12-
13-
if(argc != 2) {
14-
cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the computation of convolutions"
15-
<< "\nHough Circle detect\nUsage: " <<argv[0] <<" <path/imagename>\n" << endl;
16-
return -1;
17-
}
18-
19-
cv::Mat src, image;
20-
21-
src = cv::imread( argv[1], 1 );
22-
if( src.empty() ) { cout << "Cannot load " << argv[1] << endl; return -1; }
23-
24-
cv::cvtColor(src, image, cv::COLOR_BGR2GRAY);
25-
cv::GaussianBlur(image, image, Size(5,5), 0, 0);
6+
#include <iostream>
7+
#include <vector>
268

27-
vector<cv::Vec3f> circles;
28-
cv::HoughCircles(image, circles, cv::HOUGH_GRADIENT, 2, image.cols/4);
9+
#include <opencv2/opencv.hpp>
2910

30-
for( size_t i = 0; i < circles.size(); ++i ) {
31-
cv::circle(
32-
src,
33-
cv::Point(cvRound(circles[i][0]), cvRound(circles[i][1])),
34-
cvRound(circles[i][2]),
35-
cv::Scalar(0,0,255),
36-
2,
37-
cv::LINE_AA
38-
);
39-
}
11+
using std::cout;
12+
using std::endl;
13+
using std::vector;
4014

41-
cv::imshow( "Hough Circles", src);
42-
cv::waitKey(0);
15+
void help(char** argv) {
16+
cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the computation of convolutions"
17+
<< "\nHough Circle detect\nUsage: " << argv[0] <<" <path/imagename>\n"
18+
<< "Example:\n" << argv[0] << " ../stuff.jpg\n" << endl;
19+
}
4320

44-
return 0;
21+
int main(int argc, char** argv) {
22+
help(argv);
23+
if (argc != 2) {
24+
return -1;
25+
}
26+
27+
cv::Mat src, image;
28+
29+
src = cv::imread(argv[1], 1);
30+
if (src.empty()) {
31+
cout << "Cannot load " << argv[1] << endl;
32+
return -1;
33+
}
34+
35+
cv::cvtColor(src, image, cv::COLOR_BGR2GRAY);
36+
cv::GaussianBlur(image, image, cv::Size(5, 5), 0, 0);
37+
38+
vector<cv::Vec3f> circles;
39+
cv::HoughCircles(image, circles, cv::HOUGH_GRADIENT, 2, image.cols/4);
40+
41+
for (size_t i = 0; i < circles.size(); ++i) {
42+
cv::circle(src,
43+
cv::Point(cvRound(circles[i][0]), cvRound(circles[i][1])),
44+
cvRound(circles[i][2]),
45+
cv::Scalar(0, 0, 255),
46+
2,
47+
cv::LINE_AA);
48+
}
49+
50+
cv::imshow("Hough Circles", src);
51+
cv::waitKey(0);
52+
53+
return 0;
4554
}

example_12-03.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Example 12-3. Using GrabCut for background removal
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <opencv2/opencv.hpp>
6+
7+
using std::cout;
8+
using std::cerr;
9+
using std::endl;
10+
11+
cv::Mat img_preview;
12+
cv::Mat img;
13+
cv::Mat markers;
14+
cv::Mat drawRect;
15+
int x_0 = -1;
16+
int y_0 = -1;
17+
int x_1, y_1;
18+
int drawr = 0;
19+
20+
bool finished;
21+
22+
void displayResult() {
23+
int rows = img.rows;
24+
int cols = img.cols;
25+
cv::Vec3b blackClr(0, 0, 0);
26+
for (int i = 0; i < rows; ++i) {
27+
for (int j = 0; j < cols; ++j) {
28+
if (markers.at<uchar>(i, j) != cv::GC_FGD && markers.at<uchar>(i, j) != cv::GC_PR_FGD) {
29+
img.at<cv::Vec3b>(i, j) = blackClr;
30+
}
31+
}
32+
}
33+
cv::imshow("image", img);
34+
finished = true;
35+
}
36+
37+
static void onMouseClick(int event, int x, int y, int, void*) {
38+
if (finished) {
39+
return;
40+
}
41+
42+
if (event == cv::EVENT_LBUTTONDOWN && drawr == 0) {
43+
if(x_0 < 0) {
44+
x_0 = x;
45+
y_0 = y;
46+
cv::ellipse(markers, cv::Point(x, y), cv::Size(1, 1),
47+
0, 0, 360, cv::GC_FGD, 3);
48+
cv::ellipse(drawRect, cv::Point(x, y), cv::Size(1, 1),
49+
0, 0, 360, cv::Scalar(0, 0, 255), 3);
50+
drawr = 1;
51+
}
52+
53+
cv::addWeighted(img,0.7,drawRect,0.3, 0, img_preview);
54+
55+
cv::imshow("image", img_preview);
56+
return;
57+
}
58+
if( event == cv::EVENT_LBUTTONUP) {
59+
drawr = 2;
60+
}
61+
if(drawr == 1) { //Just moving
62+
drawRect.setTo(0);
63+
cv::rectangle(drawRect, cv::Point(x_0,y_0), cv::Point(x,y), cv::Scalar(0,0,255), -1);
64+
65+
cv::addWeighted(img,0.7,drawRect,0.3, 0, img_preview);
66+
x_1 = x; y_1 = y;
67+
cv::imshow("image", img_preview);
68+
return;
69+
}
70+
71+
if (drawr == 2) {
72+
cv::Mat bg;
73+
cv::Mat fg;
74+
cv::rectangle(markers, cv::Point(x_0,y_0), cv::Point(x_1,y_1), cv::GC_PR_FGD, -1);
75+
cv::grabCut(img, markers, cv::Rect(0, 0, img.cols - 1, img.rows - 1),
76+
bg, fg, 5, cv::GC_EVAL);
77+
displayResult();
78+
return;
79+
}
80+
}
81+
82+
void help(char** argv) {
83+
cout << "\nExample 12-3. Using GrabCut for background removal"
84+
<< "\n- Use left mouse to drag a rectangle over the object"
85+
<< "\n- On release of left mouse button, we will perform GrabCut"
86+
<< "\n- Press any key to terminate program"
87+
<< "\nUsage: "
88+
<< argv[0] << " <path/imagename>\n"
89+
<< "\nExample:\n" << argv[0] << " ../stuff.jpg\n" << endl;
90+
}
91+
92+
93+
int main(int argc, char** argv) {
94+
help(argv);
95+
if (argc != 2) {
96+
return -1;
97+
}
98+
99+
img = cv::imread(std::string(argv[1]), CV_LOAD_IMAGE_COLOR);
100+
if (img.channels() != 3) {
101+
cerr << "Input image should have 3 channels" << endl;
102+
exit(1);
103+
}
104+
105+
markers = cv::Mat(img.size(), CV_8UC1);
106+
markers.setTo(cv::GC_PR_BGD);
107+
108+
img_preview = img.clone();
109+
drawRect = img.clone();
110+
111+
finished = false;
112+
113+
cv::namedWindow("image", cv::WINDOW_AUTOSIZE);
114+
cv::setMouseCallback("image", onMouseClick, 0);
115+
116+
cv::imshow("image", img_preview);
117+
118+
cv::waitKey(0);
119+
120+
return 0;
121+
}

0 commit comments

Comments
 (0)