【问题标题】:Checkbox detection opencv复选框检测opencv
【发布时间】:2020-11-14 23:24:15
【问题描述】:

我一直在尝试检测复选框。虽然我能够检测到其他图像中的方形轮廓,但我无法获得这个特定图像的轮廓。请帮我检测复选框。

输入图像:

这是我的代码,

for myfile in files:
    image=cv2.imread(myfile)
    image = cv2.resize(image, (180,60), interpolation = cv2.INTER_AREA)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #apply otsu's threshold
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
        #setting up threshold values
    threshold_max_area = 300
    threshold_min_area = 10
 #finding contours in the image
    cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
#getting the coordinates for each checkbox
    count=0
    centers=[]
    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.035 * peri, True)
        x,y,w,h = cv2.boundingRect(approx)
        aspect_ratio = w / float(h)
        area = cv2.contourArea(c) 
        if len(approx) == 4 and area < threshold_max_area and area > threshold_min_area and (aspect_ratio >= 0.9 and aspect_ratio <= 1.1):
            centers.append([x,y,x+w,y+h]) 
        count=count+1
    print(centers)
    
cv2.imshow(" ",image)
cv2.waitKey()

【问题讨论】:

  • 您可以从之前提出的问题link得到答案。
  • @Abhishek 当您的复选框中没有勾选标记时,您提到的链接很好。就我而言,有一个刻度线,因此我无法检测到方形轮廓。
  • 二值图像上的模板匹配怎么样?如果您知道框的大小,则可以搜索矩形。

标签: python opencv image-processing computer-vision contour


【解决方案1】:

这是解决问题的一种方法。

第 1 步:二值化

import os
import cv2
import numpy as np
import pandas as pd

### reading input image
image_path='test_sample.jpg'
image=cv2.imread(image_path)

### converting BGR to Grayscale
gray_scale=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

### Binarising image
th1,img_bin = cv2.threshold(gray_scale,180,225,cv2.THRESH_OTSU)

二进制图像:

第 2 步:寻找水平线和垂直线

### defining kernels

lWidth = 2
lineMinWidth = 15

kernal1 = np.ones((lWidth,lWidth), np.uint8)
kernal1h = np.ones((1,lWidth), np.uint8)
kernal1v = np.ones((lWidth,1), np.uint8)

kernal6 = np.ones((lineMinWidth,lineMinWidth), np.uint8)
kernal6h = np.ones((1,lineMinWidth), np.uint8)
kernal6v = np.ones((lineMinWidth,1), np.uint8)

### finding horizontal lines
img_bin_h = cv2.morphologyEx(~img_bin, cv2.MORPH_CLOSE, kernal1h) # bridge small gap in horizonntal lines
img_bin_h = cv2.morphologyEx(img_bin_h, cv2.MORPH_OPEN, kernal6h) # kep ony horiz lines by eroding everything else in hor direction

水平线:

## detect vert lines
img_bin_v = cv2.morphologyEx(~img_bin, cv2.MORPH_CLOSE, kernal1v)  # bridge small gap in vert lines
img_bin_v = cv2.morphologyEx(img_bin_v, cv2.MORPH_OPEN, kernal6v)# kep ony vert lines by eroding everything else in vert direction

垂直线:

第 3 步:结合水平线和垂直线

def fix(img):
    img[img>127]=255
    img[img<127]=0
    return img
img_bin_final = fix(fix(img_bin_h)|fix(img_bin_v))

组合二进制输出:

第 4 步:使用连通分量查找矩形

### getting labels
ret, labels, stats,centroids = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)

### drawing recangles for visualisation
for x,y,w,h,area in stats[2:]:
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)

最终输出:

【讨论】:

  • @SharathChanderP 如果您能够解决问题,请接受答案。谢谢:D
【解决方案2】:

为了从复选框中提取信息,我们应该执行一组形态学操作。其中包括通过检测水平、垂直线和轮廓过滤来识别边缘。

请在此处查看详细的博客文章:https://nanonets.com/blog/checkbox-detection。我们已经提到了如何使用 CV 和深度学习技术来提取复选框的不同技术。

边缘检测、轮廓过滤的代码sn-p。

# set min width to detect horizontal lines

line_min_width = 15

# kernel to detect horizontal lines
kernal_h = np.ones((1,line_min_width), np.uint8)

# kernel to detect vertical lines
kernal_v = np.ones((line_min_width,1), np.uint8)

# horizontal kernel on the image
img_bin_horizontal = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_h)

# verical kernel on the image
img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_v)


# combining the image

img_bin_final=img_bin_h|img_bin_v

# contour filtering 

_, labels, stats,_ = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)

for x,y,w,h,area in stats[2:]:
    cv2.rectangle(image_array,(x,y),(x+w,y+h),(0,255,0),2)

Image.fromarray(image_array).show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-29
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多