Skip to content

Commit

Permalink
Modified to work better
Browse files Browse the repository at this point in the history
  • Loading branch information
garybradski committed Aug 4, 2017
1 parent 31f6e11 commit 4d83995
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
9 changes: 7 additions & 2 deletions example_12-02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ using std::cout;
using std::endl;
using std::vector;

void help(char** argv) {
cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the computation of convolutions"
<< "\nHough Circle detect\nUsage: " << argv[0] <<" <path/imagename>\n"
<< "Example:\n" << argv[0] << " ../stuff.jpg\n" << endl;
}

int main(int argc, char** argv) {
help(argv);
if (argc != 2) {
cout << "\nExample 12-1. Using cv::dft() and cv::idft() to accelerate the computation of convolutions"
<< "\nHough Circle detect\nUsage: " << argv[0] <<" <path/imagename>\n" << endl;
return -1;
}

Expand Down
53 changes: 43 additions & 10 deletions example_12-03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ using std::endl;
cv::Mat img_preview;
cv::Mat img;
cv::Mat markers;
cv::Mat drawRect;
int x_0 = -1;
int y_0 = -1;
int x_1, y_1;
int drawr = 0;

bool finished;

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

if (event == cv::EVENT_LBUTTONDOWN) {
cv::ellipse(markers, cv::Point(x, y), cv::Size(1, 1),
if (event == cv::EVENT_LBUTTONDOWN && drawr == 0) {
if(x_0 < 0) {
x_0 = x;
y_0 = y;
cv::ellipse(markers, cv::Point(x, y), cv::Size(1, 1),
0, 0, 360, cv::GC_FGD, 3);
cv::ellipse(img_preview, cv::Point(x, y), cv::Size(1, 1),
cv::ellipse(drawRect, cv::Point(x, y), cv::Size(1, 1),
0, 0, 360, cv::Scalar(0, 0, 255), 3);
drawr = 1;
}

cv::addWeighted(img,0.7,drawRect,0.3, 0, img_preview);

cv::imshow("image", img_preview);
return;
}

if (event == cv::EVENT_RBUTTONDOWN) {
if( event == cv::EVENT_LBUTTONUP) {
drawr = 2;
}
if(drawr == 1) { //Just moving
drawRect.setTo(0);
cv::rectangle(drawRect, cv::Point(x_0,y_0), cv::Point(x,y), cv::Scalar(0,0,255), -1);

cv::addWeighted(img,0.7,drawRect,0.3, 0, img_preview);
x_1 = x; y_1 = y;
cv::imshow("image", img_preview);
return;
}

if (drawr == 2) {
cv::Mat bg;
cv::Mat fg;
cv::rectangle(markers, cv::Point(x_0,y_0), cv::Point(x_1,y_1), cv::GC_PR_FGD, -1);
cv::grabCut(img, markers, cv::Rect(0, 0, img.cols - 1, img.rows - 1),
bg, fg, 5, cv::GC_EVAL);
displayResult();
return;
}
}

int main(int argc, char** argv) {
if (argc != 2) {
void help(char** argv) {
cout << "\nExample 12-3. Using GrabCut for background removal"
<< "\n- Use left click on the image to select foreground point"
<< "\n- Use right clock on the image to perform GrabCut"
<< "\n- Use left mouse to drag a rectangle over the object"
<< "\n- On release of left mouse button, we will perform GrabCut"
<< "\n- Press any key to terminate program"
<< "\nUsage: "
<< argv[0] << " <path/imagename>\n" << endl;
<< argv[0] << " <path/imagename>\n"
<< "\nExample:\n" << argv[0] << " ../stuff.jpg\n" << endl;
}


int main(int argc, char** argv) {
help(argv);
if (argc != 2) {
return -1;
}

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

img_preview = img.clone();
drawRect = img.clone();

finished = false;

Expand Down
15 changes: 10 additions & 5 deletions example_12-04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,20 @@ static void onMouseClick(int event, int x, int y, int, void*) {
}
}

int main(int argc, char** argv) {
if (argc != 2) {
cout << "\nExample 12-4. Using Watershed for image segmentation"
void help(char** argv) {
cout << "\nExample 12-4. Using Watershed for image segmentation"
<< "\n- Use left click on the image to place marker for the new segment"
<< "\n- Use right clock on the image to perform Watershed"
<< "\n- Press any key to terminate program"
<< "\nUsage: "
<< argv[0] << " <path/imagename>\n" << endl;
return -1;
<< argv[0] << " <path/imagename>\n"
<< "\nExample:\n" << argv[0] << " ../stuff.jpg\n" << endl;
}

int main(int argc, char** argv) {
help(argv);
if (argc != 2) {
return -1;
}

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

0 comments on commit 4d83995

Please sign in to comment.