程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Opencv Python license plate color judgment

編輯:Python

After cutting out the license plate area , We need to determine what color the license plate is .
My idea is : According to different colors hsv Threshold mask , The largest white pixel value obtained from the mask is the corresponding color .

take hsv Color threshold refers to my previous blog :Opencv+python Turn on the camera or drag the slider on the picture to get the color threshold
The main difference is 3 License plates of different colors : Blue 、 Yellow and green .
The resulting threshold is as follows :

lower_blue = np.array([100, 43, 46])
upper_blue = np.array([124, 255, 255])
lower_yellow = np.array([15, 55, 55])
upper_yellow = np.array([50, 255, 255])
lower_green = np.array([0, 3, 116])
upper_green = np.array([76, 211, 255])

The code is as follows :

import cv2
import numpy as np
# img_path = cv2.imread('./rest/yellow.png')
# img_path = cv2.imread('./rest/blue.jpg')
img_path = cv2.imread('./rest/green.jpg')
cv2.imshow('origin', img_path)
height = img_path.shape[0]
width = img_path.shape[1]
print(' area :', height * width)
# Set threshold 
lower_blue = np.array([100, 43, 46])
upper_blue = np.array([124, 255, 255])
lower_yellow = np.array([15, 55, 55])
upper_yellow = np.array([50, 255, 255])
lower_green = np.array([0, 3, 116])
upper_green = np.array([76, 211, 255])
# Convert to HSV
hsv = cv2.cvtColor(img_path, cv2.COLOR_BGR2HSV)
# The mask is constructed according to the threshold 
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow) #
mask_green = cv2.inRange(hsv, lower_green, upper_green) #
# Perform bit operations on the original image and mask 
# src1: First image ( The first object to merge )src2: The second image ( The second object of the merge )mask: Rules to be merged .
res_blue = cv2.bitwise_and(img_path, img_path, mask=mask_blue)
res_yellow = cv2.bitwise_and(img_path, img_path, mask=mask_yellow)
res_green = cv2.bitwise_and(img_path, img_path, mask=mask_green)
# Display images 
# cv2.imshow('frame', img_path)
cv2.imshow('mask_blue', mask_blue)
cv2.imshow('mask_yellow', mask_yellow)
cv2.imshow('mask_green', mask_green)
# cv2.imshow('res', res)
# Yes mask To operate -- Black and white pixel statistics Because the mask area of different colors is different 
# Record the sum of black and white pixels 
blue_white = 0
blue_black = 0
yellow_white = 0
yellow_black = 0
green_white = 0
green_black = 0
# Calculate the sum of black and white pixels in each column 
for i in range(width):
for j in range(height):
if mask_blue[j][i] == 255:
blue_white += 1
if mask_blue[j][i] == 0:
blue_black += 1
if mask_yellow[j][i] == 255:
yellow_white += 1
if mask_yellow[j][i] == 0:
yellow_black += 1
if mask_green[j][i] == 255:
green_white += 1
if mask_green[j][i] == 0:
green_black += 1
print(' Blue -- white = ', blue_white)
print(' Blue -- black = ', blue_black)
print(' yellow -- white = ', yellow_white)
print(' yellow -- black = ', yellow_black)
print(' green -- white = ', green_white)
print(' green -- black = ', green_black)
color_list = [' Blue ',' yellow ',' green ']
num_list = [blue_white,yellow_white,green_white]
print(' The color of the license plate is :',color_list[num_list.index(max(num_list))])
cv2.waitKey(0)

test 1:


test 2:


test 3:


You can see , This method can basically distinguish colors well , But the disadvantage is that you need to traverse every pixel , and 3 One color goes through 3 Time , This is relatively inefficient , At present, I haven't thought of any other way , Just leave it .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved