【发布时间】:2020-09-23 04:07:05
【问题描述】:
我正在处理车牌,我所做的是对其应用一系列过滤器,例如:
- 灰度
- 模糊
- 阈值
- 二进制
问题是当我这样做时,边界处有一些像这张图片一样的轮廓,我该如何清除它们?或者让它只是黑色(蒙面)?我使用了这段代码,但有时它会掉下来。
# invert image and detect contours
inverted = cv2.bitwise_not(image_binary_and_dilated)
contours, hierarchy = cv2.findContours(inverted,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# get the biggest contour
biggest_index = -1
biggest_area = -1
i = 0
for c in contours:
area = cv2.contourArea(c)
if area > biggest_area:
biggest_area = area
biggest_index = i
i = i+1
print("biggest area: " + str(biggest_area) + " index: " + str(biggest_index))
cv2.drawContours(image_binary_and_dilated, contours, biggest_index, [0,0,255])
center, size, angle = cv2.minAreaRect(contours[biggest_index])
rot_mat = cv2.getRotationMatrix2D(center, angle, 1.)
#cv2.warpPerspective()
print(size)
dst = cv2.warpAffine(inverted, rot_mat, (int(size[0]), int(size[1])))
mask = dst * 0
x1 = max([int(center[0] - size[0] / 2)+1, 0])
y1 = max([int(center[1] - size[1] / 2)+1, 0])
x2 = int(center[0] + size[0] / 2)-1
y2 = int(center[1] + size[1] / 2)-1
point1 = (x1, y1)
point2 = (x2, y2)
print(point1)
print(point2)
cv2.rectangle(dst, point1, point2, [0,0,0])
cv2.rectangle(mask, point1, point2, [255,255,255], cv2.FILLED)
masked = cv2.bitwise_and(dst, mask)
#cv2_imshow(imgg)
cv2_imshow(dst)
cv2_imshow(masked)
#cv2_imshow(mask)
一些结果:
原版是:
二进制板是:
如何修复此代码?只是我想避免或改进它。
【问题讨论】:
-
好问题! :) 你能澄清一下
image_binary_and_dilated(第二行 - 我假设是初始图像,但它已经膨胀了吗?)和gray(在warpAffine中)是什么?也许对你为什么使用 RotationMatrix 和 warpAffine 有一个小小的直觉?这样可以更容易地完全理解代码,谢谢。 -
谢谢!你是对的,image_binary_dilared 是已经膨胀的二值图像。灰色是我的错误,应该是 image_binary_dilated,已经修复了。 WarpAffine 和 RotationMatrix 我只是用它来保持图像正确,但我认为这些不是那么必要
-
对不起,“灰色”是倒置变量,已更新
标签: python python-3.x image opencv image-processing