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..
完美,非常感謝! –