我的算法无法正常工作,应该封装车牌上的每个字母,我已经做到了:
import cv2
import numpy as np
from imutils import contours
image = cv2.imread('/content/Screenshot_3.png')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, _) = contours.sort_contours(cnts, method="left-to-right")
ROI_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area < 800 and area > 200:
x,y,w,h = cv2.boundingRect(c)
ROI = 255 - thresh[y:y+h, x:x+w]
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
#cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
ROI_number += 1
cv2.imshow(mask)
cv2.imshow(thresh)
mask
输出:
thresh
输出:
原始图像是这样的:
我在做什么错了?
答案 0 :(得分:4)
问题是由于上述连接,您的掩码尝试将W
和7
保留在相反的类中。如果您调整阈值,则会看到F
,J
,D
,2
或W
,7
。 / p>
import matplotlib.pyplot as plt
import cv2
import numpy as np
from imutils import contours
image = cv2.imread('test.png')
plt.imshow(image)
plt.show()
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, _) = contours.sort_contours(cnts, method="left-to-right")
ROI_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area < 10500 and area > 0:
x,y,w,h = cv2.boundingRect(c)
ROI = 255 - thresh[y:y+h, x:x+w]
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
#cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
ROI_number += 1
plt.imshow(thresh)
plt.show()
plt.imshow(mask)
plt.show()
如您所见,一旦更改阈值,我将得到W
和7
,但在否定类中,所有其他字符都消失了。
我们可以通过在生成蒙版之前腐蚀阈值来解决此问题。
import matplotlib.pyplot as plt
import cv2
import numpy as np
from imutils import contours
image = cv2.imread('test.png')
plt.imshow(image)
plt.show()
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel = np.ones((5,5), np.uint8)
thresh = cv2.erode(thresh, kernel, iterations=1) # eroding
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, _) = contours.sort_contours(cnts, method="left-to-right")
ROI_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area < 800:
x,y,w,h = cv2.boundingRect(c)
ROI = 255 - thresh[y:y+h, x:x+w]
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
#cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
ROI_number += 1
plt.imshow(thresh)
plt.show()
plt.imshow(mask)
plt.show()
现在,您可以应用一些简单的处理以获得更清晰的蒙版。