Я делаю серию фильтров, чтобы найти контур патентной пластины, однако часть с классом 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'
Я передаю алгоритму это изображение:
Почему так случилось?