如何在OpenCV中从图像中删除空格? [英] How to remove whitespace from an image in OpenCV?

查看:157
本文介绍了如何在OpenCV中从图像中删除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下图像,其中包含文本和文本下方有很多空白.我想裁剪空白区域,使其看起来像第二张图像.

I have the following image which has text and a lot of white space underneath the text. I would like to crop the white space such that it looks like the second image.

裁剪的图像

这就是我所做的

>>> img = cv2.imread("pg13_gau.jpg.png")
>>> gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
>>> edged = cv2.Canny(gray, 30,300)
>>> (img,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
>>> cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]

推荐答案

注释中提到了很多,最好的方法是将图像反转,使黑色文本变为白色,找到图像中所有非零点然后确定最小跨度边界框是什么.您可以使用此边界框最终裁剪图像.查找轮廓非常昂贵,这里不需要,尤其是因为您的文本是与轴对齐的.您可以结合使用 cv2.findNonZero cv2.boundingRect 来完成您需要的工作.

As many have alluded in the comments, the best way is to invert the image so the black text becomes white, find all the non-zero points in the image then determine what the minimum spanning bounding box would be. You can use this bounding box to finally crop your image. Finding the contours is very expensive and it isn't needed here - especially since your text is axis-aligned. You can use a combination of cv2.findNonZero and cv2.boundingRect to do what you need.

因此,类似的方法将起作用:

Therefore, something like this would work:

import numpy as np
import cv2

img = cv2.imread('ws.png') # Read in the image and convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8) # To invert the text to white
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
x, y, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
rect = img[y:y+h, x:x+w] # Crop the image - note we do this on the original image
cv2.imshow("Cropped", rect) # Show it
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("rect.png", rect) # Save the image

上面的代码准确地列出了我一开始所讨论的内容.我们读取了图像,但是由于某些原因,由于图像是彩色的,我们也转换为灰度.棘手的部分是代码的第三行,在该行中,我将阈值设置为低于强度128,以使深色文本变为白色.但是,这会生成一个二进制图像,因此我转换为uint8,然后按255缩放.这实际上是对文本进行反转.

The code above exactly lays out what I talked about in the beginning. We read in the image, but we also convert to grayscale as your image is in colour for some reason. The tricky part is the third line of code where I threshold below the intensity of 128 so that the dark text becomes white. This however produces a binary image, so I convert to uint8, then scale by 255. This essentially inverts the text.

接下来,给定这张图片,我们找到所有带有cv2.findNonZero的非零坐标,最后将其放入cv2.boundingRect中,这将为您提供边界框的左上角以及宽度和高度.我们最终可以使用它来裁剪图像.请注意,我们是在原始图像上执行此操作,而不是在反转图像上执行.我们仅使用NumPy数组索引为我们进行裁剪.

Next, given this image we find all of the non-zero coordinates with cv2.findNonZero and we finally put this into cv2.boundingRect which will give you the top-left corner of the bounding box as well as the width and height. We can finally use this to crop the image. Note we do this on the original image and not the inverted one. We use simply NumPy array indexing to do the cropping for us.

最后,我们显示该图像以表明它可以工作,并将其保存到磁盘中.

Finally, we show the image to show that it works and we save it to disk.

我现在得到这张图片:

对于第二张图像,一件好事是删除一些右边框和下边框.我们可以通过首先将图像裁剪到那个来做到这一点.接下来,此图像包含一些非常小的噪点像素.我建议使用很小的内核进行形态学开放,然后重做上面讨论的逻辑.

For the second image, a good thing to do is to remove some of the right border and bottom border. We can do that by cropping the image down to that first. Next, this image contains some very small noisy pixels. I would recommend doing a morphological opening with a very small kernel, then redo the logic we talked about above.

因此:

import numpy as np
import cv2

img = cv2.imread('pg13_gau_preview.png') # Read in the image and convert to grayscale
img = img[:-20,:-20] # Perform pre-cropping
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8) # To invert the text to white
gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, np.ones((2, 2), dtype=np.uint8)) # Perform noise filtering
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
x, y, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
rect = img[y:y+h, x:x+w] # Crop the image - note we do this on the original image
cv2.imshow("Cropped", rect) # Show it
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("rect.png", rect) # Save the image

这篇关于如何在OpenCV中从图像中删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