当前位置: 首页 > article >正文

Opencv识别车牌

Opencv识别车牌

#encoding:utf8
import cv2
import numpy as np
Min_Area = 50
#定位车牌
def color_position(img,output_path):
colors = [#([26,43,46], [34,255,255]), # 黄色
([100,43,46], [124,255,255]), # 蓝色
([35, 43, 46], [77, 255, 255]) # 绿色
]
hsv =cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
for (lower, upper) in colors:
lower = np.array(lower, dtype=“uint8”) # 颜色下限
upper = np.array(upper, dtype=“uint8”) # 颜色上限

    # 根据阈值找到对应的颜色
    mask = cv2.inRange(hsv, lowerb=lower, upperb=upper)
    cv2.imshow("mask", mask)
    cv2.waitKey(0)        
    output = cv2.bitwise_and(img, img, mask=mask)
    cv2.imshow("output", output)
    cv2.waitKey(0)         
    k = mark_zone_color(output,output_path)
    if k==1:
        return 1
    # 展示图片
    #cv2.imshow("image", img)
    #cv2.imshow("image-color", output)
    #cv2.waitKey(0)
return 0

def mark_zone_color(src_img,output_img):
#根据颜色在原始图像上标记
#转灰度
gray = cv2.cvtColor(src_img,cv2.COLOR_BGR2GRAY)
cv2.imshow(“gray”, gray)
cv2.waitKey(0)
#图像二值化
#ret,binary_OTSU = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
ret,binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
cv2.imshow(“binary”, binary)

cv2.waitKey(0)     
#轮廓检测
contours,hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)   
drawing = img.copy()
if len(contours)>0:
    cv2.drawContours(drawing, contours, -1, (0, 0, 255), 3) # 填充轮廓颜色
    cv2.imshow('drawing', drawing)
    cv2.waitKey(0)
#print(contours)

temp_contours = [] # 存储合理的轮廓
car_plates=[]
if len(contours)>0:
    for contour in contours:
        if cv2.contourArea(contour) > Min_Area:
            temp_contours.append(contour)
        car_plates = []
        for temp_contour in temp_contours:
            rect_tupple = cv2.minAreaRect(temp_contour)
            rect_width, rect_height = rect_tupple[1]
            if rect_width < rect_height:
                rect_width, rect_height = rect_height, rect_width
            aspect_ratio = rect_width / rect_height
            # 车牌正常情况下宽高比在2 - 5.5之间
            if aspect_ratio > 2 and aspect_ratio < 5.5:
                car_plates.append(temp_contour)
                #rect_vertices = cv2.boxPoints(rect_tupple)
                #rect_vertices = np.int0(rect_vertices)
        if len(car_plates)==1:
            #oldimg = cv2.drawContours(img, [rect_vertices], -1, (0, 0, 255), 2)
            #cv2.imshow("che pai ding wei", oldimg)
            #print(rect_tupple)
            break

