Skip to content Skip to sidebar Skip to footer

How To Draw Bigger Bounding Box And Crop Only Bounding Box Text Python Opencv

I am using easyocr to detect the text in the image. The method gives the output bounding box. The input images are shown below Image 1 Image 2 The output image is obtained using

Solution 1:

After removal of low precision results you can combine all the valid points into a single 2D array and use cv2.boundingRect to get the bounding box.

Code:

points = []
for result in results:
    points.extend(result[0])

rect = cv2.boundingRect(np.array(points))

x, y, w, h = rect

image2 = image.copy()
cv2.rectangle(image2, (x, y), (x + w, y + h), (255, 0, 0), 1)

plt.imshow(image2)
plt.show()

Images:

enter image description here enter image description here

And to crop the text use this line:

image_cropped = image[y:y+h, x:x+w]

or if more precise cropping is needed:

mask = np.zeros_like(image)
# grayscale or color image
color = 255 if len(mask.shape) == 2 else mask.shape[2] * [255]
# create a mask
for result in results:
    cv2.fillConvexPoly(mask, np.array(result[0]), color)

# mask the text, and invert the mask to preserve white background
image_masked = cv2.bitwise_or(cv2.bitwise_and(image, mask), cv2.bitwise_not(mask))

image_cropped = image_masked[y:y+h, x:x+w]

Post a Comment for "How To Draw Bigger Bounding Box And Crop Only Bounding Box Text Python Opencv"