首页 > 其他分享 >paddleocr_opencv_learn_颜色检测

paddleocr_opencv_learn_颜色检测

时间:2023-03-02 19:44:06浏览次数:44  
标签:goal img paddleocr learn cv2 opencv car import original

E:\song2\paddler_ocr\paddleocr_learn00\le00.py


from paddleocr import PaddleOCR


# 2. 声明PaddleOCR类
ocr = PaddleOCR()  
img_path = './12.jpg'

# 3. 执行预测
result = ocr.ocr(img_path, rec=True)
print(f"The predicted text box of {img_path} are follows.")
print(result)

E:\song2\paddler_ocr\paddleocr_learn00\le01.py

import numpy as np
import cv2
import matplotlib.pyplot as plt

from paddleocr import PaddleOCR


img_path = './13.jpg'

# 2. 声明PaddleOCR类
ocr = PaddleOCR()  

# 3. 执行预测
result = ocr.ocr(img_path, rec=True)

# 4. 可视化检测结果
image = cv2.imread(img_path)




for res1 in result:
    for res2 in res1:
        pos = res2[0]
        text= res2[1]
        print('------------')
        print(pos)
        print(text)
        # print(res2[0])
        # print(res2[1])
        box_temp = np.reshape(np.array(pos), [-1, 1, 2]).astype(np.int64)
        image = cv2.polylines(np.array(image), [box_temp], True, (255, 0, 0), 2)

# 画出读取的图片
plt.figure(figsize=(10, 10)) 
plt.imshow(image)
plt.show()






E:\song2\paddler_ocr\paddleocr_learn00\le02.py


import numpy as np
res = [
    [
        [[[42.0, 414.0], [483.0, 391.0], [485.0, 428.0], [44.0, 451.0]], ('上海斯格威铂尔受大酒店', 0.8757175803184509)],
        [[[187.0, 457.0], [399.0, 448.0], [401.0, 480.0], [188.0, 489.0]], ('打浦路15号', 0.8970528244972229) ],
        [[[23.0, 508.0], [514.0, 488.0], [516.0, 529.0], [25.0, 549.0]], ('绿洲仕格维花园公寓', 0.9934195280075073)],
        [[[74.0, 554.0], [428.0, 542.0], [429.0, 571.0], [75.0, 582.0]], ('打浦路252935号', 0.9223073720932007)]
    ]
]



boxes = [line[0] for line in res]


for box in boxes:

    box = np.reshape(np.array(box[0]), [-1, 1, 2]).astype(np.int64)
    print(box)
    

E:\song2\paddler_ocr\paddleocr_learn00\le03.py

# -*- coding:utf-8 -*-
import cv2
import numpy as np
 
img = cv2.imread("12.jpg", 1)
pts = np.array([[10, 10], [400, 10], [400, 400], [10, 400]], np.int64)  # 数据类型必须为 int32
print(pts)

pts = pts.reshape((-1, 1, 2))
print('-------------')
print(pts)
 
# 绘制未填充的多边形
cv2.polylines(img, [pts], isClosed=True, color=(0,0,255), thickness=1)
 
# 绘制填充的多边形
# cv2.fillPoly(img, [pts], color=(255, 255, 0))
cv2.imshow('img', img)
cv2.waitKey()

E:\song2\paddler_ocr\paddleocr_learn00\le04.py


#基于颜色信息的车牌提取

#识别普通民用小型车,车牌号是蓝底白字,正常光照下蓝色车牌的BGR大约是(138,63,23);车牌形状是矩形,长宽比是3:1
import cv2
import numpy as np

#读取车辆原始图像
car_original = cv2.imread('./car.jpg')

rows,cols,td = car_original.shape  #图像的行/高、列/宽、通道数
print(rows,cols,td)  #输出图像的大小,方便对图像的尺寸有一个了解
cv2.imshow('car_original',car_original)  #显示原始图像


'''
# 基于颜色信息进行二值化
正常光照下蓝色车牌的BGR大约是(138,63,23),
由于存在一定的偏差,在二值化的时候设置一个范围,在范围之内的颜色(车牌的蓝色背景)设置成白色,否则设置成黑色。   
'''
blue_goal = 138
green_goal = 63
red_goal = 23

threshold = 50  #二值化的时候,通过设置一个阈值来设置一个范围

