寻找最大的矩形轮廓

时间:2019-10-23 17:32:21

标签: opencv image-processing python-3.7

本地化车牌。

我正在从事车牌识别项目。定位板时,我首先获取所有矩形轮廓,然后从中选择最大的轮廓。

for img in images:
    gray_image_array = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  #convert to grayscale
    resized = imutils.resize(gray_image_array, width=300)
    ratio = gray_image_array.shape[0] / float(resized.shape[0])
    #gray_image_mf = median_filter(gray_image_array, 1) #applying unsharp mask filter
    ret,thresh1 = cv2.threshold(gray_image_array,150,255,cv2.THRESH_BINARY) #thresholding
    thresh1 = cv2.bitwise_not(thresh1)

    #......applying canny edge detection....

    v = np.median(thresh1)
    sigma=0.33
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    edges = cv2.Canny(thresh1,100,200)

    #...canny ends......

    #.......applying dilation.....

    kernel = np.ones((5,5),np.uint8)
    dilation = cv2.dilate(edges,kernel,iterations = 2)
    closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)

    #.........dilation ends.......

    contours, hierarchy = cv2.findContours(closing.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    #im = cv2.drawContours(closing,contours,-1,(255,0,0),5)
    h,w,_ = img.shape
    mask = np.zeros((h+2,w+2), np.uint8)
    for c in contours:
            poly = cv2.approxPolyDP(c, 0.02*cv2.arcLength(c,True),True)
            if len(poly) == 4:
              # if the contour has 4 vertices then floodfill that contour with black color
                co = max(c, key = cv2.contourArea)
                cnt = np.vstack(co).squeeze()
                ret,closing,mask,_ = cv2.floodFill(closing, mask, tuple(cnt[0]), 0)

    plt.imshow(closing,cmap="gray")
    plt.show()

获取错误

<ipython-input-17-4161649b8e06> in <module>
       co = max(c, key = cv2.contourArea)
       cnt = np.vstack(co).squeeze()
 --->  ret,closing,mask,_ = cv2.floodFill(closing, mask, tuple(cnt[0]), 0)

       plt.imshow(closing,cmap="gray")

one of my image is

0 个答案:

没有答案