Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OpenCV cv2 drawing rectangle with text

I draw a rectangle on my image using

cv2.rectangle(frame,(x,y),(x1,y1),(0,255,0),2) 

I would like to draw rectangles with text information on them. How do I do it? Are there any ready to use implementations available? Or should I match the top left coordinate of the rectangle and try to display a different cv2 text element with the cv2 rect element?

Can you direct me to any code implementation/workaround?

P.S: I don't want to use the object_detection. visualisation utils available with tf.

enter image description here

like image 548
55597 Avatar asked May 13 '19 08:05

55597


People also ask

How do I put text on a rectangle in OpenCV?

You can use cv2. putText() to overlay text information on top of a rectangle. For example, you can grab the contour coordinates, draw a rectangle, and put text on top of it by shifting it upwards.

How do you make a rectangle in cv2?

You can use cv2. rectangle() : cv2. rectangle(img, pt1, pt2, color, thickness, lineType, shift) Draws a simple, thick, or filled up-right rectangle.


1 Answers

You can use cv2.putText() to overlay text information on top of a rectangle. For example, you can grab the contour coordinates, draw a rectangle, and put text on top of it by shifting it upwards.

x,y,w,h = cv2.boundingRect(contour) image = cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 1) cv2.putText(image, 'Fedex', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2) 

You will get something like this

enter image description here

like image 115
nathancy Avatar answered Sep 19 '22 07:09

nathancy