使用边缘检测从图像中裁剪子图像

时间:2020-03-28 21:11:23

标签: python python-3.x image image-processing python-imaging-library

所以基本上我有一个带有空格的图像和上面的文本。 输出应仅为图片。没有文本和空格。最好的例子可能是一个模因:

like this

我相信我必须先获取角坐标,然后再使用枕头的Image.crop(corner_coordinates)之类的东西。

我该如何实现呢?

编辑:所以我尝试了一下。我使用了Canny边缘检测算法(opencv)。我现在得到了想要的边缘,也从文本中得到了边缘。如果有人可以帮助我,那会很好:)

1 个答案:

答案 0 :(得分:1)

您可能会发现非白色的最大轮廓的边界矩形。

我建议使用以下阶段:

  • 将图像从BGR转换为灰色。
  • 将灰度图像转换为二进制图像。
    使用自动阈值(使用cv2.THRESH_OTSU标志)并反转极性。
    结果是原始图像较暗时为白色,图像较亮时为黑色。
  • 使用cv2.findContours()查找轮廓(如Mark Setchell所评论)。
    查找外部轮廓比检测边缘更简单。
  • 找到具有最大面积的轮廓的边界矩形。
  • 从输入图像中裁剪边框。
    我使用NumPy数组切片而不是使用枕头。

这是代码:

import cv2

# Read input image
img = cv2.imread('img.jpg')

# Convert from BGR to Gray.
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Convert to binary image using automatic threshold and invert polarity
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

# Find contours on thresh
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]  # Use index [-2] to be compatible to OpenCV 3 and 4

# Get contour with maximum area
c = max(cnts, key=cv2.contourArea)

x, y, w, h = cv2.boundingRect(c)

# Crop the bounding rectangle (use .copy to get a copy instead of slice).
crop = img[y:y+h, x:x+w, :].copy()


# Draw red rectangle for testing
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), thickness = 2)

# Show result
cv2.imshow('img', img)
cv2.imshow('crop', crop)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

crop
enter image description here

img
enter image description here

thresh
enter image description here