你需要的是阈值。在OpenCV中,您可以使用^{}来实现这一点。
我试过了。我的方法如下:转换为灰度
对图像设置阈值以仅获取签名而不获取其他内容
查找阈值图像中显示的像素的位置
在原始灰度中围绕该区域裁剪
从对显示没有那么严格的裁剪创建新的阈值图像
这是我的尝试,我觉得效果很好。import cv2
import numpy as np
# load image
img = cv2.imread('image.jpg')
rsz_img = cv2.resize(img, None, fx=0.25, fy=0.25) # resize since image is huge
gray = cv2.cvtColor(rsz_img, cv2.COLOR_BGR2GRAY) # convert to grayscale
# threshold to get just the signature
retval, thresh_gray = cv2.threshold(gray, thresh=100, maxval=255, type=cv2.THRESH_BINARY)
# find where the signature is and make a cropped region
points = np.argwhere(thresh_gray==0) # find where the black pixels are
points = np.fliplr(points) # store them in x,y coordinates instead of row,col indices
x, y, w, h = cv2.bounding