#注意:OpenCV中彩色图像的像素值是BGR格式,即(blue,green,red)
for i in range(0,rows):
    for j in range(0,cols):
        B = car_original[i,j,0]
        G = car_original[i,j,1]
        R = car_original[i,j,2]
        if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold:
            car_original[i,j,0] = 255
            car_original[i,j,1] = 255
            car_original[i,j,2] = 255
        else:
            car_original[i,j,0] = 0
            car_original[i,j,1] = 0
            car_original[i,j,2] = 0
# 相当于颜色过滤 
cv2.imshow('car_binarization',car_original)  #显示基于颜色信息二值化之后的图像


# 接下来进行形态学处理 ,先膨胀再腐蚀
kernel = np.ones((3,3),np.int8)
dilation = cv2.dilate(car_original,kernel,iterations=3)  #膨胀三次
cv2.imshow('car_dilation',dilation)  #膨胀三次之后,可以看出车牌矩形区域内全部是白色

erosion = cv2.erode(dilation,kernel,iterations=3) #再腐蚀三次,降噪
cv2.imshow('car_erosion',erosion)  #形态学处理之后最终得到的图像


#形态学处理之后的图像还是三通道的黑白图,通过灰度化转换成单通道的黑白图
car = cv2.cvtColor(erosion,cv2.COLOR_BGR2GRAY)


#查找轮廓
contours,hierarchy = cv2.findContours(car,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
    x,y,w,h = cv2.boundingRect(c)  #轮廓的边界矩形,x,y是矩形左上角的坐标,w,h是矩形的宽度和高度

    # 车牌形状是矩形,长宽比是3:1
    if w>60 and h>20:  #如果当前的这个轮廓区域足够大,它一定是车牌号矩形区域。
                       # 如果原始图像有大面积的蓝色背景,例如车的颜色是蓝色的,则可以设置w和h的上限值,即min<w<max and min<h<max
        x_goal = x
        y_goal = y
        w_goal = w
        h_goal = h
        goal = c

car_goal = cv2.imread('./car.jpg')
cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,255,0),2)  #把车牌号在原始图像上标记出来
cv2.imshow('goal',car_goal)
cv2.waitKey(0)




E:\song2\paddler_ocr\paddleocr_learn00\le05.py

#基于边缘检测的车牌提取
import cv2
import numpy as np

from imutils.perspective import four_point_transform

#读取车辆原始图像
car_original = cv2.imread('car_edge.jpg')
car_gray = cv2.cvtColor(car_original,cv2.COLOR_BGR2GRAY)  #灰度化

# cv2.imshow('car_gray',car_gray)
car_blur = cv2.GaussianBlur(car_gray,(3,3),0)  #高斯模糊
car_edge = cv2.Canny(car_blur,250,300)  #Canny边缘检测
# cv2.imshow('car_edge',car_edge)

#接下来进行形态学处理
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,1))  #设定核的形状大小,由于车牌的字符是横向排列的,设定是横向膨胀
car_dilation_2 = cv2.dilate(car_edge,kernel,iterations=2)  #膨胀两次,保证字符区域全部连通起来,膨胀必须一步到位,否则再次腐蚀可能会将图像回复原装
# cv2.imshow('car_dilation_2',car_dilation_2)
car_erosion_4 = cv2.erode(car_dilation_2,kernel,iterations=4)  #再腐蚀四次,尽可能多的去除小块碎片
# cv2.imshow('car_erosion_4',car_erosion_4)
car_dilation_4 = cv2.dilate(car_erosion_4,kernel,iterations=2)  #再膨胀两次,保证膨胀总次数和腐蚀总次数相同
# cv2.imshow('car_dilation_4',car_dilation_4)

#再进行Y方向的膨胀腐蚀
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,5))  #设定核的形状大小,纵向
car_erosion_6 = cv2.erode(car_dilation_4,kernel,iterations=2)  #再沿Y方向腐蚀两次,尽可能多的去除小块碎片
# cv2.imshow('car_erosion_6',car_erosion_6)
car_dilation_6 = cv2.dilate(car_erosion_6,kernel,iterations=2)  #再膨胀两次,保证膨胀总次数和腐蚀总次数相同
# cv2.imshow('car_dilation_6',car_dilation_6)  #经过六次腐蚀和六次膨胀,最终得到理想的结果

