opencv: cv2.rectangle() TypeError: an integer is required (got type tuple)
System information (version)
- OpenCV => 4.0.1.25
- Operating System / Platform => Windows 64 bit
- Compiler => MSC v.1900 64 bit (AMD64)
Detailed description
Traceback (most recent call last):
File "thresh.py", line 24, in <module>
cropped_image = region_of_interest(thresh_img)
File "thresh.py", line 13, in region_of_interest
cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
TypeError: an integer is required (got type tuple)
Steps to reproduce
The error arises in the rectangle() function.
Here is the small script in which i am trying to use it.
import numpy as np
import cv2
def thresh(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, th = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
return th
def region_of_interest(image):
height = image.shape[0]
mask = np.zeros_like(image, np.uint8)
req_height = 0.4*height
cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
masked_image = cv2.bitwise_and(image, mask)
f_image = cv2.bitwise_not(masked_image)
return f_image
image = cv2.imread('image2.jpg')
copy_img = np.copy(image)
thresh_img = thresh(copy_img)
cropped_image = region_of_interest(thresh_img)
cv2.imshow('image', cropped_image)
cv2.waitKey(0)
Error occuring on this line:-
cv2.rectangle(mask, (0, req_height), (image.shape[1], height), (0, 255, 0), 5, 2)
I have tried passing in one argument after the color attribute. I also have tried naming the attributes i.e:- —> color=(0,255,0), thickness=5 etc.
Let me know if I have missed out any information which should have been given.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 17 (1 by maintainers)
Commits related to this issue
- Fix: OpenCV 4.0.1 rectangle arguments https://github.com/opencv/opencv/issues/14866 — committed to Winand/hsv_color_picker by Winand 3 years ago
As of now explicitly marking the arguments as int makes the code work.
cv2.rectangle(mask, (0, int(req_height)), (int(image.shape[1]), int(height)), (0, 255, 0), 5)
Sorry about the hassle.
The error message is completely wrong as always. I think, the cause of this error may be the case, when the image array is a view into the actual data, instead of a plain array and OpenCV apparently needs the image to be
C_CONTIGUOUS
.I would however recommend, that you use
np.ascontiguousarray(image)
instead ofcv2.UMat(image).get()
to avoid unneeded copies.This solution did not work for me. However, I did find one that does. I converted my numpy array to a cv2.Umat object and then used it’s get() method to get it back as a numpy array then saved the it back to the original variable. Somehow this cleaned(?) the data and made rectangle() work again.
image = cv2.UMat(image).get()
image = np.array(image) before the function call worked for me
cv2.rectangle() is full of stupid questions. Sometimes it convert img and return nothing, sometimes it don’t convert and return a Umat which I need to convert to numpy again. Notice that this happened in just one file and the same version of cv2!
谢谢你们我用 image = np.ascontiguousarray(image) 解决了
Thanks for the solution. You saved me after few days…
I also noticed that input type may cause some differences, but I didn’t figure it out since it is so weird. In my case I just used regular cv2 and numpy. These problems wasted my precious time which should be spent with beautiful girls.
I resolve it by add .copy(). ex: cv2.rectangle(rgb_float.copy(), (int(focus_box[‘x1’]), int(focus_box[‘y1’])), (int(focus_box[‘x2’]), int(focus_box[‘y2’])), (0, 0, 255))
I had the same problems. Make sure you also check how you load the image. If you didn’t use CV2 but for example skimage.io.imread, the above described problems will likely happen 😉