2301_77406127 2023-10-03 20:59 采纳率: 66.7%
浏览 15
已结题

给车牌识别代码加一个识别轮廓长宽比的代码

下面这个代码是关于从照片提取车牌并识别的 能不能在筛选轮廓那里加一段筛选轮廓长宽比在2.5到4的代码


import cv2
import imutils
import pytesseract
import numpy as np
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
original_image = cv2.imread('img.jpg')
original_image = imutils.resize(original_image, width=500 )
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
gray_image = cv2.bilateralFilter(gray_image, 11, 17, 17)
edged_image = cv2.Canny(gray_image, 30,200)
contours, new = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
img1 = original_image.copy()
cv2.drawContours(img1, contours, -1, (0, 255, 0), 3)
cv2.imshow("img1", img1)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:30]
screenCnt = None
img2 = original_image.copy()
cv2.drawContours(img2, contours, -1, (0, 255, 0), 3)
cv2.imshow("img2", img2)
count = 0
idx = 7
car_contours = []

for c in contours:
    contour_perimeter = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.018 * contour_perimeter, True)

    if len(approx) == 4:
        screenCnt = approx

        x, y, w, h = cv2.boundingRect(c)
        new_img = original_image[y: y + h, x: x + w]

        cv2.imwrite('./' + str(idx) + '.png', new_img)
        idx += 1
        break

cv2.drawContours(original_image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("detected license plate", original_image)

cropped_License_Plate = './7.png'
cv2.imshow("cropped license plate", cv2.imread(cropped_License_Plate))

text = pytesseract.image_to_string(cropped_License_Plate, lang='eng')
print("License plate is:", text)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 写回答

7条回答 默认 最新

  • B64A-消闲 2023-10-05 14:21
    关注
    import cv2
    import imutils
    import pytesseract
    
    pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
    original_image = cv2.imread('img.jpg')
    original_image = imutils.resize(original_image, width=500)
    gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
    gray_image = cv2.bilateralFilter(gray_image, 11, 17, 17)
    edged_image = cv2.Canny(gray_image, 30, 200)
    contours, new = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    img1 = original_image.copy()
    cv2.drawContours(img1, contours, -1, (0, 255, 0), 3)
    cv2.imshow("img1", img1)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:30]
    screenCnt = None
    img2 = original_image.copy()
    cv2.drawContours(img2, contours, -1, (0, 255, 0), 3)
    cv2.imshow("img2", img2)
    count = 0
    idx = 7
    car_contours = []
    
    for c in contours:
        contour_perimeter = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.018 * contour_perimeter, True)
    
        if len(approx) == 4:
            x, y, w, h = cv2.boundingRect(c)
            aspect_ratio = float(w) / h
    
            if 2.5 <= aspect_ratio <= 4:
                screenCnt = approx
    
                new_img = original_image[y: y + h, x: x + w]
                cv2.imwrite('./' + str(idx) + '.png', new_img)
                idx += 1
                break
    
    cv2.drawContours(original_image, [screenCnt], -1, (0, 255, 0), 3)
    cv2.imshow("detected license plate", original_image)
    
    cropped_License_Plate = './7.png'
    cv2.imshow("cropped license plate", cv2.imread(cropped_License_Plate))
    
    text = pytesseract.image_to_string(cropped_License_Plate, lang='eng')
    print("License plate is:", text)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 已采纳回答 10月6日
  • 创建了问题 10月3日