#查找轮廓
contours,hierarchy = cv2.findContours(car_dilation_6,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
    x,y,w,h = cv2.boundingRect(c)  #轮廓的边界矩形,x,y是矩形左上角的坐标,w,h是矩形的宽度和高度
    if w>60 and h>20:  #如果当前的这个轮廓区域足够大,它一定是车牌号矩形区域。
        x_goal = x
        y_goal = y
        w_goal = w
        h_goal = h
        goal = c
car_goal = cv2.imread('car_edge.jpg')
cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,255,0),2)  #把车牌号在原始图像上标记出来
# cv2.imshow('car_goal',car_goal)

license_plate = car_original[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal,:]  #把车牌提取出来
# cv2.imshow('license_plate',license_plate)  #仅显示车牌区域



'''
传统的字符分割方法主要有:直接分割法、基于识别基础上的分割法、自适应分割线聚类法
    直接分割法简单,但是其局限是分割点的确定需要比较高的准确性
    基于识别基础上的分割法,把识别和分割结合起来,但是需要识别的高准确性,根据识别和分类的耦合程度又有不同的划分
    自适应分割线聚类法,建立一个分类器,用来判断图像的每一列是否是分割线,是根据训练样本来进行自适应学习的神经网络分类器,但是对于粘连字符训练困难
    还有直接把字符组成的单词当作一个整体来识别的,例如运用马尔科夫数学模型等方法进行处理,主要用于印刷体文本识别
'''
#把车牌放大
license_plate_large = cv2.resize(license_plate,None,fx=3,fy=3,interpolation=cv2.INTER_CUBIC)  #interpolation:插值方法
# cv2.imshow('license_plate_large',license_plate_large)  #显示放大之后的车牌
license_plate_gray = cv2.cvtColor(license_plate_large,cv2.COLOR_BGR2GRAY)  #灰度化
img_blur = cv2.GaussianBlur(license_plate_gray,(5,5),0)  #高斯模糊
canny_edge = cv2.Canny(img_blur,80,200)  #Canny边缘检测
# cv2.imshow('canny_edge',canny_edge)


'''
#使用Otsu算法进行二值化
ret,threshold = cv2.threshold(img_blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('threshold',threshold)  #仅显示车牌区域
'''

(x,y)=canny_edge.shape #返回的分别是矩阵的行数和列数,x是行数,y是列数
#将第一列和最后一列设置成黑色,这样pointCount的第一个和最后一个值都是0,则[上个为0当前不为0,即为开始;上个不为0当前为0,即为结束]不会出问题
canny_edge[:,0] = 0
canny_edge[:,y-1] = 0
pointCount=np.zeros(y,dtype=np.uint8)#每列白色的个数
#i是列数,j是行数
for i in range(0,y):
    for j in range(0,x):
        if(canny_edge[j,i]==255):
            pointCount[i]=pointCount[i]+1


#start和end里面存放的是每个字符在图像中的起始列和结束列,即垂直分割
start = []
end = []
# 对图像进行垂直分割
for index in range(1, y):
    # 上个为0当前不为0,即为开始
    if ((pointCount[index-1] == 0) & (pointCount[index] != 0)):
        start.append(index)
    # 上个不为0当前为0,即为结束
    elif ((pointCount[index-1] != 0) & (pointCount[index] == 0)):
        end.append(index)


#将分割之后的字符显示出来
imgArr=np.zeros(x)
imgArr=imgArr.reshape((-1,1)) #转置,行向量转成列向量
for idx in range(0,len(start)):
    if idx in [0,1,3,4,5,6,7]:  #将车牌字符里面的小圆圈点去掉,只保留车牌里面有用的字符
        temp_img=canny_edge[:,start[idx]:end[idx]]
        imgArr=np.hstack((imgArr,temp_img))  #矩阵拼接,将各个字符拼接一下显示出来
(xx,yy)=imgArr.shape
imgArr=imgArr[:,1:yy]

cv2.imshow('vertical_cut',imgArr)

#根据类似的分割算法实现,在垂直分割之后,还可以对每个字符进行水平分割,在此不再赘述。
#垂直分割和水平分割之后,即可以得到每个字符,再用相关的算法识别字符,在此字符识别暂时不写了。
cv2.waitKey(0)

E:\song2\paddler_ocr\paddleocr_learn00\le06.py

import cv2
import matplotlib.pyplot as plt

