车牌尺寸

# 1. 导入所需库 import cv2 import numpy as np import matplotlib.pyplot as plt from skimage.feature import hog, local_binary_pattern from skimage import exposure import pytesseract # 2. 配置Tesseract路径(若未添加到环境变量需手动指定) # pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # Windows示例 # pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Linux示例 # pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract' # Mac示例 # 3. 加载并预处理图像 def preprocess_image(image_path): # 读取图像 image = cv2.imread(image_path) if image is None: raise ValueError("无法读取图像,请检查路径是否正确") # 灰度化处理 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 高斯滤波去噪(核大小(5,5),标准差0) blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0) return image, gray_image, blurred_image # 4. 提取HOG特征 def extract_hog_features(blurred_image): # 计算HOG特征(方向数9,细胞大小(8,8),块大小(2,2)) fd, hog_image = hog( blurred_image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True, multichannel=False ) # 调整HOG图像强度以适应显示 hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10)) return fd, hog_image_rescaled # 5. 提取LBP特征 def extract_lbp_features(blurred_image): # 计算LBP特征(邻域点数8,半径1,统一模式) lbp_image = local_binary_pattern(blurred_image, P=8, R=1, method='uniform') # 转换为uint8格式以适应显示 lbp_image = lbp_image.astype(np.uint8) return lbp_image # 6. Haar特征检测车牌区域 def detect_license_plate(gray_image, original_image): # 加载预训练的车牌级联分类器(俄罗斯车牌训练集,适用于多数车牌类型) car_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_russian_plate_number.xml') # 检测车牌区域 plates = car_cascade.detectMultiScale( gray_image, scaleFactor=1.1, # 缩放因子 minNeighbors=5, # 相邻检测框阈值 minSize=(30, 30) # 最小车牌尺寸 ) # 在原图上绘制车牌边框并提取车牌区域 detected_image = original_image.copy() plate_regions = [] for (x, y, w, h) in plates: # 绘制绿色边框(厚度2) cv2.rectangle(detected_image, (x, y), (x+w, y+h), (0, 255, 0), 2) # 提取车牌区域图像 plate_region = original_image[y:y+h, x:x+w] plate_regions.append((plate_region, x, y, w, h)) return detected_image, plate_regions # 7. OCR提取车牌文本 def extract_plate_text(plate_region): # 转换为灰度图以提高识别准确率 plate_gray = cv2.cvtColor(plate_region, cv2.COLOR_BGR2GRAY) # 二值化处理(自适应阈值,增强文本对比度) _, plate_binary = cv2.threshold(plate_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # 使用Tesseract提取文本(psm 8:将图像视为单个单词) text = pytesseract.image_to_string(plate_binary, config='--psm 8 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') # 去除空格和换行符 text = text.strip().replace(' ', '').replace('\n', '').replace('\r', '') return text, plate_binary # 8. 显示所有处理结果 def show_results(original, hog_img, lbp_img, detected_plates, plate_regions, plate_texts, plate_binaries): # 设置显示窗口大小 plt.rcParams['figure.figsize'] = (15, 10) # 创建子图 fig, axes = plt.subplots(2, 3) axes = axes.flatten() # 显示原图(转换BGR为RGB以适应matplotlib) axes[0].imshow(cv2.cvtColor(original, cv2.COLOR_BGR2RGB)) axes[0].set_title('原始图像') axes[0].axis('off') # 显示HOG特征图 axes[1].imshow(hog_img, cmap='gray') axes[1].set_title('HOG特征图') axes[1].axis('off') # 显示LBP特征图 axes[2].imshow(lbp_img, cmap='gray') axes[2].set_title('LBP特征图') axes[2].axis('off') # 显示检测到车牌的图像 axes[3].imshow(cv2.cvtColor(detected_plates, cv2.COLOR_BGR2RGB)) axes[3].set_title('车牌检测结果') axes[3].axis('off') # 显示第一个车牌区域及识别结果 if plate_regions: axes[4].imshow(cv2.cvtColor(plate_regions[0][0], cv2.COLOR_BGR2RGB)) axes[4].set_title(f'提取的车牌区域\n识别结果:{plate_texts[0]}' if plate_texts[0] else '提取的车牌区域\n未识别到文本') axes[4].axis('off') axes[5].imshow(plate_binaries[0], cmap='gray') axes[5].set_title('车牌二值化图像') axes[5].axis('off') else: axes[4].text(0.5, 0.5, '未检测到车牌', ha='center', va='center', transform=axes[4].transAxes) axes[4].axis('off') axes[5].axis('off') plt.tight_layout() plt.show() # 9. 主函数(整合所有步骤) def main(image_path): try: # 步骤1:图像预处理 original_image, gray_image, blurred_image = preprocess_image(image_path) # 步骤2:提取HOG特征 hog_fd, hog_image = extract_hog_features(blurred_image) # 步骤3:提取LBP特征 lbp_image = extract_lbp_features(blurred_image) # 步骤4:检测车牌区域 detected_plate_image, plate_regions = detect_license_plate(gray_image, original_image) # 步骤5:提取车牌文本 plate_texts = [] plate_binaries = [] for plate_region, x, y, w, h in plate_regions: text, binary = extract_plate_text(plate_region) plate_texts.append(text) plate_binaries.append(binary) print(f'检测到车牌文本:{text}') # 步骤6:显示所有结果 show_results( original_image, hog_image, lbp_image, detected_plate_image, plate_regions, plate_texts, plate_binaries ) except Exception as e: print(f"程序运行出错:{str(e)}") # 运行主函数(替换为你的交通图像路径) if __name__ == "__main__": IMAGE_PATH = r'D:\path\to\traffic_image.jpg' # 请替换为实际图像路径 main(IMAGE_PATH) 如何改进,具体步骤
12-03
在原代码中,`hog` 函数调用时使用了 `multichannels` 参数,这是一个拼写错误,并且在较新版本的 `scikit-image` 库中,该参数已被弃用,应使用 `channel_axis` 来替代。以下是改进后的代码: ```python import cv2 import numpy as np import matplotlib.pyplot as plt from skimage.feature import hog, local_binary_pattern from skimage import exposure import pytesseract # 2. 配置Tesseract路径(若未添加到环境变量需手动指定) # pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # Windows示例 # pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Linux示例 # pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract' # Mac示例 # 3. 加载并预处理图像 def preprocess_image(image_path): # 读取图像 image = cv2.imread(image_path) if image is None: raise ValueError("无法读取图像,请检查路径是否正确") # 灰度化处理 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 高斯滤波去噪(核大小(5,5),标准差0) blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0) return image, gray_image, blurred_image # 4. 提取HOG特征 def extract_hog_features(blurred_image): # 计算HOG特征(方向数9,细胞大小(8,8),块大小(2,2)) fd, hog_image = hog( blurred_image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True, channel_axis=None # 因为是灰度图像,没有通道轴,所以设置为None ) # 调整HOG图像强度以适应显示 hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10)) return fd, hog_image_rescaled # 5. 提取LBP特征 def extract_lbp_features(blurred_image): # 计算LBP特征(邻域点数8,半径1,统一模式) lbp_image = local_binary_pattern(blurred_image, P=8, R=1, method='uniform') # 转换为uint8格式以适应显示 lbp_image = lbp_image.astype(np.uint8) return lbp_image # 6. Haar特征检测车牌区域 def detect_license_plate(gray_image, original_image): # 加载预训练的车牌级联分类器(俄罗斯车牌训练集,适用于多数车牌类型) car_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_russian_plate_number.xml') # 检测车牌区域 plates = car_cascade.detectMultiScale( gray_image, scaleFactor=1.1, # 缩放因子 minNeighbors=5, # 相邻检测框阈值 minSize=(30, 30) # 最小车牌尺寸 ) # 在原图上绘制车牌边框并提取车牌区域 detected_image = original_image.copy() plate_regions = [] for (x, y, w, h) in plates: # 绘制绿色边框(厚度2) cv2.rectangle(detected_image, (x, y), (x+w, y+h), (0, 255, 0), 2) # 提取车牌区域图像 plate_region = original_image[y:y+h, x:x+w] plate_regions.append((plate_region, x, y, w, h)) return detected_image, plate_regions # 7. OCR提取车牌文本 def extract_plate_text(plate_region): # 转换为灰度图以提高识别准确率 plate_gray = cv2.cvtColor(plate_region, cv2.COLOR_BGR2GRAY) # 二值化处理(自适应阈值,增强文本对比度) _, plate_binary = cv2.threshold(plate_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # 使用Tesseract提取文本(psm 8:将图像视为单个单词) text = pytesseract.image_to_string(plate_binary, config='--psm 8 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') # 去除空格和换行符 text = text.strip().replace(' ', '').replace('\n', '').replace('\r', '') return text, plate_binary # 8. 显示所有处理结果 def show_results(original, hog_img, lbp_img, detected_plates, plate_regions, plate_texts, plate_binaries): # 设置显示窗口大小 plt.rcParams['figure.figsize'] = (15, 10) # 创建子图 fig, axes = plt.subplots(2, 3) axes = axes.flatten() # 显示原图(转换BGR为RGB以适应matplotlib) axes[0].imshow(cv2.cvtColor(original, cv2.COLOR_BGR2RGB)) axes[0].set_title('原始图像') axes[0].axis('off') # 显示HOG特征图 axes[1].imshow(hog_img, cmap='gray') axes[1].set_title('HOG特征图') axes[1].axis('off') # 显示LBP特征图 axes[2].imshow(lbp_img, cmap='gray') axes[2].set_title('LBP特征图') axes[2].axis('off') # 显示检测到车牌的图像 axes[3].imshow(cv2.cvtColor(detected_plates, cv2.COLOR_BGR2RGB)) axes[3].set_title('车牌检测结果') axes[3].axis('off') # 显示第一个车牌区域及识别结果 if plate_regions: axes[4].imshow(cv2.cvtColor(plate_regions[0][0], cv2.COLOR_BGR2RGB)) axes[4].set_title(f'提取的车牌区域\n识别结果:{plate_texts[0]}' if plate_texts[0] else '提取的车牌区域\n未识别到文本') axes[4].axis('off') axes[5].imshow(plate_binaries[0], cmap='gray') axes[5].set_title('车牌二值化图像') axes[5].axis('off') else: axes[4].text(0.5, 0.5, '未检测到车牌', ha='center', va='center', transform=axes[4].transAxes) axes[4].axis('off') axes[5].axis('off') plt.tight_layout() plt.show() # 9. 主函数(整合所有步骤) def main(image_path): try: # 步骤1:图像预处理 original_image, gray_image, blurred_image = preprocess_image(image_path) # 步骤2:提取HOG特征 hog_fd, hog_image = extract_hog_features(blurred_image) # 步骤3:提取LBP特征 lbp_image = extract_lbp_features(blurred_image) # 步骤4:检测车牌区域 detected_plate_image, plate_regions = detect_license_plate(gray_image, original_image) # 步骤5:提取车牌文本 plate_texts = [] plate_binaries = [] for plate_region, x, y, w, h in plate_regions: text, binary = extract_plate_text(plate_region) plate_texts.append(text) plate_binaries.append(binary) print(f'检测到车牌文本:{text}') # 步骤6:显示所有结果 show_results( original_image, hog_image, lbp_image, detected_plate_image, plate_regions, plate_texts, plate_binaries ) except Exception as e: print(f"程序运行出错:{str(e)}") # 运行主函数(替换为你的交通图像路径) if __name__ == "__main__": IMAGE_PATH = r'D:\path\to\traffic_image.jpg' # 请替换为实际图像路径 main(IMAGE_PATH) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值