Skip to content

Commit 4d83995

Browse files
committed
Modified to work better
1 parent 31f6e11 commit 4d83995

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

example_12-02.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ using std::cout;
1212
using std::endl;
1313
using std::vector;
1414

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+
}
20+
1521
int main(int argc, char** argv) {
22+
help(argv);
1623
if (argc != 2) {
17-
cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the computation of convolutions"
18-
<< "\nHough Circle detect\nUsage: " << argv[0] <<" <path/imagename>\n" << endl;
1924
return -1;
2025
}
2126

example_12-03.cpp

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ using std::endl;
1111
cv::Mat img_preview;
1212
cv::Mat img;
1313
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;
1419

1520
bool finished;
1621

@@ -34,33 +39,60 @@ static void onMouseClick(int event, int x, int y, int, void*) {
3439
return;
3540
}
3641

37-
if (event == cv::EVENT_LBUTTONDOWN) {
38-
cv::ellipse(markers, cv::Point(x, y), cv::Size(1, 1),
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),
3947
0, 0, 360, cv::GC_FGD, 3);
40-
cv::ellipse(img_preview, cv::Point(x, y), cv::Size(1, 1),
48+
cv::ellipse(drawRect, cv::Point(x, y), cv::Size(1, 1),
4149
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+
4255
cv::imshow("image", img_preview);
4356
return;
4457
}
45-
46-
if (event == cv::EVENT_RBUTTONDOWN) {
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) {
4772
cv::Mat bg;
4873
cv::Mat fg;
74+
cv::rectangle(markers, cv::Point(x_0,y_0), cv::Point(x_1,y_1), cv::GC_PR_FGD, -1);
4975
cv::grabCut(img, markers, cv::Rect(0, 0, img.cols - 1, img.rows - 1),
5076
bg, fg, 5, cv::GC_EVAL);
5177
displayResult();
5278
return;
5379
}
5480
}
5581

56-
int main(int argc, char** argv) {
57-
if (argc != 2) {
82+
void help(char** argv) {
5883
cout << "\nExample 12-3. Using GrabCut for background removal"
59-
<< "\n- Use left click on the image to select foreground point"
60-
<< "\n- Use right clock on the image to perform GrabCut"
84+
<< "\n- Use left mouse to drag a rectangle over the object"
85+
<< "\n- On release of left mouse button, we will perform GrabCut"
6186
<< "\n- Press any key to terminate program"
6287
<< "\nUsage: "
63-
<< argv[0] << " <path/imagename>\n" << endl;
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) {
6496
return -1;
6597
}
6698

@@ -74,6 +106,7 @@ int main(int argc, char** argv) {
74106
markers.setTo(cv::GC_PR_BGD);
75107

76108
img_preview = img.clone();
109+
drawRect = img.clone();
77110

78111
finished = false;
79112

example_12-04.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,20 @@ static void onMouseClick(int event, int x, int y, int, void*) {
9090
}
9191
}
9292

93-
int main(int argc, char** argv) {
94-
if (argc != 2) {
95-
cout << "\nExample 12-4. Using Watershed for image segmentation"
93+
void help(char** argv) {
94+
cout << "\nExample 12-4. Using Watershed for image segmentation"
9695
<< "\n- Use left click on the image to place marker for the new segment"
9796
<< "\n- Use right clock on the image to perform Watershed"
9897
<< "\n- Press any key to terminate program"
9998
<< "\nUsage: "
100-
<< argv[0] << " <path/imagename>\n" << endl;
101-
return -1;
99+
<< argv[0] << " <path/imagename>\n"
100+
<< "\nExample:\n" << argv[0] << " ../stuff.jpg\n" << endl;
101+
}
102+
103+
int main(int argc, char** argv) {
104+
help(argv);
105+
if (argc != 2) {
106+
return -1;
102107
}
103108

104109
img = cv::imread(std::string(argv[1]), CV_LOAD_IMAGE_COLOR);

0 commit comments

Comments
 (0)