import numpy as np

from paddleocr import PaddleOCR



#读取车辆原始图像
img_original = cv2.imread('./13.jpg')
rows,cols,td = img_original.shape  #图像的行/高、列/宽、通道数
#print(rows,cols,td)  #输出图像的大小,方便对图像的尺寸有一个了解
cv2.imshow('car_original',img_original)  #显示原始图像

#基于颜色信息进行二值化
#正常光照下蓝色的BGR大约是(138,63,23),由于存在一定的偏差,在二值化的时候设置一个范围,在范围之内的颜色(蓝色背景)设置成白色,否则设置成黑色。
blue_goal =254 
green_goal = 61 
red_goal = 24 

threshold = 50  #二值化的时候,通过设置一个阈值来设置一个范围

#注意:OpenCV中彩色图像的像素值是BGR格式,即(blue,green,red)
for i in range(0,rows):
    for j in range(0,cols):
        B = img_original[i,j,0]
        G = img_original[i,j,1]
        R = img_original[i,j,2]
        if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold:
            img_original[i,j,0] = 255
            img_original[i,j,1] = 255
            img_original[i,j,2] = 255
        else:
            img_original[i,j,0] = 0
            img_original[i,j,1] = 0
            img_original[i,j,2] = 0

# 相当于颜色过滤 
cv2.imshow('img_binarization',img_original)  #显示基于颜色信息二值化之后的图像


#接下来进行形态学处理
#先膨胀再腐蚀 

# kernel = np.ones((15,15),np.int8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))  #设定核的形状大小,
dilation_1 = cv2.dilate(img_original,kernel,iterations=3)  #膨胀3次

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))  #设定核的形状大小,纵向膨胀
dilation_2 = cv2.dilate(dilation_1,kernel,iterations=3)  #膨胀3次

cv2.imshow('dilation1',dilation_2)  


kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))  
erosion_1 = cv2.erode(dilation_2,kernel,iterations=5)  #再腐蚀5次,尽可能多的去除小块碎片

cv2.imshow('car_dilation',erosion_1)  


# #形态学处理之后的图像还是三通道的黑白图,通过灰度化转换成单通道的黑白图
dst_img = cv2.cvtColor(erosion_1,cv2.COLOR_BGR2GRAY)


#查找轮廓
contours,hierarchy = cv2.findContours(dst_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    x,y,w,h = cv2.boundingRect(c)  #轮廓的边界矩形,x,y是矩形左上角的坐标,w,h是矩形的宽度和高度

    if w>320 and h>210:  #如果当前的这个轮廓区域足够大,它一定是车牌号矩形区域。
#                         # 如果原始图像有大面积的蓝色背景,例如车的颜色是蓝色的,则可以设置w和h的上限值,即min<w<max and min<h<max
        x_goal = x
        y_goal = y
        w_goal = w
        h_goal = h
        goal = c

car_goal = cv2.imread('./13.jpg')
cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,0,255),4)  #把车牌号在原始图像上标记出来
cv2.imshow('goal',car_goal)



img_humi = img_original[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal]  #把车牌提取出来
# print(img_humi)


# # 2. 声明PaddleOCR类
# ocr = PaddleOCR()  

# # 3. 执行预测
# result = ocr.ocr(img_humi, rec=True)

# # 4. 可视化检测结果
# image = cv2.imread(img_humi)




# for res1 in result:
#     for res2 in res1:
#         pos = res2[0]
#         text= res2[1]
#         print('------------')
#         print(pos)
#         print(text)
#         # print(res2[0])
#         # print(res2[1])
#         box_temp = np.reshape(np.array(pos), [-1, 1, 2]).astype(np.int64)
#         img_humi = cv2.polylines(np.array(img_humi), [box_temp], True, (255, 0, 0), 2)

# # 画出读取的图片
plt.figure(figsize=(10, 10)) 
plt.imshow(img_humi)
plt.show()





# cv2.waitKey(0)

E:\song2\paddler_ocr\paddleocr_learn00\le07.py

import cv2
import matplotlib.pyplot as plt

import numpy as np

from paddleocr import PaddleOCR



#读取车辆原始图像
img_original = cv2.imread('./13.jpg')
rows,cols,td = img_original.shape  #图像的行/高、列/宽、通道数
#print(rows,cols,td)  #输出图像的大小,方便对图像的尺寸有一个了解
# cv2.imshow('car_original',img_original)  #显示原始图像

