如何从纸上提取这6个符号(签名)(opencv)
How to extract these 6 symbols (signatures) from paper (opencv)
我有一张图片:
我正在尝试一个一个地提取符号。
我试过 findContours()
但我得到了很多内部轮廓。有什么办法吗?
在查找轮廓时始终确保感兴趣区域为白色。在这种情况下,将图像转换为灰度后,应用倒置二进制阈值,使签名为白色。这样做之后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()
结果:
这里我有一些提取的签名。
我有一张图片:
我正在尝试一个一个地提取符号。
我试过 findContours()
但我得到了很多内部轮廓。有什么办法吗?
在查找轮廓时始终确保感兴趣区域为白色。在这种情况下,将图像转换为灰度后,应用倒置二进制阈值,使签名为白色。这样做之后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()
结果:
这里我有一些提取的签名。