0

I have a text in an image, and there's a bounding box around it, like so:

enter image description here

What I'm trying to do is to find the top-left & bottom-right coordinates to calculate the diagonal of the rectangle. I didn't find anything in particular in python libraries. Here's the code of the rectangle:

image = cv2.imread('output.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9,9), 0)
thresh = 
cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 
cv2.THRESH_BINARY_INV,11,30)


kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
dilate = cv2.dilate(thresh, kernel, iterations=4)


cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

ROI_number = 0
for c in cnts:
   area = cv2.contourArea(c)
   if area > 10000:
     x,y,w,h = cv2.boundingRect(c)
     boundbox = cv2.rectangle(image, (x, y), (x + w, y + h), 
     (36,255,12), 3)

Any ideas? Thank you in advance!

1 Answers1

0

if you have the array of the image you can write an algorithm that scan line per line the array first horizontally and then vertically to search the coordinates of the y and x that you are looking for. That is an RGB image and the rectangle looks very visibile and with a good contrast, so i suggest you to extract first the "G" of the image, to do it you have to decimate your array in order to extract the green part of the image, and then run a for loop looking for a certain number of pixel with an intensity value that you like in that row or columns and then return the index where you found it as the coordinate.

Best regards

Marco Helm
  • 16
  • 3