本项目仅是初学者学习pytorch过程中随手写的,有些地方不够严谨请谅解,仅供参考
import cv2
import numpy as np
from PIL import Image
img_init = cv2.imread(self.carlicense_content) # 加载原始图片
img_resize = cv2.resize(img_init, dsize=(1000, 600), fx=0, fy=0) # 重置图片尺寸
img_blur = cv2.blur(img_resize, (2, 2)) # 对图片进行降噪
lower_limi = np.array([80, 30, 0]) # 设置色域上下界
upper_limi = np.array([230, 110, 70])
img_limi = cv2.inRange(img_blur, lower_limi, upper_limi) # 选出指定色域(二值化,黑色:0,白色:255)
cont = cv2.findContours(img_limi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] # 找出色域外边界
c = max(cont, key=cv2.contourArea) # 在边界中找最大面积的区域
rect = cv2.minAreaRect(c) # 绘制出该区域最小外接矩形
box = cv2.boxPoints(rect) # 记录上述矩形四点坐标
def mouse_callback(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDBLCLK:
print('BGR:', img_resize[y, x], 'x:', x, 'y:', y)
cv2.setMouseCallback('IMG', mouse_callback)
cv2.namedWindow('IMG', cv2.WINDOW_NORMAL)
cv2.imshow('IMG', img_resize)
cv2.waitKey(0)
img_final = img_resize[int(min(box[1][1], box[2][1]))
:int(max(box[0][1], box[3][1]))
, int(min(box[0][0], box[1][0]))
:int(max(box[2][0], box[3][0]))] # 圈出车牌
img_final_resize = cv2.resize(img_final, dsize=(200, 50), fx=0, fy=0)
lower_limi2 = np.array([100, 100, 100]) # 设置色域上下界
upper_limi2 = np.array([255, 255, 255])
img_final_bin = cv2.inRange(img_final_resize, lower_limi2, upper_limi2)