首页 > 解决方案 > 从车牌中查找轮廓时出错 - fillConvexPoly

问题描述

我正在制作一系列过滤器来查找专利板的轮廓,但是具有类 fillConvexPoly 的部分不起作用。

这是课程:

'''This function finds the most plate-like contour'''

def find_plate(image):
    # use thresholding to isolate the plate
    (_,thresh) = cv2.threshold(image, 60, 255,cv2.THRESH_BINARY)

    # find the contours of the thresholded image
    (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    # sort the contours by size
    srt_cnts =  sorted(cnts, key=cv2.contourArea, reverse=True)
    # find the aspect ratio of each of the four largest contours to eliminate any with
    # the wrong shape
    bestContour = []
    for (i,c) in enumerate(srt_cnts[:4]):
        (x,y,w,h) = cv2.boundingRect(c)
        aspectRatio = w / float(h)
        if 1.5 <= aspectRatio <= 2.5:
            bestContour = srt_cnts[i]
            break

    # create a mask to isolate the plate area
    mask = np.zeros((img_height, img_width), np.uint8)
    cv2.fillConvexPoly(mask, bestContour, 1)
    masked_img = cv2.bitwise_and(image, image, mask=mask)

    # fill mask in with white
    mask2 = np.zeros((img_height+2, img_width+2), np.uint8)
    seed = (0,0)
    cv2.floodFill(masked_img, mask2, seed, (255,255,255))

    x,y,w,h = cv2.boundingRect(bestContour)

    plate_crop = masked_img[y+2:y+h-2,x+5:x+w-5]
    plate_crop = cv2.GaussianBlur(plate_crop, ksize=(3,3), sigmaX=0)

    plate_thresh = cv2.adaptiveThreshold(plate_crop, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 7)

    return plate_thresh

滤波灰度和直方图均衡后的输出为:

调用方法..

plate_thresh2 = find_plate(hist_equal)
imshow(plate_thresh2)

输出:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-88-274610a4c666> in <module>()
----> 1 plate_thresh2 = find_plate(hist_equal)
      2 imshow(plate_thresh2)

<ipython-input-87-e91dfe20127c> in find_plate(image)
     22     # create a mask to isolate the plate area
     23     mask = np.zeros((img_height, img_width), np.uint8)
---> 24     cv2.fillConvexPoly(mask, bestContour, 1)
     25     masked_img = cv2.bitwise_and(image, image, mask=mask)
     26 

TypeError: Expected Ptr<cv::UMat> for argument '%s'

我给算法这个图像:

在此处输入图像描述

为什么会这样?

标签: pythonimageopencvimage-processing

解决方案


推荐阅读