# HoughCircle ROI error

**URL:** <https://forum.opencv.org/t/houghcircle-roi-error/4845>\
**Category:** C++\
**Created:** [August 19, 2021, 6:39pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845 "2021-08-19T18:39:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![100367230](https://avatars.discourse-cdn.com/v4/letter/1/54ee81/32.png) [@100367230](https://forum.opencv.org/u/100367230)\
**Post date:** [August 19, 2021, 6:39pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/1 "2021-08-19T18:39:05Z")

</div>

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

```auto
    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?**

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 19, 2021, 6:41pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/2 "2021-08-19T18:41:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 19, 2021, 7:08pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/4 "2021-08-19T19:08:52Z")

</div>

not helpful.

if there _is_ an error _msg_, we need to see it !

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 19, 2021, 8:38pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/6 "2021-08-19T20:38:01Z")

</div>

oh noes, not _images_ of code anywhere …

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

---

<div class="post-metadata">

**Author:** ![100367230](https://avatars.discourse-cdn.com/v4/letter/1/54ee81/32.png) [@100367230](https://forum.opencv.org/u/100367230)\
**Post date:** [August 20, 2021, 10:44am UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/7 "2021-08-20T10:44:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 20, 2021, 12:11pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/8 "2021-08-20T12:11:17Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 20, 2021, 12:24pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/10 "2021-08-20T12:24:06Z")

</div>

> [@100367230](#):
>
> they can be drawn

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 20, 2021, 12:25pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/11 "2021-08-20T12:25:20Z")

</div>

> [@100367230](#):
>
> ```auto
> Rect box(v3fCircles[i][0] - v3fCircles[i][2], v3fCircles[i][1] - v3fCircles[i][2], 2 * v3fCircles[i][2], 2 * v3fCircles[i][2]);
> 
> ```

again, check values here

and gdamn, learn to use your debugger, mate !

---

<div class="post-metadata">

**Author:** ![100367230](https://avatars.discourse-cdn.com/v4/letter/1/54ee81/32.png) [@100367230](https://forum.opencv.org/u/100367230)\
**Post date:** [August 20, 2021, 12:43pm UTC](https://forum.opencv.org/t/houghcircle-roi-error/4845/12 "2021-08-20T12:43:49Z")

</div>

> [@berak](#):
>
> `Rect valid = box & Rect(0,0,mask.cols,mask.rows); // intersection`

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