photoopencv中的字符分割

photoopencv中的字符分割,opencv,ocr,Opencv,Ocr,我随身带着上面的车牌图像。我的目标是对每个角色进行单独的分割,并将其传递到我的神经网络中。我尝试使用以下代码查找countours并使用边界矩形分割这些字符: img = cv2.imread('download.jpeg') gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray_img = cv2.GaussianBlur(gray_img, (5,5), 0) ret, im_th = cv2.threshold(gray_img, 90

我随身带着上面的车牌图像。我的目标是对每个角色进行单独的分割,并将其传递到我的神经网络中。我尝试使用以下代码查找countours并使用边界矩形分割这些字符:

img = cv2.imread('download.jpeg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_img = cv2.GaussianBlur(gray_img, (5,5), 0)
ret, im_th = cv2.threshold(gray_img, 90, 255, cv2.THRESH_BINARY_INV)
im_th = cv2.adaptiveThreshold(gray_img, 255, 
cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,75, 10)
im_th = cv2.bitwise_not(im_th)

ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, ctrs, -1, (0,255,0), 3)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
print len(rects)
for rect in rects:
    cv2.rectangle(img,(rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0,255,0),3)
    length = int(rect[3] * 1.6)
    pt1 = int(rect[1] + rect[3] // 2 - length // 2)
    pt2 = int(rect[0] + rect[2] // 2 - length // 2)
    roi = img[pt1:pt1+length, pt2:pt2+length]

上面的代码创建的区域包括除字符以外的边界矩形。虽然我可以手动过滤掉这些区域,但它会因图像而异。如果我只需要提取带有字符的区域,我将如何执行此操作?

您应该找到字母所在的行。如果你能提取车牌号区域,通过PCA你可以找到字母的旋转。我对图像处理是新手,所以我不太清楚你的意思。你能给我一个更详细的回答吗?
#read image
img = cv2.imread('input_image.png')

#grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.waitKey(0)

#binarize 
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
cv2.waitKey(0)

#find contours
im2,ctrs, hier = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_SIMPLE)

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = img[y:y+h, x:x+w]

    # show ROI
    #cv2.imwrite('roi_imgs.png', roi)
    cv2.imshow('charachter'+str(i), roi)
    cv2.rectangle(img,(x,y),( x + w, y + h ),(90,0,255),2)
    cv2.waitKey(0)

cv2.imshow('marked areas',img)
cv2.waitKey(0)