Maham
Maham

Reputation: 442

OpenCV cv2.rectangle output binary image

I have been trying to draw rectangle on a black image, uscv2.rectangle.Here is my code : (It is just a sample, in actual code there is a loop i.e values x2,y2,w2,h2 changes in a loop)

  heir = np.zeros((np.shape(image1)[0],np.shape(image1)[1]),np.uint8);
  cv2.rectangle(heir,(x2,y2),(x2+w2,y2+h2),(255,255,0),5)
  cv2.imshow("img",heir);
  cv2.waitKey()

It is giving the following output: output image : heir Why the image is like that? Why the boundaries are not just a line a width 5. I have tried, but I am not able to figure it out.

Upvotes: 1

Views: 4657

Answers (2)

Bade
Bade

Reputation: 747

Because you are loading the image to be tagged (draw rectangles) in grayscale, thats why when you are adding rectangles/bounding boxes the colors are being converted to grayscale.

To fix the issue, open image in "color" format. Since, you didn't included that part of code, here is the proposed solution:

tag_img     = cv2.imread(MYIMAGE,1)

Pay attention to the second parameter here, which is "1" and means load image as color. Read more about reading images here: https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_image_display/py_image_display.html

Upvotes: 0

Can't post this in a comment, but it's a negative answer: the same operations work for me on Windows/python 2.7.8/opencv3.1

import numpy as np
import cv2

heir = np.zeros((100,200),np.uint8);
x2=10
y2=20
w2=30
h2=40
cv2.rectangle(heir,(x2,y2),(x2+w2,y2+h2),(255,255,0),5)
cv2.imshow("img",heir);
cv2.waitKey()

enter image description here

Upvotes: 2

Related Questions