如何从OpenCV中的图像中删除空格?

Ant*_*ony 13 python opencv image-processing opencv3.0

我有下面的图像,文字下面有文字和很多空白区域.我想裁剪白色空间,使它看起来像第二个图像.

在此输入图像描述

裁剪图像

在此输入图像描述

这就是我所做的

>>> 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]
Run Code Online (Sandbox Code Playgroud)

ray*_*ica 12

正如许多人在评论中提到的那样,最好的方法是反转图像,使黑色文本变为白色,找到图像中的所有非零点,然后确定最小跨越边界框将是什么.您可以使用此边界框最终裁剪图像.查找轮廓非常昂贵,此处不需要 - 尤其是因为文本是轴对齐的.您可以结合使用cv2.findNonZerocv2.boundingRect执行您需要的操作.

因此,这样的事情会起作用:

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
Run Code Online (Sandbox Code Playgroud)

上面的代码确切地说明了我在开始时谈到的内容.我们在图像中读取,但由于某些原因,我们也会转换为灰度,因为您的图像是彩色的.棘手的部分是第三行代码,其中我的阈值低于128的强度,因此暗文本变为白色.然而,这会产生一个二进制图像,所以我转换为uint8,然后缩放255.这实际上反转了文本.

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

最后,我们展示图像以显示它的工作原理并将其保存到磁盘.


我现在得到这个图像:

在此输入图像描述


对于第二张图片,一件好事就是删除一些右边框和底边框.我们可以通过将图像裁剪到第一个来实现.接下来,该图像包含一些非常小的噪声像素.我建议用一个非常小的内核做一个形态开口,然后重做我们上面谈到的逻辑.

因此:

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
Run Code Online (Sandbox Code Playgroud)

注意:由于隐私而删除了输出图像

  • 您可以将其标记为主持人将其删除,或直接向Imgur发送[删除请求](https://imgur.com/removalrequest). (2认同)