Crop text out of binary image OpenCV C++

我们两清 提交于 2019-12-23 03:42:10

问题


How can I crop the image so that only the text is included in the image using OpenCV?


回答1:


Approach

  1. Dilating the image in in the vertical and horizontal direction
  2. Finding the bounding rectangle
  3. Cropping the image

Code

# reading the input image in grayscale image
image = cv2.imread('image2.png',cv2.IMREAD_GRAYSCALE)
image /= 255
if image is None:
    print 'Can not find/read the image data'
# Defining ver and hor kernel
N = 5
kernel = np.zeros((N,N), dtype=np.uint8)
kernel[2,:] = 1
dilated_image = cv2.dilate(image, kernel, iterations=2)

kernel = np.zeros((N,N), dtype=np.uint8)
kernel[:,2] = 1
dilated_image = cv2.dilate(dilated_image, kernel, iterations=2)
image *= 255

# finding contours in the dilated image
contours,a = cv2.findContours(dilated_image,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# finding bounding rectangle using contours data points
rect = cv2.boundingRect(contours[0])
pt1 = (rect[0],rect[1])
pt2 = (rect[0]+rect[2],rect[1]+rect[3])
cv2.rectangle(image,pt1,pt2,(100,100,100),thickness=2)

# extracting the rectangle
text = image[rect[1]:rect[1]+rect[3],rect[0]:rect[0]+rect[2]]

plt.subplot(1,2,1), plt.imshow(image,'gray')
plt.subplot(1,2,2), plt.imshow(text,'gray')

plt.show()

Output




回答2:


Assuming that you have pixel information you could draw a convex hull around the white text. Once that you have the hull, you can get the lowest and highest x and y coordinate and use those as your rectangle coordinates so that you could crop the rest of the image.

An example of how to obtain a convex hull in Open_CV (C++) is available here.

It might be a good idea to apply some filter on the image (maybe use the erosion filter) so that you can remove any false positives.




回答3:


Take projection of white pixels in x-direction and y-direction. The lowest and highest values in x and y direction are bound of your rectangle's y and x co-ordinates respectively. This solution is for simple situations with low processing and in-place calculation.



来源:https://stackoverflow.com/questions/23125359/crop-text-out-of-binary-image-opencv-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!