#基于颜色信息进行二值化
#正常光照下蓝色的BGR大约是(138,63,23),由于存在一定的偏差,在二值化的时候设置一个范围,在范围之内的颜色(蓝色背景)设置成白色,否则设置成黑色。
blue_goal =254 
green_goal = 61 
red_goal = 24 

threshold = 50  #二值化的时候,通过设置一个阈值来设置一个范围

#注意:OpenCV中彩色图像的像素值是BGR格式,即(blue,green,red)
for i in range(0,rows):
    for j in range(0,cols):
        B = img_original[i,j,0]
        G = img_original[i,j,1]
        R = img_original[i,j,2]
        if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold:
            img_original[i,j,0] = 255
            img_original[i,j,1] = 255
            img_original[i,j,2] = 255
        else:
            img_original[i,j,0] = 0
            img_original[i,j,1] = 0
            img_original[i,j,2] = 0

# 相当于颜色过滤 
# cv2.imshow('img_binarization',img_original)  #显示基于颜色信息二值化之后的图像


#接下来进行形态学处理
#先膨胀再腐蚀 

# kernel = np.ones((15,15),np.int8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))  #设定核的形状大小,
dilation_1 = cv2.dilate(img_original,kernel,iterations=3)  #膨胀3次

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))  #设定核的形状大小,纵向膨胀
dilation_2 = cv2.dilate(dilation_1,kernel,iterations=3)  #膨胀3次

# cv2.imshow('dilation1',dilation_2)  


kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))  
erosion_1 = cv2.erode(dilation_2,kernel,iterations=5)  #再腐蚀5次,尽可能多的去除小块碎片

# cv2.imshow('car_dilation',erosion_1)  


# #形态学处理之后的图像还是三通道的黑白图,通过灰度化转换成单通道的黑白图
dst_img = cv2.cvtColor(erosion_1,cv2.COLOR_BGR2GRAY)


#查找轮廓
contours,hierarchy = cv2.findContours(dst_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    x,y,w,h = cv2.boundingRect(c)  #轮廓的边界矩形,x,y是矩形左上角的坐标,w,h是矩形的宽度和高度

    if w>320 and h>210:  #如果当前的这个轮廓区域足够大,它一定是车牌号矩形区域。
#                         # 如果原始图像有大面积的蓝色背景,例如车的颜色是蓝色的,则可以设置w和h的上限值,即min<w<max and min<h<max
        x_goal = x
        y_goal = y
        w_goal = w
        h_goal = h
        goal = c


car_goal = cv2.imread('./13.jpg')
cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,0,255),4)  #把车牌号在原始图像上标记出来
# cv2.imshow('goal',car_goal)



img_humi = car_goal[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal]  #把车牌提取出来
# cv2.imshow('img_humi',img_humi)


# print(img_humi)


# # 2. 声明PaddleOCR类
ocr = PaddleOCR()  

# # 3. 执行预测
result = ocr.ocr(img_humi, rec=True)

# # 4. 可视化检测结果
# image = cv2.imread(img_humi)




