HoughCircle ROI error

'm trying to make a roi of detected circles by HoughCircle so I type the following code:

    vector<Vec3f> v3fCircles;
    HoughCircles(region2, v3fCircles, HOUGH_GRADIENT, 2, 120, 70, 35, 40, 45);

    for (int i = 0; i < v3fCircles.size(); i++) {
        cout << v3fCircles[i][0] << "," << v3fCircles[i][1] << endl;

                    
        Rect box(v3fCircles[i][0] - v3fCircles[i][2], v3fCircles[i][1] - v3fCircles[i][2], 2 * v3fCircles[i][2], 2 * v3fCircles[i][2]);
        Mat roi = mask(box);
             
              circle(region2,
                Point(v3fCircles[i][0], v3fCircles[i][1]),
                3,
                Scalar(0, 255, 255),
                CV_FILLED);
            circle(region2,
                Point(v3fCircles[i][0], v3fCircles[i][1]),
                v3fCircles[i][2],
                Scalar(255, 0, 0),
                2);}

Where region2 is another ROI of the original image (right 1/2 of the image where i detect circles)
The console output give me this after i=5

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:\build\3_4_winpack-build-win64-vc14\opencv\modules\core\src\matrix.cpp

Basically it says the rectangle roi is out of image range? what is wrong?

please show error . . . . . . . . …

not helpful.

if there is an error msg, we need to see it !

also, noone can run your incomplete code to reproduce it.

oh noes, not images of code anywhere …

and again, i know, asking a good question is terribly hard,

Yeah it is I’m sorry I think now is better

1 Like

start checking, if region2 is valid, and if box is inside the mask

you could also “clip” a Rect against an image, to make sure it is valid:

Rect box = ...;
Rect valid = box & Rect(0,0,mask.cols,mask.rows); // intersection
Mat roi = mask(valid);
1 Like

you are allowed to draw outside the image !
(but not to access pixels outside, what HoughCircles etc might try)

again, check values here

and gdamn, learn to use your debugger, mate !

That fix my problem!! thank you so much!!

1 Like