0

I want to draw a box around the word 'Deleted' in the following image using opencv and find the co-ordinates.

enter image description here

I obtained the above image from the following code:

kernel = np.ones((3,3),np.uint8)
dilation = cv2.dilate(img,kernel,iterations =1)
plt.imshow(dilation)

The original image is:

This is the original image

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Emm
  • 71
  • 8
  • 1
    Invert the image so the text is white on black background. Then get the contours and remove the small ones by area filtering. Then get the rotated bounding boxes from cv2.minAreaRect(). See https://docs.opencv.org/4.1.1/d3/dc0/group__imgproc__shape.html#ga3d476a3417130ae5154aea421ca7ead9 – fmw42 Sep 05 '20 at 05:50
  • Can you give me a sample code for this. I'm not able to code it. ` imagem = cv2.bitwise_not(dilation) imagem = cv2.cvtColor(imagem, cv2.COLOR_RGB2GRAY) _,contours, hierarchy = cv2.findContours(imagem, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(imagem, contours, -1, (0,255,0), 3) ` I get a black screen – Emm Sep 05 '20 at 06:45
  • See the answer below. – fmw42 Sep 05 '20 at 17:20

1 Answers1

0

Building on the code you have already written, you need to invert the result and apply findContours().

inv_img = cv2.bitwise_not(dilation)
contours, hierarchy = cv2.findContours(gray_in, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
  rect = cv2.minAreaRect(cnt)
  box = cv2.cv.BoxPoints(rect) # cv2.boxPoints(rect) for OpenCV 3.x
  box = np.int0(box)
  cv2.drawContours(im,[box],0,(0,0,255),2)

You need to see which box is better and which contour is the best fit. This answer would be helpful: Python OpenCV Box2D