Python OpenCV cv2 drawing rectangle with text

后端 未结 2 1185
孤城傲影
孤城傲影 2021-02-05 07:22

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 t

相关标签:
2条回答
  • 2021-02-05 07:27

    you may need to extend your code with a function that takes your text as input, position_x, position_y .. and it will measure the size of the letters and dynamically set a rectangle width based on that.

    you can use: cv2.getTextSize(text, font, font_scale, thickness)

    to get how many pixels will it use and then use it to define the rectangle width.

    0 讨论(0)
  • 2021-02-05 07:46

    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

    0 讨论(0)
提交回复
热议问题