一 设计目的
二 设计内容和要求
1、原始数据:
2、技术参数与条件:
3、设计要求:
三 项目展示
这是选择“来自图片”按钮后,选择图片进行识别,当选择“来自摄像头”按钮时,左侧图像区域会实时展示摄像头所获得的视频流,并对其逐帧识别并更新信息在右侧识别区域。
此处为输入图像后,对于图像的形状预处理图像
这是程序识别的后台反馈
四 主要原理和实现过程
1、图像预处理技术:
2、车牌定位技术:
3、字符分割技术:
4、特征提取与字符识别技术:
(1)HOG(方向梯度直方图)
(2)SVM(支持向量机)
5、系统整体运行过程

“来自图片”模式
在“来自图片”模式下,用户通过文件选择对话框挑选一张包含车牌的图片。系统首先调用 img_math.img_read
方法读取选定图片,接着由 CardPredictor
类的 img_first_pre
方法进行初步预处理,包括图像的大小调整、降噪以及初步定位等,以确保图像质量满足后续处理的需求。
随后,系统并发地执行颜色轮廓检测和颜色区域定位。这两个任务分别由 img_color_contours
和 img_only_color
方法实现,并通过多线程技术来加速处理过程。完成定位后,系统根据颜色信息和形状分析,从原始图像中裁剪出车牌区域,并对这些区域进行更细致的处理,包括字符分割及抗干扰处理等。
接下来,利用预训练的支持向量机(SVM)模型对分割后的字符进行 HOG 特征提取并进行识别。针对英文数字和中文字符,系统使用不同的模型。识别出的字符结果将在用户界面中展示,并且会更新相关标签以反映识别状态和颜色信息,从而提供直观的用户反馈。
“来自摄像头”模式
当切换到“来自摄像头”模式时,系统初始化摄像头并开始实时视频流的捕获。每帧图像都会通过 vedio_thread
静态方法进行处理,该方法不断读取摄像头获取的图像帧,并进行与图片识别类似的预处理、车牌定位、字符识别等一系列操作。
为了确保实时性,系统在处理图像时会控制频率,每两秒进行一次识别更新。通过优化的算法参数配置和高效的图像处理技术,系统能够在有限的时间内完成对视频流中车牌的连续识别。识别结果会在 GUI 界面上即时展示,用户可以直观地看到车牌识别的动态过程。
五 项目代码
1、main.py
这段主要是包含了
''' 1、ThreadWithReturnValue类 创建可以返回结果的线程 2、Surface类 负责构建GUI界面及处理图像识别逻辑 __init__:初始化界面布局 get_imgtk方法:图像格式转换,并根据视窗大小调整图像尺寸 how_roi1和show_roi2方法:在界面上显示识别出的车牌区域和结果,包括字符识别和颜色分析的结果 show_img_pre:展示预处理图像,用于调试 from_pic 从图片中识别车牌 from_vedio 从摄像头中识别车牌 vedio_thread 视频流处理的线程主体,每隔一定时间间隔执行图像预处理、字符轮廓识别和颜色识别,并在界面上更新结果。 close_window 窗口关闭时的回调函数以妥善处理线程资源 3、主函数 '''
# -*- coding: utf-8 -*-
import threading # 用于多线程处理
import time
import tkinter as tk
import cv2 # 用于图像处理
import config
import zhongjiantuxiang
import img_function as predict
import img_math
from threading import Thread
from tkinter import ttk
from tkinter.filedialog import *
from PIL import Image, ImageTk # 用于图形转换
from tkinter import messagebox # 摄像头打不开后弹出提醒
# 引入Tkinter的常量 用于界面布局
from tkinter.constants import LEFT, BOTH, RIGHT, TOP, W, Y, NW, SE
# 继承自Thread的ThreadWithReturnValue类,用于创建可以返回结果的线程
class ThreadWithReturnValue(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None):
Thread.__init__(self, group, target, name, args, kwargs, daemon=daemon)
self._return1 = None
self._return2 = None
self._return3 = None
# 确定了三个实例对象用于存储线程执行后的返回值
def run(self):
if self._target is not None:
self._return1, self._return2, self._return3 = self._target(*self._args, **self._kwargs)
def join(self):
Thread.join(self)
return self._return1, self._return2, self._return3
# Surface类是Tkinter框架的扩展,负责构建GUI界面及处理图像识别逻辑
class Surface(ttk.Frame):
pic_path = ""
viewhigh = 600
viewwide = 600
update_time = 0
thread = None
thread_run = False
camera = None
color_transform = {"green": ("绿牌", "#55FF55"), "yello": ("黄牌", "#FFFF00"), "blue": ("蓝牌", "#6666FF")}
# 构造函数__init__:初始化界面布局
def __init__(self, win):
# 初始化: __init__方法初始化一个窗口(win)
# 并在其中设置了多个框架(frame_left, frame_right1, frame_right2)以组织布局。
# 窗口标题被设为"车牌识别",并且窗口初始状态被最大化(win.state("zoomed"))
ttk.Frame.__init__(self, win)
frame_left = ttk.Frame(self)
frame_right1 = ttk.Frame(self)
frame_right2 = ttk.Frame(self)
win.title("车牌识别")
win.state("zoomed")
# 布局设置:
# frame_left: 用于显示原始图片。
# frame_right1: 分为两部分,上部用于展示通过形状定位的车牌位置,下部展示识别结果和相关信息(如识别出的字符、颜色等)。
# frame_right2: 包含按钮,用于从图片或摄像头中获取图像,以及查看预处理后的图像。
self.pack(fill=tk.BOTH, expand=tk.YES, padx="10", pady="10")
frame_left.pack(side=LEFT, expand=1, fill=BOTH)
frame_right1.pack(side=TOP, expand=1, fill=tk.Y)
frame_right2.pack(side=RIGHT, expand=0)
# 控件创建:
# 文本标签(ttk.Label)用于描述各个部分的功能。
# 按钮(ttk.Button)分别用于触发从图片读取、从摄像头捕获图像和查看预处理图像的操作。
# 图像显示标签(self.image_ctl和self.roi_ctl) 用于展示原始图片和识别的车牌区域。
# 文本标签(self.r_ctl, self.color_ctl, self.roi_ct2, self.r_ct2, self.color_ct2)显示识别结果和车牌颜色等信息。
ttk.Label(frame_left, text='原图:').pack(anchor="nw") # 文本标签
ttk.Label(frame_right1, text='车牌位置:').grid(column=0, row=0, sticky=tk.W)# 文本标签
ttk.Label(frame_right1, text='形状定位识别结果:').grid(column=0, row=2, sticky=tk.W)# 文本标签
# ttk.Label(frame_right1, text='颜色定位车牌位置:').grid(column=0, row=5, sticky=tk.W)
ttk.Label(frame_right1, text='颜色定位识别结果:').grid(column=0, row=7, sticky=tk.W)
from_pic_ctl = ttk.Button(frame_right2, text="来自图片", width=20, command=self.from_pic) # 按钮功能绑定
from_vedio_ctl = ttk.Button(frame_right2, text="来自摄像头", width=20, command=self.from_vedio) # 按钮功能绑定
from_img_pre = ttk.Button(frame_right2, text="查看形状预处理图像", width=20, command=self.show_img_pre) # 按钮功能绑定
self.image_ctl = ttk.Label(frame_left) # 原始图像创建
self.image_ctl.pack(anchor="nw") # 原始图像放置
self.roi_ctl = ttk.Label(frame_right1) # 创建标签用于显示“形状定位车牌位置”的结果
self.roi_ctl.grid(column=0, row=1, sticky=tk.W) # 标签放置
self.r_ctl = ttk.Label(frame_right1, text="", font=('Times', '20')) # 创建空标签用于显示“形状定位识别结果”
self.r_ctl.grid(column=0, row=3, sticky=tk.W)
self.color_ctl = ttk.Label(frame_right1, text="", width="20") # 显示颜色相关的识别信息
self.color_ctl.grid(column=0, row=4, sticky=tk.W)
self.roi_ct2 = ttk.Label(frame_right1) # 展示“颜色定位车牌位置”的结果
# self.roi_ct2.grid(column=0, row=6, sticky=tk.W)
self.r_ct2 = ttk.Label(frame_right1, text="", font=('Times', '20')) # 用于显示“颜色定位识别结果”
self.r_ct2.grid(column=0, row=8, sticky=tk.W)
self.color_ct2 = ttk.Label(frame_right1, text="",width="20") # 显示与颜色定位相关的额外信息或结果
self.color_ct2.grid(column=0, row=9, sticky=tk.W)
from_vedio_ctl.pack(anchor="se", pady="5") # 按钮
from_pic_ctl.pack(anchor="se", pady="5") # 按钮
from_img_pre.pack(anchor="se", pady="5") # 按钮
# 初始化预测器:
self.predictor = predict.CardPredictor()
self.predictor.train_svm()
# get_imgtk方法:将OpenCV格式的图像转换为Tkinter可显示的图像,并根据视窗大小调整图像尺寸。
def get_imgtk(self, img_bgr):
img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
im = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=im)
wide = imgtk.width()
high = imgtk.height()
if wide > self.viewwide or high > self.viewhigh:
wide_factor = self.viewwide / wide
high_factor = self.viewhigh / high
factor = min(wide_factor, high_factor)
wide = int(wide * factor)
if wide <= 0: wide = 1
high = int(high * factor)
if high <= 0: high = 1
im = im.resize((wide, high), Image.LANCZOS) ####
imgtk = ImageTk.PhotoImage(image=im)
return imgtk
# show_roi1和show_roi2方法:在界面上显示识别出的车牌区域和结果,包括字符识别和颜色分析的结果
def show_roi1(self, r, roi, color):
if r:
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
roi = Image.fromarray(roi)
self.imgtk_roi = ImageTk.PhotoImage(image=roi)
self.roi_ctl.configure(image=self.imgtk_roi, state='enable')
self.r_ctl.configure(text=str(r))
self.update_time = time.time()
try:
c = self.color_transform[color]
self.color_ctl.configure(text=c[0], background=c[1], state='enable')
except:
self.color_ctl.configure(state='disabled')
elif self.update_time + 8 < time.time():
self.roi_ctl.configure(state='disabled')
self.r_ctl.configure(text="")
self.color_ctl.configure(state='disabled')
def show_roi2(self, r, roi, color):
if r:
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
roi = Image.fromarray(roi)
self.imgtk_roi = ImageTk.PhotoImage(image=roi)
self.roi_ct2.configure(image=self.imgtk_roi, state='enable')
self.r_ct2.configure(text=str(r))
self.update_time = time.time()
try:
c = self.color_transform[color]
self.color_ct2.configure(text=c[0], background=c[1], state='enable')
except:
self.color_ct2.configure(state='disabled')
elif self.update_time + 8 < time.time():
self.roi_ct2.configure(state='disabled')
self.r_ct2.configure(text="")
self.color_ct2.configure(state='disabled')
# show_img_pre:展示预处理图像,用于调试。
def show_img_pre(self):
filename = config.get_name()
if filename.any():
zhongjiantuxiang.img_show(filename)
# 从图片中识别车牌
def from_pic(self):
self.thread_run = False
self.pic_path = askopenfilename(title="选择识别图片", filetypes=[("jpg图片", "*.jpg"), ("png图片", "*.png")])
if self.pic_path:
img_bgr = img_math.img_read(self.pic_path)
first_img, oldimg = self.predictor.img_first_pre(img_bgr)# 原始图像预处理
self.imgtk = self.get_imgtk(img_bgr)# 原始图像改格式并在界面显示
self.image_ctl.configure(image=self.imgtk)# 更新界面图象
th1 = ThreadWithReturnValue(target=self.predictor.img_color_contours, args=(first_img, oldimg))# 线程1识别图像中的字符轮廓
th2 = ThreadWithReturnValue(target=self.predictor.img_only_color, args=(oldimg, oldimg, first_img))# 线程2 单独分析图像的颜色信息
th1.start()# 启动线程
th2.start()
r_c, roi_c, color_c = th1.join()# 从th1和th2获取返回值
r_color, roi_color, color_color = th2.join()# 从th1和th2获取返回值
print(r_c, r_color)
self.show_roi2(r_color, roi_color, color_color)# 在GUI中显示与颜色分析相关的结果
self.show_roi1(r_c, roi_c, color_c)# 在GUI中显示字符识别的结果
# 从摄像头中识别车牌
def from_vedio(self):
if self.thread_run:
return
if self.camera is None:
self.camera = cv2.VideoCapture(0)
if not self.camera.isOpened():
messagebox.showwarning('警告', '摄像头打开失败!')
self.camera = None
return
self.thread = threading.Thread(target=self.vedio_thread, args=(self,))
self.thread.setDaemon(True)
self.thread.start()
self.thread_run = True
# 视频流处理的线程主体,每隔一定时间间隔执行图像预处理、字符轮廓识别和颜色识别,并在界面上更新结果。
@staticmethod
def vedio_thread(self):
self.thread_run = True
predict_time = time.time()
while self.thread_run:
_, img_bgr = self.camera.read()
self.imgtk = self.get_imgtk(img_bgr)
self.image_ctl.configure(image=self.imgtk)
if time.time() - predict_time > 2:
first_img, oldimg = self.predictor.img_first_pre(img_bgr) # 原始图像预处理
self.imgtk = self.get_imgtk(img_bgr) # 原始图像改格式并在界面显示
self.image_ctl.configure(image=self.imgtk) # 更新界面图象
th1 = ThreadWithReturnValue(target=self.predictor.img_color_contours, args=(first_img, oldimg)) # 线程1识别图像中的字符轮廓
th2 = ThreadWithReturnValue(target=self.predictor.img_only_color, args=(oldimg, oldimg, first_img)) # 线程2 单独分析图像的颜色信息d
th1.start() # 启动线程1
th2.start() # 启动线程2
r_c, roi_c, color_c = th1.join() # 从th1和th2获取返回值
r_color, roi_color, color_color = th2.join() # 从th1和th2获取返回值
print(r_c, r_color)
self.show_roi2(r_color, roi_color, color_color) # 在GUI中显示与颜色分析相关的结果
self.show_roi1(r_c, roi_c, color_c) # 在GUI中显示字符识别的结果
# r, roi, color = self.predictor.img_color_contours(img_bgr)
# self.show_roi(r, roi, color)
# predict_time = time.time()
print("run end")
# 窗口关闭时的回调函数close_window以妥善处理线程资源
def close_window():
print("destroy")
if surface.thread_run:
surface.thread_run = False
surface.thread.join(2.0)
win.destroy()
# 主函数
if __name__ == '__main__':
win = tk.Tk()
surface = Surface(win)
# close,退出输出destroy
win.protocol('WM_DELETE_WINDOW', close_window)
# 进入消息循环
win.mainloop()
2、img_function.py
'''' 1、StatModel是一个基类,定义了模型加载和保存的接口 2、SVM类继承自StatModel,实现了基于OpenCV的SVM(支持向量机)模型,用于字符识别。【SVM支持向量机】 3、CardPredictor类负责车牌的识别流程,包括训练SVM模型、预处理图像、识别车牌字符和颜色。 train_svm方法用于训练英文数字和中文省份简称的SVM模型 save_traindata保存训练好的模型数据 img_first_pre对原始图像进行初步预处理 img_color_contours预处理后的图像上寻找车牌轮廓,并进一步识别车牌字符和颜色。 img_only_color仅通过颜色信息来定位和识别车牌 img_mser通过MSER方法检测图像中可能包含车牌的矩形区域 '''
# -*- coding: utf-8 -*-
import os
import cv2
import numpy as np
import zhongjiantuxiang
import img_math
import img_recognition
import config
SZ = 20 # 训练图片长宽
MAX_WIDTH = 1000 # 原始图片最大宽度
Min_Area = 2000 # 车牌区域允许最大面积
PROVINCE_START = 1000 # 省份标识符的起始编号
# StatModel是一个基类,定义了模型加载和保存的接口。
class StatModel(object):
def load(self, fn):
self.model = self.model.load(fn)
def save(self, fn):
self.model.save(fn)
# SVM继承自StatModel,实现了基于OpenCV的SVM(支持向量机)模型,用于字符识别。
# 它支持设置参数C(正则化参数)和gamma(核函数系数)
#!!!!!! SVM的类型为C-SVC(支持多分类),核函数为RBF(径向基函数)
class SVM(StatModel):
# 初始化方法
# 通过cv2.ml.SVM_create()创建SVM模型实例,
# 然后设置模型参数,包括核函数类型为RBF(高斯核),SVM类型为C - SVC(支持向量分类,处理多分类问题),
# 以及设置C和gamma的值。
def __init__(self, C=1, gamma=0.5):
self.model = cv2.ml.SVM_create()
self.model.setGamma(gamma)
self.model.setC(C)
self.model.setKernel(cv2.ml.SVM_RBF)
self.model.setType(cv2.ml.SVM_C_SVC)
# 训练svm
# samples:训练样本集,通常是特征向量的集合。在图像识别任务中,这可能是一系列经过特征提取(如HOG, LBP等)后的图像数据。
# responses:样本对应的标签或类别,与样本一一对应。对于字符识别,这将是每个样本所代表的字符标签(如数字或字母编码)。
# cv2.ml.ROW_SAMPLE:指定样本是以行向量形式提供的。
def train(self, samples, responses):
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
# 字符识别
# samples:待预测的特征向量,结构与训练样本相同。
# self.model.predict(samples):调用模型的预测方法,返回预测结果。
# r[1].ravel():predict返回一个元组,其中第一个元素是预测结果的类型,第二个元素是预测标签的数组。ravel()用于将多维数组拉平为一维,方便获取预测的类别标签。
def predict(self, samples):
r = self.model.predict(samples)
return r[1].ravel()
# 负责车牌的识别流程,包括训练SVM模型、预处理图像、识别车牌字符和颜色。
class CardPredictor:
def __init__(self):
pass
def __del__(self):
self.save_traindata()
# train_svm方法用于训练英文数字和中文省份简称的SVM模型。
# 它遍历特定目录下的图像文件,读取图像,进行预处理(灰度化、倾斜校正、HOG特征提取),
# 然后用这些特征和对应的标签训练SVM模型。
def train_svm(self):
# ## 初始化SVM模型
# 识别英文字母和数字
self.model = SVM(C=1, gamma=0.5)
# 识别中文
self.modelchinese = SVM(C=1, gamma=0.5)
# ## 加载或训练英文/数字模型
# # 主要功能是检查是否存在预训练好的SVM模型文件("svm.dat"),并根据情况决定是加载模型还是重新训练模型
if os.path.exists("svm.dat"):
self.model.load("svm.dat")
else:
chars_train = [] # 存放待训练的图像样本
chars_label = [] # 存放这些样本对应的标签
for root, dirs, files in os.walk("train\\chars2"):
if len(os.path.basename(root)) > 1:
continue
# 只处理单层目录结构,每个目录对应一个类别
# 处理每个训练样本
# 对于符合条件的目录,将basename转换为整数(利用ASCII码值)
# 然后遍历该目录下的所有文件。读取每个文件(图像),将其转换为灰度图像
# 并将图像添加到chars_train列表中
# 同时将对应的类别标签(基于目录名的ASCII值)添加到chars_label列表中
root_int = ord(os.path.basename(root))
for filename in files:
filepath = os.path.join(root, filename)
digit_img = cv2.imread(filepath)
digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY)
chars_train.append(digit_img)
# chars_label.append(1)
chars_label.append(root_int)
# ## 特征预处理
chars_train = list(map(img_recognition.deskew, chars_train))
chars_train = img_recognition.preprocess_hog(chars_train)
# chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
chars_label = np.array(chars_label)
print(chars_train.shape)
self.model.train(chars_train, chars_label)
# ## 加载或训练中文模型
if os.path.exists("svmchinese.dat"):
self.modelchinese.load("svmchinese.dat")
else:
chars_train = []
chars_label = []
for root, dirs, files in os.walk("train\\charsChinese"):
if not os.path.basename(root).startswith("zh_"):
continue
pinyin = os.path.basename(root)
index = img_recognition.provinces.index(pinyin) + PROVINCE_START + 1 # 1是拼音对应的汉字
for filename in files:
filepath = os.path.join(root, filename)
digit_img = cv2.imread(filepath)
digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY)
chars_train.append(digit_img)
# chars_label.append(1)
chars_label.append(index)
# ## 特征预处理
chars_train = list(map(img_recognition.deskew, chars_train))
chars_train = img_recognition.preprocess_hog(chars_train)
# chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
chars_label = np.array(chars_label)
print(chars_train.shape)
self.modelchinese.train(chars_train, chars_label)
# 保存训练好的模型数据
def save_traindata(self):
if not os.path.exists("svm.dat"):
self.model.save("svm.dat")
if not os.path.exists("svmchinese.dat"):
self.modelchinese.save("svmchinese.dat")
# 对原始图像进行初步预处理,包括调整大小、高斯模糊、灰度化、开运算和边缘检测等,为后续的车牌定位做准备
def img_first_pre(self, car_pic_file):
"""
:param car_pic_file: 图像文件
:return:已经处理好的图像文件 原图像文件
"""
if type(car_pic_file) == type(""):
img = img_math.img_read(car_pic_file)
else:
img = car_pic_file
pic_hight, pic_width = img.shape[:2]
if pic_width > MAX_WIDTH:
resize_rate = MAX_WIDTH / pic_width
img = cv2.resize(img, (MAX_WIDTH, int(pic_hight * resize_rate)), interpolation=cv2.INTER_AREA)
# 缩小图片
blur = 5
img = cv2.GaussianBlur(img, (blur, blur), 0)
oldimg = img
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 转化成灰度图像
Matrix = np.ones((20, 20), np.uint8)
img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, Matrix)
img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0)
# 创建20*20的元素为1的矩阵 开操作,并和img重合
ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
img_edge = cv2.Canny(img_thresh, 100, 200)
# Otsu’s二值化 找到图像边缘
Matrix = np.ones((4, 19), np.uint8)
img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, Matrix)
img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix)
return img_edge2, oldimg
# 在预处理后的图像上寻找车牌轮廓,并进一步识别车牌字符和颜色。
# 它使用形态学操作、阈值分割、波峰检测等技术来定位字符区域,并调用SVM模型进行字符识别。
def img_color_contours(self, img_contours, oldimg):
"""
:param img_contours: 预处理好的图像
:param oldimg: 原图像
:return: 已经定位好的车牌
"""
# # 轮廓有效性检查
if img_contours.any():
config.set_name(img_contours) # 轮廓检查与配置设置
# # 获取图像尺寸
pic_hight, pic_width = img_contours.shape[:2] # 获取图像尺寸
# # 查找车牌轮廓
card_contours = img_math.img_findContours(img_contours)
# # 图像转换与调整
card_imgs = img_math.img_Transform(card_contours, oldimg, pic_width, pic_hight)
# # 获取颜色与图像
colors, car_imgs = img_math.img_color(card_imgs)
# # 初始化
predict_result = []
roi = None
card_color = None
# # 循环处理颜色
for i, color in enumerate(colors):
if color in ("blue", "yello", "green"):
card_img = card_imgs[i]
# ## 灰度转换
gray_img = cv2.cvtColor(card_img, cv2.COLOR_BGR2GRAY) # 选取对应的车牌图像card_img并转换为灰度图像
# ## 颜色反向 黄、绿车牌字符比背景暗、与蓝车牌刚好相反,所以黄、绿车牌需要反向
if color == "green" or color == "yello":
gray_img = cv2.bitwise_not(gray_img)
# ## 二值化处理
ret, gray_img = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# ## 计算水平直方图
x_histogram = np.sum(gray_img, axis=1)
# ## 车牌区域定位
# 计算波峰阈值
x_min = np.min(x_histogram)
x_average = np.sum(x_histogram) / x_histogram.shape[0]
x_threshold = (x_min + x_average) / 2
# 寻找波峰
wave_peaks = img_math.find_waves(x_threshold, x_histogram)
# 处理没有波峰的状态
if len(wave_peaks) == 0:
print("peak less 0:")
continue
# 选择最大波峰区域 认为水平方向,最大的波峰为车牌区域
wave = max(wave_peaks, key=lambda x: x[1] - x[0])
gray_img = gray_img[wave[0]:wave[1]] # 车牌区域定位:在水平直方图中找到波峰(字符间的高峰),并选择宽度最大的波峰作为车牌的主要区域,进而裁剪图像以仅保留该区域。
# 处理垂直方向的直方图:
row_num, col_num = gray_img.shape[:2] # 获取裁剪后图像的行数(row_num)和列数(col_num)
gray_img = gray_img[1:row_num - 1]# 去掉车牌上下边缘1个像素,避免白边影响阈值判断
y_histogram = np.sum(gray_img, axis=0) # 计算垂直方向的直方图
y_min = np.min(y_histogram)
y_average = np.sum(y_histogram) / y_histogram.shape[0]
y_threshold = (y_min + y_average) / 5 # 计算阈值 U和0要求阈值偏小,否则U和0会被分成两半
# 再次处理波峰数量
wave_peaks = img_math.find_waves(y_threshold, y_histogram)
if len(wave_peaks) <= 6:
print("peak less 1:", len(wave_peaks))
continue
# 优化波峰选择:
wave = max(wave_peaks, key=lambda x: x[1] - x[0])
max_wave_dis = wave[1] - wave[0]
# 判断是否是左侧车牌边缘 即它很窄且位置在图像最左侧。如果是,则从波峰列表中移除这个波峰,避免将其误识别为字符
if wave_peaks[0][1] - wave_peaks[0][0] < max_wave_dis / 3 and wave_peaks[0][0] == 0:
wave_peaks.pop(0)
# ##组合分离汉字
# 通过合并疑似相连的字符波峰,并排除不合理的波峰,来优化字符分割,以提高车牌字符识别的准确率
cur_dis = 0 # 累加字符间隔的距离
for i, wave in enumerate(wave_peaks): # 遍历所有波峰
# enumerate()函数被用来遍历列表wave_peaks,它同时提供每个元素的索引(i)和值(wave)。i从0开始,随着循环的每次迭代递增,直到遍历完列表中的所有波峰。
# wave则是当前迭代的元素,即一个表示波峰起始和结束位置的元组
if wave[1] - wave[0] + cur_dis > max_wave_dis * 0.6: # 当前波峰与前一个波峰间的距离过大
break
else:
cur_dis += wave[1] - wave[0] # 累加当前波峰的宽度到cur_dis,用于计算累积距离
if i > 0:
wave = (wave_peaks[0][0], wave_peaks[i][1])
wave_peaks = wave_peaks[i + 1:]
wave_peaks.insert(0, wave)
point = wave_peaks[2]
point_img = gray_img[:, point[0]:point[1]]
if np.mean(point_img) < 255 / 5:
wave_peaks.pop(2)
if len(wave_peaks) <= 6:
print("peak less 2:", len(wave_peaks))
continue
part_cards = img_math.seperate_card(gray_img, wave_peaks) # 字符分割:基于处理后的波峰列表,将车牌区域分割成单个字符的图像part_cards
# ## 字符识别:对每一个分割出的字符图像进行预处理(如调整大小、增加边框使其符合模型输入要求),
# 然后使用预先训练好的模型(对于中文字符使用modelchinese,对于数字和英文字母使用model)进行识别,
# 并将识别结果添加到predict_result列表中。
for i, part_card in enumerate(part_cards):
# 检查并排除非字符区域 可能是固定车牌的铆钉
if np.mean(part_card) < 255 / 5:
print("a point")
continue
# 备份原字符区域
part_card_old = part_card
# 调整字符区域尺寸
w = abs(part_card.shape[1] - SZ) // 2
part_card = cv2.copyMakeBorder(part_card, 0, 0, w, w, cv2.BORDER_CONSTANT, value=[0, 0, 0]) # 调整字符区域尺寸
part_card = cv2.resize(part_card, (SZ, SZ), interpolation=cv2.INTER_AREA) # 调整图像尺寸
part_card = img_recognition.preprocess_hog([part_card]) # 特征提取
# 字符分类预测
if i == 0: # 根据字符索引i判断是否为第一位字符
resp = self.modelchinese.predict(part_card) # 预测
charactor = img_recognition.provinces[int(resp[0]) - PROVINCE_START] # 将预测结果转换为实际字符,对于中文字符,还需根据省份索引映射实际省份名称
else:
resp = self.model.predict(part_card) # 预测
charactor = chr(resp[0]) # 将预测结果转换为实际字符
# 排除边缘误识别
if charactor == "1" and i == len(part_cards) - 1: # 识别出的字符是否为"1"且是否为最后一个字符
if part_card_old.shape[0] / part_card_old.shape[1] >= 7: # 检查该字符区域的长宽比,如果过细(高度是宽度的7倍以上),则认为这是一个边缘效应
continue
# 将正确识别的字符追加到predict_result列表中
predict_result.append(charactor)
roi = card_img # 车牌图像
card_color = color # 车牌颜色
break
return predict_result, roi, card_color # 识别到的字符、定位的车牌图像、车牌颜色
# 仅通过颜色信息来定位和识别车牌
def img_only_color(self, filename, oldimg, img_contours):
"""
:param filename: 图像文件
:param oldimg: 原图像文件
:return: 已经定位好的车牌
"""
pic_hight, pic_width = img_contours.shape[:2]
lower_blue = np.array([100, 110, 110])
upper_blue = np.array([130, 255, 255])
lower_yellow = np.array([15, 55, 55])
upper_yellow = np.array([50, 255, 255])
lower_green = np.array([50, 50, 50])
upper_green = np.array([100, 255, 255])
hsv = cv2.cvtColor(filename, cv2.COLOR_BGR2HSV)
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
mask_green = cv2.inRange(hsv, lower_yellow, upper_green)
output = cv2.bitwise_and(hsv, hsv, mask=mask_blue + mask_yellow + mask_green)
# 根据阈值找到对应颜色
output = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
Matrix = np.ones((20, 20), np.uint8)
img_edge1 = cv2.morphologyEx(output, cv2.MORPH_CLOSE, Matrix)
img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix)
card_contours = img_math.img_findContours(img_edge2)
card_imgs = img_math.img_Transform(card_contours, oldimg, pic_width, pic_hight)
colors, car_imgs = img_math.img_color(card_imgs)
predict_result = []
roi = None
card_color = None
for i, color in enumerate(colors):
if color in ("blue", "yello", "green"):
card_img = card_imgs[i]
gray_img = cv2.cvtColor(card_img, cv2.COLOR_BGR2GRAY)
# 黄、绿车牌字符比背景暗、与蓝车牌刚好相反,所以黄、绿车牌需要反向
if color == "green" or color == "yello":
gray_img = cv2.bitwise_not(gray_img)
ret, gray_img = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
x_histogram = np.sum(gray_img, axis=1)
x_min = np.min(x_histogram)
x_average = np.sum(x_histogram) / x_histogram.shape[0]
x_threshold = (x_min + x_average) / 2
wave_peaks = img_math.find_waves(x_threshold, x_histogram)
if len(wave_peaks) == 0:
print("peak less 0:")
continue
# 认为水平方向,最大的波峰为车牌区域
wave = max(wave_peaks, key=lambda x: x[1] - x[0])
gray_img = gray_img[wave[0]:wave[1]]
# 查找垂直直方图波峰
row_num, col_num = gray_img.shape[:2]
# 去掉车牌上下边缘1个像素,避免白边影响阈值判断
gray_img = gray_img[1:row_num - 1]
y_histogram = np.sum(gray_img, axis=0)
y_min = np.min(y_histogram)
y_average = np.sum(y_histogram) / y_histogram.shape[0]
y_threshold = (y_min + y_average) / 5 # U和0要求阈值偏小,否则U和0会被分成两半
wave_peaks = img_math.find_waves(y_threshold, y_histogram)
if len(wave_peaks) < 6:
print("peak less 1:", len(wave_peaks))
continue
wave = max(wave_peaks, key=lambda x: x[1] - x[0])
max_wave_dis = wave[1] - wave[0]
# 判断是否是左侧车牌边缘
if wave_peaks[0][1] - wave_peaks[0][0] < max_wave_dis / 3 and wave_peaks[0][0] == 0:
wave_peaks.pop(0)
# 组合分离汉字
cur_dis = 0
for i, wave in enumerate(wave_peaks):
if wave[1] - wave[0] + cur_dis > max_wave_dis * 0.6:
break
else:
cur_dis += wave[1] - wave[0]
if i > 0:
wave = (wave_peaks[0][0], wave_peaks[i][1])
wave_peaks = wave_peaks[i + 1:]
wave_peaks.insert(0, wave)
point = wave_peaks[2]
point_img = gray_img[:, point[0]:point[1]]
if np.mean(point_img) < 255 / 5:
wave_peaks.pop(2)
if len(wave_peaks) <= 6:
print("peak less 2:", len(wave_peaks))
continue
part_cards = img_math.seperate_card(gray_img, wave_peaks)
for i, part_card in enumerate(part_cards):
# 可能是固定车牌的铆钉
if np.mean(part_card) < 255 / 5:
print("a point")
continue
part_card_old = part_card
w = abs(part_card.shape[1] - SZ) // 2
part_card = cv2.copyMakeBorder(part_card, 0, 0, w, w, cv2.BORDER_CONSTANT, value=[0, 0, 0])
part_card = cv2.resize(part_card, (SZ, SZ), interpolation=cv2.INTER_AREA)
part_card = img_recognition.preprocess_hog([part_card]) # HOG(向梯度直方图)特征提取
if i == 0:
resp = self.modelchinese.predict(part_card)
charactor = img_recognition.provinces[int(resp[0]) - PROVINCE_START] # 省份列表
else:
resp = self.model.predict(part_card)
charactor = chr(resp[0])
# 判断最后一个数是否是车牌边缘,假设车牌边缘被认为是1
if charactor == "1" and i == len(part_cards) - 1:
if part_card_old.shape[0] / part_card_old.shape[1] >= 7: # 1太细,认为是边缘
continue
predict_result.append(charactor)
roi = card_img
card_color = color
break
return predict_result, roi, card_color # 识别到的字符、定位的车牌图像、车牌颜色
#通过MSER方法检测图像中可能包含车牌的矩形区域
def img_mser(self, filename):
if type(filename) == type(""):
img = img_math.img_read(filename)
else:
img = filename
oldimg = img
mser = cv2.MSER_create(_min_area=600)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
regions, boxes = mser.detectRegions(gray)
colors_img = []
for box in boxes:
x, y, w, h = box
width, height = w, h
if width < height:
width, height = height, width
ration = width / height
if w * h > 1500 and 3 < ration < 4 and w > h:
cropimg = img[y:y + h, x:x + w]
colors_img.append(cropimg)
zhongjiantuxiang.img_show(img)
colors, car_imgs = img_math.img_color(colors_img)
for i, color in enumerate(colors):
if color != "no":
print(color)
zhongjiantuxiang.img_show(car_imgs[i])
3、img_recognition.py
""" 1、图像矫正 deskew 2、字符图像特征提取 preprocess_hog 3、省份列表 """
import cv2
import numpy as np
from numpy.linalg import norm # 用于计算向量的范数
SZ = 20 # 训练图片长宽
MAX_WIDTH = 1000 # 原始图片最大宽度
Min_Area = 2000 # 车牌区域允许最大面积
PROVINCE_START = 1000
# 来自opencv的sample,用于svm训练
# 对输入图像进行倾斜校正
def deskew(img):
m = cv2.moments(img)
if abs(m['mu02']) < 1e-2:
return img.copy()
skew = m['mu11'] / m['mu02']
M = np.float32([[1, skew, -0.5 * SZ * skew], [0, 1, 0]])
img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
return img
# 来自opencv的sample,用于svm训练
# 对字符图像进行HOG(向梯度直方图)特征提取
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n * ang / (2 * np.pi))
bin_cells = bin[:10, :10], bin[10:, :10], bin[:10, 10:], bin[10:, 10:]
mag_cells = mag[:10, :10], mag[10:, :10], mag[:10, 10:], mag[10:, 10:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
provinces = [
"zh_cuan", "川",
"zh_e", "鄂",
"zh_gan", "赣",
"zh_gan1", "甘",
"zh_gui", "贵",
"zh_gui1", "桂",
"zh_hei", "黑",
"zh_hu", "沪",
"zh_ji", "冀",
"zh_jin", "津",
"zh_jing", "京",
"zh_jl", "吉",
"zh_liao", "辽",
"zh_lu", "鲁",
"zh_meng", "蒙",
"zh_min", "闽",
"zh_ning", "宁",
"zh_qing", "青",
"zh_qiong", "琼",
"zh_shan", "陕",
"zh_su", "苏",
"zh_sx", "晋",
"zh_wan", "皖",
"zh_xiang", "湘",
"zh_xin", "新",
"zh_yu", "豫",
"zh_yu1", "渝",
"zh_yue", "粤",
"zh_yun", "云",
"zh_zang", "藏",
"zh_zhe", "浙"
]
4、config.py
# 来实现全局变量的功能,主要是为了在不同函数或者模块间传递和共享数据
class global_var:
name = None
def set_name(name):
global_var.name = name
def get_name():
return global_var.name
5、img_math.py
1读文件函数 2限制点坐标函数 3精确定位车牌区域 4查找轮廓 5图像矫正函数 6颜色识别与精确裁剪 7波峰检测 8根据波峰分割字符 9MSER颜色识别函数
import cv2
import numpy as np
MAX_WIDTH = 1000
Min_Area = 2000
SZ = 20
PROVINCE_START = 1000
# 读取图像文件。
# 使用np.fromfile读取文件内容为字节流,再通过cv2.imdecode解码为BGR色彩空间的图像,适合大文件读取,减少内存消耗。
def img_read(filename):
return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), cv2.IMREAD_COLOR)
# 以uint8方式读取filename 放入imdecode中,cv2.IMREAD_COLOR读取彩色照片
# 限制点坐标。确保图像处理中使用的点坐标不小于0,避免索引越界。
def point_limit(point):
if point[0] < 0:
point[0] = 0
if point[1] < 0:
point[1] = 0
# 精确定位车牌区域。
# 在HSV色彩空间中,根据给定的Hue(色调)范围限制(limit1和limit2),以及饱和度和亮度的阈值,找出车牌大致区域的边界,用于后续的精确裁剪。
def accurate_place(card_img_hsv, limit1, limit2, color):
row_num, col_num = card_img_hsv.shape[:2]
xl = col_num
xr = 0
yh = 0
yl = row_num
row_num_limit = 21
col_num_limit = col_num * 0.8 if color != "green" else col_num * 0.5 # 绿色有渐变
for i in range(row_num):
count = 0
for j in range(col_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if limit1 < H <= limit2 and 34 < S and 46 < V:
count += 1
if count > col_num_limit:
if yl > i:
yl = i
if yh < i:
yh = i
for j in range(col_num):
count = 0
for i in range(row_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if limit1 < H <= limit2 and 34 < S and 46 < V:
count += 1
if count > row_num - row_num_limit:
if xl > j:
xl = j
if xr < j:
xr = j
return xl, xr, yh, yl
# 查找轮廓。
# 在输入的图像中寻找满足面积阈值(Min_Area)的所有轮廓,并筛选出长宽比符合车牌特性的轮廓,用于后续的矩形拟合和矫正
def img_findContours(img_contours):
contours, hierarchy = cv2.findContours(img_contours, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > Min_Area]
print("findContours len = ", len(contours))
# 排除面积最小的点
car_contours = []
for cnt in contours:
ant = cv2.minAreaRect(cnt)
width, height = ant[1]
if width < height:
width, height = height, width
ration = width / height
if 2 < ration < 5.5:
car_contours.append(ant)
box = cv2.boxPoints(ant)
return car_contours
# 图像矫正。
# 对每个检测到的轮廓进行最小外接矩形的计算,根据矩形的角度进行图像的旋转和平移变换,以矫正车牌图像至水平或接近水平状态,便于后续的文字识别。
def img_Transform(car_contours, oldimg, pic_width, pic_hight):
car_imgs = []
for car_rect in car_contours:
if -1 < car_rect[2] < 1:
angle = 1
# 对于角度为-1 1之间时,默认为1
else:
angle = car_rect[2]
car_rect = (car_rect[0], (car_rect[1][0] + 5, car_rect[1][1] + 5), angle)
box = cv2.boxPoints(car_rect)
heigth_point = right_point = [0, 0]
left_point = low_point = [pic_width, pic_hight]
for point in box:
if left_point[0] > point[0]:
left_point = point
if low_point[1] > point[1]:
low_point = point
if heigth_point[1] < point[1]:
heigth_point = point
if right_point[0] < point[0]:
right_point = point
if left_point[1] <= right_point[1]: # 正角度
new_right_point = [right_point[0], heigth_point[1]]
pts2 = np.float32([left_point, heigth_point, new_right_point]) # 字符只是高度需要改变
pts1 = np.float32([left_point, heigth_point, right_point])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight))
point_limit(new_right_point)
point_limit(heigth_point)
point_limit(left_point)
car_img = dst[int(left_point[1]):int(heigth_point[1]), int(left_point[0]):int(new_right_point[0])]
car_imgs.append(car_img)
elif left_point[1] > right_point[1]: # 负角度
new_left_point = [left_point[0], heigth_point[1]]
pts2 = np.float32([new_left_point, heigth_point, right_point]) # 字符只是高度需要改变
pts1 = np.float32([left_point, heigth_point, right_point])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight))
point_limit(right_point)
point_limit(heigth_point)
point_limit(new_left_point)
car_img = dst[int(right_point[1]):int(heigth_point[1]), int(new_left_point[0]):int(right_point[0])]
car_imgs.append(car_img)
return car_imgs
#颜色识别与精确裁剪。
# 对每个车牌图像进行颜色分析,判断车牌主要颜色(绿色、黄色、蓝色或黑白),并基于颜色信息进行二次精确裁剪,去除非车牌区域。
def img_color(card_imgs):
colors = []
for card_index, card_img in enumerate(card_imgs):
green = yello = blue = black = white = 0
card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
# 有转换失败的可能,原因来自于上面矫正矩形出错
if card_img_hsv is None:
continue
row_num, col_num = card_img_hsv.shape[:2]
card_img_count = row_num * col_num
for i in range(row_num):
for j in range(col_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if 11 < H <= 34 and S > 34:
yello += 1
elif 35 < H <= 99 and S > 34:
green += 1
elif 99 < H <= 124 and S > 34:
blue += 1
if 0 < H < 180 and 0 < S < 255 and 0 < V < 46:
black += 1
elif 0 < H < 180 and 0 < S < 43 and 221 < V < 225:
white += 1
color = "no"
limit1 = limit2 = 0
if yello * 2 >= card_img_count:
color = "yello"
limit1 = 11
limit2 = 34 # 有的图片有色偏偏绿
elif green * 2 >= card_img_count:
color = "green"
limit1 = 35
limit2 = 99
elif blue * 2 >= card_img_count:
color = "blue"
limit1 = 100
limit2 = 124 # 有的图片有色偏偏紫
elif black + white >= card_img_count * 0.7:
color = "bw"
colors.append(color)
card_imgs[card_index] = card_img
if limit1 == 0:
continue
xl, xr, yh, yl = accurate_place(card_img_hsv, limit1, limit2, color)
if yl == yh and xl == xr:
continue
need_accurate = False
if yl >= yh:
yl = 0
yh = row_num
need_accurate = True
if xl >= xr:
xl = 0
xr = col_num
need_accurate = True
if color == "green":
card_imgs[card_index] = card_img
else:
card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh - yl) // 4 else card_img[yl - (yh - yl) // 4:yh,xl:xr]
if need_accurate:
card_img = card_imgs[card_index]
card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
xl, xr, yh, yl = accurate_place(card_img_hsv, limit1, limit2, color)
if yl == yh and xl == xr:
continue
if yl >= yh:
yl = 0
yh = row_num
if xl >= xr:
xl = 0
xr = col_num
if color == "green":
card_imgs[card_index] = card_img
else:
card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh - yl) // 4 else card_img[yl - (yh - yl) // 4:yh,xl:xr]
return colors, card_imgs
# 根据设定的阈值和图片直方图,找出波峰,用于分隔字符
# 波峰检测。根据给定的阈值,在直方图中检测波峰点,常用于字符分割前的准备工作,识别字符间的边界。
def find_waves(threshold, histogram):
up_point = -1 # 上升点
is_peak = False
if histogram[0] > threshold:
up_point = 0
is_peak = True
wave_peaks = []
for i, x in enumerate(histogram):
if is_peak and x < threshold:
if i - up_point > 2:
is_peak = False
wave_peaks.append((up_point, i))
elif not is_peak and x >= threshold:
is_peak = True
up_point = i
if is_peak and up_point != -1 and i - up_point > 4:
wave_peaks.append((up_point, i))
return wave_peaks
#根据波峰分离车牌字符
# 利用find_waves找到的波峰,将车牌图像分割成单个字符图像,为字符识别做准备
def seperate_card(img, waves):
part_cards = []
for wave in waves:
part_cards.append(img[:, wave[0]:wave[1]])
return part_cards
#另一种颜色识别方法。与img_color类似,通过MSER算法(最大稳定极值区域)识别字符区域的颜色
def img_mser_color(card_imgs):
colors = []
for card_index, card_img in enumerate(card_imgs):
green = yello = blue = black = white = 0
card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
if card_img_hsv is None:
continue
row_num, col_num = card_img_hsv.shape[:2]
card_img_count = row_num * col_num
for i in range(row_num):
for j in range(col_num):
H = card_img_hsv.item(i, j, 0)
S = card_img_hsv.item(i, j, 1)
V = card_img_hsv.item(i, j, 2)
if 11 < H <= 34 and S > 34:
yello += 1
elif 35 < H <= 99 and S > 34:
green += 1
elif 99 < H <= 124 and S > 34:
blue += 1
if 0 < H < 180 and 0 < S < 255 and 0 < V < 46:
black += 1
elif 0 < H < 180 and 0 < S < 43 and 221 < V < 225:
white += 1
color = "no"
if yello * 2 >= card_img_count:
color = "yello"
elif green * 2 >= card_img_count:
color = "green"
elif blue * 2 >= card_img_count:
color = "blue"
elif black + white >= card_img_count * 0.7:
color = "bw"
colors.append(color)
card_imgs[card_index] = card_img
return colors, card_imgs
6、zhongjiantuxiang.py
'''
用于中间环节对处理图像的输出
'''
# -*- coding: utf-8 -*-
import cv2
import numpy as np
#显示图像处理的中间结果或最终结果
def img_show(filename):
if filename.dtype == "float32":
filename = filename.astype(np.uint8)
cv2.imshow("img_show", filename)
cv2.waitKey(0)
# 在原图像(oldimg)上绘制轮廓。突出显示图像中的特定形状或区域。
def img_contours(oldimg, box):
box = np.int0(box)
oldimg = cv2.drawContours(oldimg, [box], 0, (0, 0, 255), 2)
cv2.imshow("img_contours", oldimg)
cv2.waitKey(0)
# 接收一个图像,并返回该图像的高度和宽度。
def img_car(img_contours):
pic_hight, pic_width = img_contours.shape[:2]
return pic_hight, pic_width