我有一张图片:
,我正在尝试一一提取符号。
我尝试过findContours()
,但内部轮廓很多。有什么办法吗?
答案 0 :(得分:4)
在寻找轮廓时,始终要确保感兴趣的区域为白色。在这种情况下,将图像转换为灰度后,应用反向二进制阈值,以使签名为白色。完成此操作后,findContours()
将轻松找到所有签名。
代码:
以下实现是在python中实现的:
import cv2
image = cv2.imread(r'C:\Users\Jackson\Desktop\sign.jpg')
#--- Image was too big hence I resized it ---
image = cv2.resize(image, (0, 0), fx = 0.5, fy = 0.5)
#--- Converting image to grayscale ---
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#--- Performing inverted binary threshold ---
retval, thresh_gray = cv2.threshold(gray, 0, 255, type = cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow('sign_thresh_gray', thresh_gray)
#--- finding contours ---
image, contours, hierarchy = cv2.findContours(thresh_gray,cv2.RETR_EXTERNAL, \
cv2.CHAIN_APPROX_SIMPLE)
for i, c in enumerate(contours):
if cv2.contourArea(c) > 100:
x, y, w, h = cv2.boundingRect(c)
roi = image[y :y + h, x : x + w ]
cv2.imshow('sign_{}.jpg'.format(i), roi)
cv2.waitKey()
cv2.destroyAllWindows()
结果:
这里有一些提取的签名。