python - 从车牌中查找轮廓时出错 - 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'
我给算法这个图像:
为什么会这样?
解决方案
推荐阅读
- r - 无法使用 stringr::string_extract 在正则表达式中转义 $
- arrays - 如何在 Go 中清除和重用数组(不是切片)?
- mongodb - MongoDB - 我可以创建一个索引来隔离文档键中的值以加快搜索速度吗?
- python - 如何正确验证 REST API 查询参数?
- python - Python 语法:带有输入元组的 __getitem__ 和 __setitem__?
- java - 比较 3 个数组并将 Null 错误判断为某个值
- postsharp - PostSharp 不会在最新的 Visual Studio (16.8.1) 中构建
- java - 如何优化这个for循环?
- firebase - 将自定义域从一个项目更改为另一个项目
- java - 如何在 Json 中仅解包 id 参数