use*_*_12 6 python opencv image image-processing computer-vision
我有以下图像,它是收据图像,收据周围有很多空白区域。我想裁剪空白区域。我无法手动裁剪它,所以我正在寻找一种方法来做到这一点。
剪下一张:
从以下帖子中尝试了此代码:How to remove whitespace from an image in OpenCV?
gray = load_image(IMG_FILE) # image file
gray = 255*(gray < 128).astype(np.uint8)
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
x, y, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
rect = load_image(IMG_FILE)[y:y+h, x:x+w] # Crop the image - note we do this on the original image
Run Code Online (Sandbox Code Playgroud)
它正在裁剪白色空间的一小部分。
这是一个简单的方法:
获取二值图像。加载图像,转换为灰度,应用大的高斯模糊,然后是大津阈值
进行形态学操作。我们首先用一个小内核 morph open 来去除噪声,然后用一个大内核 morph close 来组合轮廓
找到封闭的边界框并裁剪 ROI。我们找到所有非零点的坐标,找到边界矩形,并裁剪 ROI。
这是检测到的 ROI 以绿色突出显示
裁剪的投资回报率

import cv2
# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (25,25), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Perform morph operations, first open to remove noise, then close to combine
noise_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, noise_kernel, iterations=2)
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, close_kernel, iterations=3)
# Find enclosing boundingbox and crop ROI
coords = cv2.findNonZero(close)
x,y,w,h = cv2.boundingRect(coords)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
crop = original[y:y+h, x:x+w]
cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('image', image)
cv2.imshow('crop', crop)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)
我是您尝试的代码的原始作者。它不起作用的原因是因为文本周围有一些嘈杂的像素,从而导致了算法的失败。如果通过简单的开形态学运算去除噪声,您就可以获得所需的结果。事实上,这是在我的答案的第二个版本中完成的,不幸的是您没有尝试:
import cv2
import numpy as np
gray = load_image(IMG_FILE) # image file
# Threshold the image so that black text is white
gray = 255*(gray < 128).astype(np.uint8)
# Additionally do an opening operation with a 2 x 2 kernel
O = np.ones(2, dtype=np.uint8)
gray_morph = cv2.morphologyEx(gray, cv2.MORPH_OPEN, O)
# Continue where we left off
coords = cv2.findNonZero(gray_morph) # Find all non-zero points (text)
x, y, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
rect = load_image(IMG_FILE)[y:y+h, x:x+w] # Crop the image
Run Code Online (Sandbox Code Playgroud)
由此我们得到: