使用Opencv和Python的多选扫描程序

时间:2016-01-15 11:03:11

标签: python opencv detection multiple-choice

我正在开发一个项目来扫描这种多选文件:

Multiple Choices Image

代码有效但有时我会遇到一些麻烦来识别复选框的正确位置。

我按照以下步骤继续:

  • 首先,我检测到图像右侧的20个黑框,然后将x和宽度添加到列表中:

    
    image = cv2.imread(args["image"])
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    (_, thresh) = cv2.threshold(gray, 220, 255,cv2.THRESH_BINARY) 
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
    closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    dilation = cv2.dilate(closed,None,iterations = 5)
    (_,contours,_)=cv2.findContours(dilation.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    sorted_rows = sorted(contours, key = cv2.contourArea, reverse = True)[1:21]
    for idx,row in enumerate(sorted_rows):
        (_x,y,w,h) = cv2.boundingRect(row)
        rows.append([(y),(y+h)])
    
     
    
  • 然后我检测到五列:

    
    (_, thresh) = cv2.threshold(gray, 127, 255,cv2.THRESH_BINARY) 
    closed = cv2.erode(thresh, None, iterations = 4)
    (_,contours,_)=cv2.findContours(closed.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    sorted_cols = sorted(contours, key = cv2.contourArea, reverse = True)[1:6]
    for col in sorted_cols:
        # add to list
        (x,y,w,h) = cv2.boundingRect(col)
        cols.append([(x),(x+w)])
    
    
  • 接下来,我分别按x和y的位置对两个列表进行排序:

    
    cols = sorted(cols, key = lambda x: x[0])
    rows = sorted(rows, key = lambda x: x[0])
    
    
  • 然后我循环遍历列列表和行列表以构建我的十字路口的坐标,在那里我可以找到五个复选框

    
    count_iterations = 0
    for col in cols:
        for row in rows:  
            count_iterations +=1 
            crop = image[row[0]:row[1], col[0]: col[1]]
            denoised = cv2.fastNlMeansDenoisingColored(crop,None,10,10,7,21)
            edges = cv2.Canny(denoised,220,250)
            closed = cv2.dilate(edges, None, iterations = 1)
            (_,contours,_)= cv2.findContours(closed.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
            checkboxes = sorted(contours, key = cv2.contourArea, reverse = True)[:5]
            crdn_checkboxes = []
            for check_box in checkboxes:
                crdn_checkboxes.append(cv2.boundingRect(check_box))
         
    
  • 最后,我按照x位置对复选框进行排序,然后检查像素密度以找到合适的复选框:

    
    crdn_checkboxes = sorted(crdn_checkboxes, key = lambda x: x[0])
    for idx, crdn in enumerate(crdn_checkboxes):
    
    
    x,y,w,h = crdn
    check_box = crop[y:y+h,x:x+w]
    check_box_img_gray = cv2.cvtColor(check_box, cv2.COLOR_BGR2GRAY)
    (_, thresh) = cv2.threshold(check_box_img_gray,200,255,cv2.THRESH_BINARY)
    height,width = thresh.shape[:2]
    tot_px = height * width
    checked_box =  cv2.countNonZero(thresh) / float(tot_px)
    if checked_box < 0.6: 
        print "Found"
    

问题似乎是由于五个复选框左侧存在的行号被识别为复选框。

Line processed

我想知道是否有人可以帮忙解决这个问题并最终审核我的代码。

0 个答案:

没有答案