#把车牌号所在的位置截取出来
if len(car_plates)==1:
    # 方法1 利用轮廓的坐标点来获取左上角 右下角的坐标
    #for car_plate in car_plates:
        ## 左上角的坐标
        #row_min,col_min = np.min(car_plate[:,0,:],axis=0)
        ## 右下角的坐标
        #row_max,col_max = np.max(car_plate[:,0,:],axis=0)
        #cv2.rectangle(img,(row_min,col_min),(row_max,col_max),(0,255,0),2)
        #card_img = img[col_min:col_max,row_min:row_max,:]
        #cv2.imshow("img",img)
    # 方法2,利用外接矩形来定位
    x,y,w,h = cv2.boundingRect(car_plates[0])
    cv2.rectangle(img,(x,y),(x+w, y+h),(0,255,0),2)
    cv2.imshow("img",img)
    cv2.waitKey(0)
    card_img = img[y:y+h,x:x+w,:]
    cv2.imwrite('card_img.jpg',card_img)
    cv2.imshow("card_img.",card_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return 1
return 0

if name == “main”:
img = cv2.imread(‘…\Opencv-6-datas\cartest.png’)
color_position(img, ‘’)
在这里插入图片描述

#encoding:utf8
import  cv2
import numpy as np
Min_Area = 50
#定位车牌
def color_position(img,output_path):
    colors = [#([26,43,46], [34,255,255]), # 黄色
           ([100,43,46], [124,255,255]), # 蓝色
    ([35, 43, 46], [77, 255, 255]) # 绿色
    ]
    hsv =cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    for (lower, upper) in colors:
        lower = np.array(lower, dtype="uint8") # 颜色下限
        upper = np.array(upper, dtype="uint8") # 颜色上限

        # 根据阈值找到对应的颜色
        mask = cv2.inRange(hsv, lowerb=lower, upperb=upper)
        cv2.imshow("mask", mask)
        cv2.waitKey(0)        
        output = cv2.bitwise_and(img, img, mask=mask)
        cv2.imshow("output", output)
        cv2.waitKey(0)         
        k = mark_zone_color(output,output_path)
        if k==1:
            return 1
        # 展示图片
        #cv2.imshow("image", img)
        #cv2.imshow("image-color", output)
        #cv2.waitKey(0)
    return 0
def mark_zone_color(src_img,output_img):
    #根据颜色在原始图像上标记
    #转灰度
    gray = cv2.cvtColor(src_img,cv2.COLOR_BGR2GRAY)
    cv2.imshow("gray", gray)
    cv2.waitKey(0)     
    #图像二值化
    #ret,binary_OTSU = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    ret,binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)
    cv2.imshow("binary", binary)    

    cv2.waitKey(0)     
    #轮廓检测
    contours,hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)   
    drawing = img.copy()
    if len(contours)>0:
        cv2.drawContours(drawing, contours, -1, (0, 0, 255), 3) # 填充轮廓颜色
        cv2.imshow('drawing', drawing)
        cv2.waitKey(0)
    #print(contours)

    temp_contours = [] # 存储合理的轮廓
    car_plates=[]
    if len(contours)>0:
        for contour in contours:
            if cv2.contourArea(contour) > Min_Area:
                temp_contours.append(contour)
            car_plates = []
            for temp_contour in temp_contours:
                rect_tupple = cv2.minAreaRect(temp_contour)
                rect_width, rect_height = rect_tupple[1]
                if rect_width < rect_height:
                    rect_width, rect_height = rect_height, rect_width
                aspect_ratio = rect_width / rect_height
                # 车牌正常情况下宽高比在2 - 5.5之间
                if aspect_ratio > 2 and aspect_ratio < 5.5:
                    car_plates.append(temp_contour)
                    #rect_vertices = cv2.boxPoints(rect_tupple)
                    #rect_vertices = np.int0(rect_vertices)
            if len(car_plates)==1:
                #oldimg = cv2.drawContours(img, [rect_vertices], -1, (0, 0, 255), 2)
                #cv2.imshow("che pai ding wei", oldimg)
                #print(rect_tupple)
                break

    #把车牌号所在的位置截取出来
    if len(car_plates)==1:
        # 方法1 利用轮廓的坐标点来获取左上角 右下角的坐标
        #for car_plate in car_plates:
            ## 左上角的坐标
            #row_min,col_min = np.min(car_plate[:,0,:],axis=0)
            ## 右下角的坐标
            #row_max,col_max = np.max(car_plate[:,0,:],axis=0)
            #cv2.rectangle(img,(row_min,col_min),(row_max,col_max),(0,255,0),2)
            #card_img = img[col_min:col_max,row_min:row_max,:]
            #cv2.imshow("img",img)
        # 方法2,利用外接矩形来定位
        x,y,w,h = cv2.boundingRect(car_plates[0])
        cv2.rectangle(img,(x,y),(x+w, y+h),(0,255,0),2)
        cv2.imshow("img",img)
        cv2.waitKey(0)
        card_img = img[y:y+h,x:x+w,:]
        cv2.imwrite('card_img.jpg',card_img)
        cv2.imshow("card_img.",card_img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        return 1
    return 0

if __name__ == "__main__":
    img = cv2.imread('..\Opencv-6-datas\cartest.png')           
    color_position(img, '')

http://www.kler.cn/a/16620.html

相关文章:

  • 2024/11/13 英语每日一段
  • 【算法】——二分查找合集
  • vue3+element-plus==> el-form输入响应式失效踩坑!!!!!!!!!!
  • 2019年下半年试题二:论软件系统架构评估及其应用
  • 数据结构-集合
  • 【真题笔记】21年系统架构设计师案例理论点总结
  • 多级缓存建设方案
  • PHP图片上传代码怎么写和代码的用发
  • vue3表单输入绑定
  • DDD系列:三、Repository模式
  • C++项目中打破循环依赖的锁链:实用方法大全
  • 【Java校招面试】基础知识(二)——Spring Framework AOP
  • java stream 实践篇
  • day1_内存区域
  • 枚举法计算24点游戏
  • C++Primer第五版【阅读笔记】
  • LeetCode 560. 和为 K 的子数组
  • kettle不同数据源的字段不一致的合并后插入数据库
  • 如何使用快速排序算法对整数数组进行就地排序?
  • 从4k到42k,软件测试工程师的涨薪史,给我看哭了
  • 我的医学预测模型评价步骤(仅供参考)
  • SmartEngine流程引擎之Custom模式
  • ApplicationContextAware接口
  • ETL工具 - Kettle 输入输出算子介绍
  • MyBatisPlus代码生成器使用
  • Linux Ansible角色介绍