[英]Crop text out of binary image OpenCV C++
How can I crop the image so that only the text is included in the image using OpenCV? 如何使用OpenCV裁剪图像,以便仅将文本包括在图像中?
Approach 方法
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 输出量
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.
拥有船体后,您可以获得最低和最高的x和y坐标,并将它们用作矩形坐标,以便可以裁剪图像的其余部分。
An example of how to obtain a convex hull in Open_CV (C++) is available here . 此处提供了有关如何在Open_CV(C ++)中获得凸包的示例。
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. 在图像上应用一些滤镜(也许使用侵蚀滤镜)可能是个好主意,这样您就可以消除任何误报。
Take projection of white pixels in x-direction and y-direction. 沿x方向和y方向投影白色像素。 The lowest and highest values in x and y direction are bound of your rectangle's y and x co-ordinates respectively.
x和y方向上的最低和最高值分别与矩形的y和x坐标绑定。 This solution is for simple situations with low processing and in-place calculation.
该解决方案适用于处理量低和就地计算的简单情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.