# Doubt in Bounding Rectangle Algorithm

**URL:** <https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495>\
**Category:** Uncategorized\
**Tags:** programming\
**Created:** [September 10, 2023, 4:46pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495 "2023-09-10T16:46:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 10, 2023, 4:46pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/1 "2023-09-10T16:46:30Z")

</div>

Hello everyone,  
I am currently working on issue [#24217](https://github.com/opencv/opencv/issues/24217).  
I reproduced the issue on my local device using Windows 10 OS.  
I referred the following docs in order to understand the boundingRect function:

- [OpenCV: Structural Analysis and Shape Descriptors](https://docs.opencv.org/4.x/d3/dc0/group __imgproc__ shape.html#ga103fcbda2f540f3ef1c042d6a9b35ac7)
- 

[https://docs.opencv.org/3.1.0/dd/d49/tutorial\_py\_contour\_features.html](https://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html)

And [this documentation](https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_bindings/py_bindings_basics/py_bindings_basics.html) to understand how python bindings and concepts of OOPs work in opencv.

Finally considering that the actual source code for bounding Rectangle function is present in [test\_boundingrect.cpp file](https://github.com/opencv/opencv/blob/1a8d37d19e236036310b055e6baf9d00cac839c1/modules/imgproc/test/test_boundingrect.cpp#L51), I am not understanding that in src vector are random points being pushed in generate\_src\_points function? If so, why?

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/7b605a09020f422262c1999776788333f33d8c5a.png)

Instead can we just push those points which represent non zero pixels. Basically I was just trying to dry run the algorithm for the below example:  
img = np.array([  
[0, 0, 0, 0, 0],  
[1, 1, 1, 0, 0],  
], np.uint8)  
Hence, just wanted to know what will be the src vector.

Can you provide the algorithm or any resource regarding the same so that I can understand it better. Thank you very much and apologies if the doubt is silly (my first attempt to contribute in open source 😄)

---

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 10, 2023, 5:25pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/3 "2023-09-10T17:25:37Z")

</div>

Okay, Now I am a bit confused so …first where can I find the actual src code?

> [@berak](#):
>
> oh my. `boundingRect()` should be applied to a contour, not an image

but contours are the boundaries right? of the objects in an image so if we apply the bounding rect to the below image

> [@Dhruv\_J](#):
>
> img = np.array([  
> [0, 0, 0, 0, 0],  
> [1, 1, 1, 0, 0],  
> ], np.uint8)

it should work right ?  
Thank you!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 10, 2023, 6:33pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/5 "2023-09-10T18:33:18Z")

</div>

Contributing to OpenCV is not for beginners. You need to be proficient in C++ development. That includes navigating the source code.

You found a _test case_, not the actual implementation.

> [@Dhruv\_J](#):
>
> > [@berak](#):
> >
> > oh my. `boundingRect()` should be applied to a contour, not an image

That is wrong.

Docs clearly state:

> […] or non-zero pixels of gray-scale image

---

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 10, 2023, 7:37pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/6 "2023-09-10T19:37:08Z")

</div>

Thank you for your feedback  
I understand that contributing to OpenCV requires proficiency in C++ development and familiarity with the source code. but I believe that learning and contributing can go hand in hand and “Where there is a will , there is a way”.

Regarding your comment about the test case, I apologize for the oversight. It would be of great help if you could locate me towards the actual implementation or the source code  
Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 12, 2023, 5:36pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/7 "2023-09-12T17:36:02Z")

</div>

> [@crackwitz](#):
>
> > [@Dhruv\_J](#):
> >
> > > [@berak](#):
> > >
> > > oh my. `boundingRect()` should be applied to a contour, not an image

Perhaps I am understanding what berak was saying, there seems to be a problem in detecting the contour… when I used findcontour() and then applied bounding rect() function the output was correct  
for eg for this array  
[[0 0 0]  
[0 0 0]  
[0 0 0]  
[255 255 255]]  
By using boundingrect(img):(0, 3, 1, 1)  
By using  
contours, \_ = cv2.findContours(img,cv2.RETR\_EXTERNAL, cv2.CHAIN\_APPROX\_SIMPLE) and then boundingrect(contours):(0, 3, 3, 1)

So can anyone point out the file in which detection of contours takes place specifically for bounding rect

I even performed dry run on the boundingrect() function present in [modules/core/src/types.cpp](https://github.com/opencv/opencv/blob/6694d87a23886182900cdb49876d98989cc6e16d/modules/core/src/types.cpp) (Hope I am correct this time!) for

```auto
img = np.array([
   [0, 0, 0, 0, 0],
   [1, 1, 1, 0, 0],
], np.uint8)

```

by using findcontours and then boxpoint function to find the coordinates of the rotated rectangle and then dry running the below program:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6acd6b23bfa4bc99a6c553c4e548decc80f31afd.png)

and the dry run output is coming correct that is (0 ,1 ,3 ,1)  
So Can you please provide me with the file or code using which bounding rect finds the contour.

I even saw the scatterplot provided [#24217](https://github.com/opencv/opencv/issues/24217). Can you provide me with more explanation on the same if possible… regarding the pattern …like (odd,even) combination of (w,h) is yeilding wrong output?  
Thanks

---

<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:** [September 12, 2023, 5:51pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/8 "2023-09-12T17:51:50Z")

</div>

sorry, but it’s still the wrong code.

> <https://github.com/opencv/opencv/blob/4.x/modules/imgproc/src/shapedescr.cpp#L1137>

---

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 13, 2023, 6:49pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/9 "2023-09-13T18:49:44Z")

</div>

Thank you very much for the help  
In the [maskboundingRect](https://github.com/opencv/opencv/blob/4.x/modules/imgproc/src/shapedescr.cpp#L1042) function is 4 byte alignment of the pointer necessary??

```auto
const uchar* _ptr = img.ptr(i);
const uchar* ptr = (const uchar*)alignPtr(_ptr, 4);

```

Or we can directly go with

```auto
cv::Rect maskBoundingRect(const cv::Mat& img) {
    CV_Assert(img.depth() == CV_8U && img.channels() == 1);

    cv::Size size = img.size();
    int xmin = size.width, ymin = size.height, xmax = -1, ymax = -1;
    bool flag = false;

    for (int i = 0; i < size.height; i++) {
        for (int j = 0; j < size.width; j++) {
            if (img.at<uchar>(i, j) != 0) {
                flag = true;
                xmin = std::min(xmin, j);
                xmax = std::max(xmax, j);
                ymin = std::min(ymin, i);
                ymax = std::max(ymax, i);
            }
        }
    }

    if (!flag) {
        xmin = ymin = xmax = ymax = 0;
    }

    return cv::Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
}

```

I even referred chatgpt for the same in order to know about the error : It stated that the error was due to the 4 byte alignment ? So, Can I go ahead with the above code?

---

<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:** [September 14, 2023, 6:11am UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/11 "2023-09-14T06:11:54Z")

</div>

> [@Dhruv\_J](#):
>
> I even referred chatgpt for the same in order to know about the error : It stated that the error was due to the 4 byte alignment

oh my. what did it say (about what ?), exactly ?  
(and why would you believe that ?)

your (far too much !) simplified mockup code does not need or would even profit from ptr alignment, but now, go find out why this line is so:

> <https://github.com/opencv/opencv/blob/4790a3732e725b102f6c27858e7b43d78aee2c3e/modules/imgproc/src/shapedescr.cpp#L1075>

---

<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:** [September 15, 2023, 7:26am UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/12 "2023-09-15T07:26:27Z")

</div>

i cannot repeat any problem from the issue using c++:

```auto

for (int h :{3,4,5,6,7,8,9}) {
    for (int w :{5,6,7,8,9,10}) {
       Mat m(h,w,CV_8U,Scalar(0));
       m(Rect(w-3,h-1,3,1)) = 1;
       //cout << m << endl;
       Rect r = maskBoundingRect(m); // see post above
       Rect r2 = boundingRect(m);
       cout << (r==r2) << " " << r << r2 << endl;
    }
}

```

---

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 15, 2023, 6:37pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/13 "2023-09-15T18:37:35Z")

</div>

Hello!

> [@berak](#):
>
> oh my. what did it say (about what ?), exactly ?  
> (and why would you believe that ?)

I just wanted to know how that how significant and important is it to use memory alignment in the code and hence I referred to chatgpt . I even referred [this](https://web.archive.org/web/20201021053824/https://developer.ibm.com/technologies/systems/articles/pa-dalign/) page for understanding the memory alignment concept. but modern CPUs and compilers are designed to handle unaligned data efficiently so why is it so…like will it make a very large difference?

> [@berak](#):
>
> i cannot repeat any problem from the issue using c++:
> 
> ```auto
> 
> ```

Secondly, When I tested bounding rect in c++

```auto
    uint8_t img[2][5] = {
        {0, 0, 0, 0, 0},
        {1, 1, 1, 0, 0}
    };
    Mat matImg(2, 5, CV_8U, img);

    Rect r = boundingRect(matImg);
    cout << r << endl;

```

the output was

```auto
[1 x 1 from (0, 1)]

```

Am I going wrong somewhere?

Thanks 😄

---

<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:** [September 16, 2023, 6:06am UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/14 "2023-09-16T06:06:55Z")

</div>

i’m still getting

```
[3 x 1 from (0, 1)]

```

from your experiment above …

were you able to reproduce the other problem cases (from c++) ?

however, i _can_ reproduce the python issues, on the same box, same local c++/py build

but, hmm, i cannot find any usage of this code path in the library, the resp. unit test handles only points, not pixels

---

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 16, 2023, 2:32pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/16 "2023-09-16T14:32:05Z")

</div>

Hello!  
I had reproduced these issues in the test folder using visual studio 2022 which was made during the end of the build.

for this example

> [@Dhruv\_J](#):
>
> Secondly, When I tested bounding rect in c++
> 
> ```auto
> 
> ```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/612ba1099de5b7179a3ed6c268d96ef4067a28e2.png)

> [@berak](#):
>
> were you able to reproduce the other problem cases (from c++) ?

I tried the code given originally in the [issue](https://github.com/opencv/opencv/issues/24217):

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/53564da9869b4e043fedc24cc34993e42d920cd5.png)

I think that I have implemented the code in a wrong place  
Ideally where should have I implemented the code ??

Sorry for late reply

---

<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:** [September 16, 2023, 3:01pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/17 "2023-09-16T15:01:56Z")

</div>

please post text, not useless screenshots of it

---

<div class="post-metadata">

**Author:** ![Dhruv\_J](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dhruv_j/32/8602_2.png) [@Dhruv\_J](https://forum.opencv.org/u/Dhruv_J)\
**Post date:** [September 16, 2023, 3:10pm UTC](https://forum.opencv.org/t/doubt-in-bounding-rectangle-algorithm/14495/18 "2023-09-16T15:10:05Z")

</div>

code:

```auto
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main() {
    for (int img_w = 3; img_w < 20; img_w++) {
        for (int img_h = 1; img_h < 15; img_h++) {
            Mat img(img_h, img_w, CV_8U, Scalar(0));
            img.row(img_h - 1)(Rect(0, 0, 3, 1)) = 255;

            Rect boundingRect = cv::boundingRect(img);

            if (boundingRect.width != 3) {
                cout << img_w << "x" << img_h << ": " << "w=" << boundingRect.width << endl;
            }
        }
    }

    return 0;
}

```

Output:

```auto
3x4: w=1
3x8: w=1
3x12: w=1
5x2: w=1
5x6: w=1
5x10: w=1
5x14: w=1
7x4: w=1
7x8: w=1
7x12: w=1
9x2: w=1
9x6: w=1
9x10: w=1
9x14: w=1
11x4: w=1
11x8: w=1
11x12: w=1
13x2: w=1
13x6: w=1
13x10: w=1
13x14: w=1
15x4: w=1
15x8: w=1
15x12: w=1
17x2: w=1
17x6: w=1
17x10: w=1
17x14: w=1
19x4: w=1
19x8: w=1
19x12: w=1

C:\Users\dhruv\source\repos\OpenCVtest\x64\Release\OpenCVtest.exe (process 18220) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.

```

‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Users\dhruv\source\repos\OpenCVtest\x64\Release\OpenCVtest.exe’. Symbols loaded.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\ntdll.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\kernel32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\KernelBase.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\ucrtbase.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\opencv\Builds\install\x64\vc17\bin\opencv\_world480.dll’. Symbols loaded.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\gdi32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\win32u.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\gdi32full.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\msvcp\_win.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\user32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\ole32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\rpcrt4.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\combase.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\ws2\_32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\oleaut32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\comdlg32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\msvcrt.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\SHCore.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\shlwapi.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\shell32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\advapi32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\sechost.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\msvcp140.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\vcruntime140\_1.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\vcruntime140.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\WinSxS\amd64\_microsoft.windows.common-controls\_6595b64144ccf1df\_5.82.19041.1110\_none\_792d1c772443f647\comctl32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\mfplat.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\cfgmgr32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\mf.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\mfreadwrite.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\dxgi.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\d3d11.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\concrt140.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\mfcore.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\crypt32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\bcrypt.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\powrprof.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\ksuser.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\kernel.appcore.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\cryptbase.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\imm32.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\bcryptprimitives.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\RTWorkQ.dll’.  
‘OpenCVtest.exe’ (Win32): Loaded ‘C:\Windows\System32\umpdc.dll’.  
The thread 0x45c8 has exited with code 0 (0x0).  
The thread 0x476c has exited with code 0 (0x0).  
The thread 0x4f8 has exited with code 0 (0x0).  
The program ‘[18384] OpenCVtest.exe’ has exited with code 0 (0x0).
