如何从纸张中提取这6个符号(签名)(opencv)

leo*_*abh 5 opencv feature-extraction contour

我有一个图像:

一个图像

我正在尝试一一提取这些迹象。我尝试过findContours(),但得到了很多内部轮廓。有什么办法可以做到这一点吗?

Jer*_*uke 5

在寻找轮廓时始终确保感兴趣的区域为白色。在这种情况下,将图像转换为灰度后,应用反转的二进制阈值,使签名为白色。这样做之后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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

#--- 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()
Run Code Online (Sandbox Code Playgroud)

结果:

这里我有一些提取的签名。

在此输入图像描述

在此输入图像描述

在此输入图像描述