Assertion failed error with opencv

I'm trying to select a region of an image with opencv but I randomly get this assertion failed error:

OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 &&
(unsigned)y < (unsigned)size.p[0])) in cv::Mat::ptr, file
c:\dependencies\opencv\build\include\opencv2\core\mat.inl.hpp, line 774


Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>

using namespace cv; 
using namespace std; 

enum BUTTON {LEFT};
enum KEYS {SPACE};
bool mButton[1] = { false };
bool key[1] = { false };

void CallBackFunc(int event, int x, int y, int flags, void* userdata);
void thresholdArea(Point, Mat &);
void explore(int, int, int, Mat &, vector<bool> &, Vec3b);

int main(int argc, char **argv) { 
	//Create matrix to store image
	Mat image;
	Point mPos, target;
	int key = 0;

	//initialize capture
	VideoCapture cap;

	//access primary camera
	//cap.open(0);

	//Create window to show
	namedWindow("window", 1);
	
	//load test image
	image = imread("lena.bmp", IMREAD_COLOR);
	do {
		//copy webcam stream to image
		//cap >> image;

		//print image to screen
		imshow("window", image);

		//get mouse events
		setMouseCallback("window", CallBackFunc, &mPos);
		if (mButton[LEFT]) {
			mButton[LEFT] = false;
			target.x = mPos.x;
			target.y = mPos.y;
			thresholdArea(target, image);
			int x = 0;
		}
	
		//reload test image if 'enter' key pressed
		if (key == 13) {
			image = imread("lena.bmp", IMREAD_COLOR);
		}

		key = waitKey(16);
	} while (key != 27);	//press escape to exit

	destroyAllWindows();

	return 0; 
}

void CallBackFunc(int event, int x, int y, int flags, void* userdata) {
	Point *p = (Point*)userdata;
	p->x = x;
	p->y = y;

	if (event == EVENT_LBUTTONDOWN) {
		mButton[LEFT] = true;
	}
}

void thresholdArea(Point target, Mat &image) {
	Vec3b color;
	Vec3b pPix = image.ptr <Vec3b>(target.y)[target.x];
	vector<bool> isSeen(image.cols * image.rows, false);
	
	//color of pixel at mouse click (as 24-bit number)
	int center = pPix[0] << 16 | pPix[1] << 8 | pPix[2] << 0;

	//new color of region 
	color[0] = 255;
	color[1] = 0;
	color[2] = 255;
	
	explore(center, target.x, target.y, image, isSeen, color);
	int i = 0;
}

void explore(int center, int x, int y, Mat &image, vector<bool> &isSeen, Vec3b color) {
	Vec3b pPix = image.ptr <Vec3b>(y)[x];
	
	//color of current pixel (as 24-bit number)
	int pColor = pPix[0] << 16 | pPix[1] << 8 | pPix[2] << 0;

	//exit if out of bounds
	if (x < 0 || x >= image.cols || y < 0 || y >= image.rows)
		return;
	if (isSeen[y * image.cols + x])
		return;

	//flag pixel as seen
	isSeen.at(y * image.cols + x) = true;
	
	//if current pixel color is within + or -10% of center pixel
	if (pColor > center - 0xffffff * .1 && pColor < center + 0xffffff * .1) {
		image.ptr <Vec3b>(y)[x] = color;
	}
	else
		return;

	//explore 8-neighbors
	explore(center, x + 1, y, image, isSeen, color);
	explore(center, x - 1, y, image, isSeen, color);
	explore(center, x, y + 1, image, isSeen, color);
	explore(center, x, y - 1, image, isSeen, color);
	explore(center, x - 1, y + 1, image, isSeen, color);
	explore(center, x - 1, y - 1, image, isSeen, color);
	explore(center, x + 1, y - 1, image, isSeen, color);
	explore(center, x + 1, y + 1, image, isSeen, color);
}


It happens in the explore function when I try to move in the y direction, but it doesn't happen all the time. Sometimes I get it the first time I click on the image, sometimes it happens the third time, and so on.

Any help would be appreciated.
Last edited on
you check for out of bounds on line 98, but already access it on line 92.
Thanks so much.
I can't believe I missed that.
Topic archived. No new replies allowed.