I have this code to move around the image when dragging but when I reach one of the image limits it returns the following error
Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:GHA-OCV-1_workci-gha-workflowci-gha-workflowopencvmodulescoresrc matrix.cpp, line 809
Mat originalImage;
Mat currentImage;
bool isDragging = false;
Point startPoint;
Point lastMousePos;
Rect viewportRect(0, 0, 1000, 500);
void updateImage() {
Mat tempImage = originalImage.clone();
Mat viewport = tempImage(viewportRect);
currentImage = viewport.clone();
imshow("Drawing", currentImage);
}
static void drawCallback(int event, int x, int y, int flags, void* param) {
switch (event) {
case EVENT_LBUTTONDOWN:
if (flags & EVENT_FLAG_SHIFTKEY) {
isDragging = true;
startPoint = Point(x, y);
lastMousePos = startPoint;
}
break;
case EVENT_MOUSEMOVE:
if (isDragging) {
int dx = x - lastMousePos.x;
int dy = y - lastMousePos.y;
viewportRect.x -= dx;
viewportRect.y -= dy;
lastMousePos = Point(x, y);
updateImage();
}
break;
case EVENT_LBUTTONUP:
isDragging = false;
break;
}
}
int main()
{
string imagePath = "C:/Users/Yuri/Pictures/space.jpg";
originalImage = imread(imagePath, IMREAD_COLOR);
currentImage = originalImage.clone();
namedWindow("Drawing");
updateImage();
setMouseCallback("Drawing", drawCallback, nullptr);
while (1) {
imshow("Drawing", currentImage);
int key = waitKey(0);
if (key == 27)
break;
}
destroyAllWindows();
return 0;
}
*I’m using currentImage
because it will be useful later
I imagine there is a problem with showing something outside the limits of the image since there is nothing there, but I have no idea how to fix it.