检测图中车牌demo


import cv2

# 读取图片

rawImage = cv2.imread("D:\\11\\test\\1\\pos22.jpg")

cv2.imshow("1",rawImage)

# 高斯模糊,将图片平滑化,去掉干扰的噪声

image = cv2.GaussianBlur(rawImage, (3,3),0)

# 图片灰度化

image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Sobel算子(X方向)

Sobel_x = cv2.Sobel(image, cv2.CV_16S,1,0)

# Sobel_y = cv2.Sobel(image, cv2.CV_16S, 0, 1)

absX = cv2.convertScaleAbs(Sobel_x)# 转回uint8

# absY = cv2.convertScaleAbs(Sobel_y)

# dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)

image = absX

# 二值化:图像的二值化,就是将图像上的像素点的灰度值设置为0或255,图像呈现出明显的只有黑和白

ret, image = cv2.threshold(image,0,255, cv2.THRESH_OTSU)

# 闭操作:闭操作可以将目标区域连成一个整体,便于 后续轮廓的提取。

kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (3,5))

image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX)

# 膨胀腐蚀(形态学处理)

kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (20,1))

kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1,19))

image = cv2.dilate(image, kernelX)

image = cv2.erode(image, kernelX)

image = cv2.erode(image, kernelY)

image = cv2.dilate(image, kernelY)

# 平滑处理,中值滤波

image = cv2.medianBlur(image,3)

# 查找轮廓

contours, w1 = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]

for itemin contours:

rect = cv2.boundingRect(item)

x = rect[0]

y = rect[1]

weight = rect[2]

height = rect[3]

if weight > (height *2):

# 裁剪区域图片

        chepai = rawImage[y:y + height, x:x + weight]

cv2.imshow('chepai'+str(x), chepai)

chepaigray = cv2.cvtColor(chepai, cv2.COLOR_RGB2GRAY)

# cv2.imshow("2",chepaigray)

imageblur = cv2.medianBlur(chepaigray,3)

# cv2.imshow("1",imageblur)

#    image = cv2.imread("D:\\11\\new\\2.jpg")

retval,result=cv2.threshold(chepaigray,150,255,cv2.THRESH_BINARY)

cv2.imshow("re",result)

# 绘制轮廓

#image = cv2.drawContours(rawImage, contours, -1, (0, 0, 255), 3)

cv2.imshow('image', image)

cv2.waitKey(0)

cv2.destroyAllWindows()

你可能感兴趣的:(检测图中车牌demo)