首页 > 解决方案 > 如何在签名上创建边界框并忽略文本区域

问题描述

我正在从图像中检测手写签名。我需要检测签名然后提取它,在签名周围创建矩形,裁剪矩形并保存它。

我正在努力将签名的整个区域识别为一个轮廓或一组轮廓。因为我正在处理这种图像。 https://raw.githubusercontent.com/ahmetozlu/signature_extractor/master/inputs/in2.jpg 或者也处理灰度​​背景图像。我使用下面的代码,它会创建小盒子。关于如何解决这个问题的任何建议?

import cv2
import uuid
image = cv2.imread('img.png')
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU,img)
cv2.bitwise_not(img,img)
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 5))
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, rect_kernel)
contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

count=0
id = uuid.uuid4()
if len(contours) != 0:
    for c in contours:


        x,y,w,h = cv2.boundingRect(c)
        filename = '{0}{1}{2}.{3}'.format('D:\users\outputs/',id, count, "jpg")
        position='{0},{1}'.format(x, y)
        print("Coordinates of the Signatures:" ,position)
        print(filename)
        count += 1
        print(count)
        if(h>70):
            cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),1)
            roi = image[y:y+h, x:x+w]
            cv2.imwrite(filename, roi)


            # roi2 = image[y:y + h, x:x + w]
           

cv2.imwrite('D:\users\outputs/Result.jpg', image)
cv2.waitKey(0)

标签: pythonopencvcomputer-vision

解决方案


推荐阅读