#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "stdio.h"
#include <iostream>
using namespace std;
using namespace cv;
string filename;
char filename_tmp[10] = "tmp.jpg";
Mat image;
string winName = "show";
enum{NOT_SET = 0, IN_PROCESS = 1, SET = 2};
enum{LS_LEFT = 0, LS_RIGHT = 1, LS_NONE = 2};
uchar rectState, mouse_flag;
Rect rect;
Mat mask;
const Scalar GREEN = Scalar(0,255,0);
const Scalar RED = Scalar(0,0,255);
const Scalar BLUE = Scalar(255,0,0);
Mat bgdModel, fgdModel;
int line_count[4];
const int BGD_KEY = CV_EVENT_FLAG_CTRLKEY;
const int FGD_KEY = CV_EVENT_FLAG_SHIFTKEY;
int i, j;
void setRectInMask(){
rect.x = max(0, rect.x);
rect.y = max(0, rect.y);
rect.width = min(rect.width, image.cols-rect.x);
rect.height = min(rect.height, image.rows-rect.y);
}
static void getBinMask( const Mat& comMask, Mat& binMask ){
binMask.create( comMask.size(), CV_8UC1 );
binMask = comMask & 1;
}
void on_mouse( int event, int x, int y, int flags, void* )
{
switch( event ){
case CV_EVENT_LBUTTONDOWN:
mouse_flag = LS_LEFT;
if( rectState == NOT_SET){
rectState = IN_PROCESS;
rect = Rect( x, y, 1, 1 );
}
break;
case CV_EVENT_LBUTTONUP:
if( rectState == IN_PROCESS ){
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
rectState = SET;
(mask(rect)).setTo( Scalar(GC_PR_FGD));
}
break;
case CV_EVENT_RBUTTONDOWN:
mouse_flag = LS_RIGHT;
line_count[0] = x;
line_count[1] = y;
break;
case CV_EVENT_RBUTTONUP:
mouse_flag = LS_NONE;
line_count[0] = 0;
line_count[1] = 0;
line_count[2] = 0;
line_count[3] = 0;
imwrite(filename_tmp,image);
break;
case CV_EVENT_MOUSEMOVE:
if(mouse_flag == LS_LEFT){
if( rectState == IN_PROCESS ){
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
image = imread(filename_tmp, 1 );
rectangle(image, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
imshow(winName, image);
}
}else if(mouse_flag == LS_RIGHT){
IplImage pI = image;
IplImage pI_2 = mask;
line_count[2] = x;
line_count[3] = y;
if((flags & BGD_KEY) != 0){
cvLine(&pI, Point(line_count[0], line_count[1]), Point(line_count[2], line_count[3]), RED, 5);
cvLine(&pI_2, Point(line_count[0], line_count[1]), Point(line_count[2], line_count[3]), cvScalar(0,0,0), 5);
}else if((flags & FGD_KEY) != 0){
cvLine(&pI, Point(line_count[0], line_count[1]), Point(line_count[2], line_count[3]), BLUE, 5);
cvLine(&pI_2, Point(line_count[0], line_count[1]), Point(line_count[2], line_count[3]), cvScalar(1,0,0), 5);
}
line_count[0] = x;
line_count[1] = y;
imshow(winName, image);
}
break;
}
}
int main(int argc, char* argv[]){
Mat res;
Mat binMask;
filename = argv[1];
image = imread( filename, 1 );
imshow(winName, image);
imwrite(filename_tmp,image);
mask.create(image.size(), CV_8UC1);
rectState = NOT_SET;
mask.setTo(GC_BGD);
setMouseCallback(winName, on_mouse, 0);
while(1){
int c = waitKey(0);
if(c == '\x1b'){
break;
}else if(c == 'n'){
image = imread(filename, 1 );
grabCut(image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_MASK);
getBinMask( mask, binMask );
image.copyTo(res, binMask );
imshow("11", res);
}
}
return 0;
}