Files
jlengrand.github.io/_posts/2011-10-27-opencv-rect-expects-four-integers.markdown
julien lengrand-lambert 8ee4ff838d Highlights Python code
2014-02-09 17:23:00 +01:00

1.5 KiB

layout, status, published, title, author, author_login, author_email, author_url, wordpress_id, wordpress_url, date, categories, tags, comments
layout status published title author author_login author_email author_url wordpress_id wordpress_url date categories tags comments
post publish true OpenCV : 'rect' expects four integers Julien Lengrand-Lambert jlengrand julien@lengrand.fr http://www.lengrand.fr 207 http://www.lengrandlambert.fr/wordpress/?p=207 2011-10-27 09:48:25.000000000 +02:00
programming
OpenCV
Computer Vision
Python
windows
image processing

You may encounter this error when using OpenCV with Python bindings, while trying to use the rectangle structure.

Here is an example :

{% highlight python %} #!/usr/bin/env python

import cv

img = cv.LoadImage("test.jpg") dims = cv.GetSize(img) roi = [0, 0, dims[0] / 2, dims[1] / 2 ] cv.SetImageROI(img, roi) {% endhighlight %}

You will get this result:

{% highlight python %} Traceback (most recent call last): File "newfile.py", line 8, in cv.SetImageROI(img, roi) TypeError: CvRect argument 'rect' expects four integers {% endhighlight %}

The answer is pretty simple, you have to set rect as a tuple and not a list:

{% highlight python %} roi = (0, 0, dims[0] / 2, dims[1] / 2 ) {% endhighlight %}

There it is, pretty simple, isn't?!

I still lost 15 minuts searching for it yesterday ^^, won't do it twice :D

A small warning however, the values of the tuple can't be changed once initialized !

See ya