2013-10-21 59 views
1

我想在一個特定的地方在OpenCV中繪製一個盒子,以便我可以在那裏裁剪它。現在我正試圖圍繞我感興趣的區域繪製一個回放,但是我在mouseEvent()中的drawBox()中出現錯誤。以下是我的代碼,以下是錯誤輸出。我怎樣才能使這個工作?我在看的地方都有舊的IplImage和其他不贊成使用的功能。用我的鼠標在OpenCV中繪製一個盒子(使用Mat not IplImage)

#include stuff 

using namespace cv; 

bool isDrawing = false; 
Point start, end; 

void drawBox(Point start, Point end, Mat& img){ 
Scalar color = (0,255,0); 
rectangle(img, start, end, color, 1, 8, 0); 
return; 
} 

void mouseEvent(int evt, int x, int y, int flags, void* param){ 
if(isDrawing){ 
    if(evt==CV_EVENT_LBUTTONUP){ 
     printf("up %d %d\n",x,y); 
     isDrawing = false; 
     end.x = x; 
     end.y = y; 
     drawBox(start, end, (Mat&) param); 
     return; 
    } 
} 
else{ 
    if(evt==CV_EVENT_LBUTTONDOWN){ 
     printf("down %d %d\n",x,y); 
     isDrawing = true; 
     start.x = x; 
     start.y = y; 
     return; 
    } 
} 
} 

int main(){ 

Mat feed = imread("C:/Users/Timo/Desktop/image1.jpg", CV_LOAD_IMAGE_GRAYSCALE); 

namedWindow("Feed"); 
imshow("Feed", feed); 

cvSetMouseCallback("Feed", mouseEvent, &feed); 

waitKey(0); 

return 1; 
} 

CONSOLE

down 293 26 
up 520 217 
OpenCV Error: Assertion failed <cn <= 4> in unknown function, file ..\..\..\src\opencv\modules\core\src\matrix.cpp, line 845 

POPUP

Unhandled exception at 0x80000000 in opencv_project.exe:)xC0000005: Access violation. 

輸出日誌

First-chance exception at 0x7796c41f in opencv_project.exe: Microsoft C++ exception: cv::Exception at memory location 0x002de9b4.. 
Unhandled exception at 0x7796c41f in opencv_project.exe: Microsoft C++ exception: cv::Exception at memory location 0x002de9b4.. 

回答

1

的問題是要傳遞一個指針cvSetMouseCallback(),然後試圖把它看成一個參考。

您可以通過更換線

drawBox(start, end, (Mat&) param); 

cv::Mat* image = static_cast<cv::Mat *>(param); 
drawBox(start, end, *image); 

而且解決這個問題,真正看到你畫你需要再次調用imshow()盒子。即:

rectangle(img, start, end, color, 1, 8, 0); 
imshow("Feed", img); 
+0

完美,非常感謝! –