for res1 in result:
    for res2 in res1:
        pos = res2[0]
        text= res2[1]
        print('------------')
        print(pos[0])
        print(pos[0][0])
        print(pos[0][1])
        print(type( pos[0][1]))
        print(text)
        # print(res2[0])
        # print(res2[1])
        box_temp = np.reshape(np.array(pos), [-1, 1, 2]).astype(np.int64)
        img_humi = cv2.polylines(np.array(img_humi), [box_temp], True, (0, 255, 0), 2)
        # cv2.putText(img_humi,text,(pos[0],pos[1]),cv2.FONT_HERSHEY_SIMPLEX,1)
        cv2.putText(img_humi,text[0],(int(pos[0][0]), int(pos[0][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5,( 0,255,255),1,cv2.LINE_AA)




# 画出读取的图片
plt.figure(figsize=(10, 10)) 
plt.imshow(img_humi)
plt.show()


cv2.waitKey(0)

E:\song2\paddler_ocr\paddleocr_learn00\le08.py

import cv2
import matplotlib.pyplot as plt

import numpy as np

from paddleocr import PaddleOCR



#读取车辆原始图像
img_original = cv2.imread('./13.jpg')
rows,cols,td = img_original.shape  #图像的行/高、列/宽、通道数
#print(rows,cols,td)  #输出图像的大小,方便对图像的尺寸有一个了解
# cv2.imshow('car_original',img_original)  #显示原始图像

#基于颜色信息进行二值化
#正常光照下蓝色的BGR大约是(138,63,23),由于存在一定的偏差,在二值化的时候设置一个范围,在范围之内的颜色(蓝色背景)设置成白色,否则设置成黑色。
blue_goal =49 
green_goal = 63 
red_goal = 200 

threshold = 50  #二值化的时候,通过设置一个阈值来设置一个范围

#注意:OpenCV中彩色图像的像素值是BGR格式,即(blue,green,red)
for i in range(0,rows):
    for j in range(0,cols):
        B = img_original[i,j,0]
        G = img_original[i,j,1]
        R = img_original[i,j,2]
        if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold:
            img_original[i,j,0] = 255
            img_original[i,j,1] = 255
            img_original[i,j,2] = 255
        else:
            img_original[i,j,0] = 0
            img_original[i,j,1] = 0
            img_original[i,j,2] = 0

# 相当于颜色过滤 
# cv2.imshow('img_binarization',img_original)  #显示基于颜色信息二值化之后的图像


#接下来进行形态学处理
#先膨胀再腐蚀 

# kernel = np.ones((15,15),np.int8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))  #设定核的形状大小,
dilation_1 = cv2.dilate(img_original,kernel,iterations=3)  #膨胀3次

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10))  #设定核的形状大小,纵向膨胀
dilation_2 = cv2.dilate(dilation_1,kernel,iterations=3)  #膨胀3次

# cv2.imshow('dilation1',dilation_2)  


kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))  
erosion_1 = cv2.erode(dilation_2,kernel,iterations=5)  #再腐蚀5次,尽可能多的去除小块碎片

# cv2.imshow('car_dilation',erosion_1)  


# #形态学处理之后的图像还是三通道的黑白图,通过灰度化转换成单通道的黑白图
dst_img = cv2.cvtColor(erosion_1,cv2.COLOR_BGR2GRAY)


#查找轮廓
contours,hierarchy = cv2.findContours(dst_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    x,y,w,h = cv2.boundingRect(c)  #轮廓的边界矩形,x,y是矩形左上角的坐标,w,h是矩形的宽度和高度

    if w>320 and h>210:  #如果当前的这个轮廓区域足够大,它一定是车牌号矩形区域。
#                         # 如果原始图像有大面积的蓝色背景,例如车的颜色是蓝色的,则可以设置w和h的上限值,即min<w<max and min<h<max
        x_goal = x
        y_goal = y
        w_goal = w
        h_goal = h
        goal = c


car_goal = cv2.imread('./13.jpg')
cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,0,255),4)  #把车牌号在原始图像上标记出来
# cv2.imshow('goal',car_goal)



img_humi = car_goal[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal]  #把车牌提取出来
# cv2.imshow('img_humi',img_humi)


# print(img_humi)


# # 2. 声明PaddleOCR类
ocr = PaddleOCR()  

# # 3. 执行预测
result = ocr.ocr(img_humi, rec=True)

# # 4. 可视化检测结果
# image = cv2.imread(img_humi)




for res1 in result:
    for res2 in res1:
        pos = res2[0]
        text= res2[1]
        print('------------')
        print(pos[0])
        print(pos[0][0])
        print(pos[0][1])
        print(type( pos[0][1]))
        print(text)
        # print(res2[0])
        # print(res2[1])
        box_temp = np.reshape(np.array(pos), [-1, 1, 2]).astype(np.int64)
        img_humi = cv2.polylines(np.array(img_humi), [box_temp], True, (0, 255, 0), 2)
        # cv2.putText(img_humi,text,(pos[0],pos[1]),cv2.FONT_HERSHEY_SIMPLEX,1)
        cv2.putText(img_humi,text[0],(int(pos[0][0]), int(pos[0][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5,( 0,255,255),1,cv2.LINE_AA)




# 画出读取的图片
plt.figure(figsize=(10, 10)) 
plt.imshow(img_humi)
plt.show()


cv2.waitKey(0)

标签:goal,img,paddleocr,learn,cv2,opencv,car,import,original
From: https://www.cnblogs.com/zhuoss/p/17173122.html

相关文章