@TOC
一、彩色图像转换
(一)彩色图像文件转换为灰度文件
1.使用OpenCV
引入包以及文件路径
import cv2
import numpy as np
source_path="F:\\Anaconda3\\project\\jupyter\\picture\\lena.png"
使用opencv,直接读取为灰度图片
cv_read=cv2.imread(source_path,0)
cv2.imshow('lena',cv_read)
cv2.waitKey(0)
结果如下: 使用opencv,读取彩色,再转为灰度
cv_read=cv2.imread(source_path)
cv_gray=cv2.cvtColor(cv_read,cv2.COLOR_BGR2GRAY)
cv2.imshow('lena',cv_gray)
cv2.waitKey(0)
结果如下:
2.不使用OpenCV
不使用opencv的转换函数
row,col,channel=cv_read.shape
lena_gray=np.zeros((row,col))
for r in range(row):
for l in range(col):
lena_gray[r,l]=1/3*cv_read[r,l,0]+1/3*cv_read[r,l,1]+1/3*cv_read[r,l,2]
cv2.imshow("lena",lena_gray.astype("uint8"))
cv2.waitKey(0)
(二)彩色图像(RGB)转为HSV、HSI 格式
1.转为HSV格式
cv_read=cv2.imread(source_path)
lena_hsv=cv2.cvtColor(cv_read,cv2.COLOR_BGR2HSV)
cv2.imshow('lena',lena_hsv)
cv2.waitKey(0)
结果如下:
2.转为HSI格式
def RBG_to_HSI(rgb_img):
"""
: param rgb_img: RGB彩色图像
: return : HSI图像
"""
row=np.shape(rgb_img)[0]
col=np.shape(rgb_img)[1]
hsi_img=rgb_img.copy()
B,G,R=cv2.split(rgb_img)
[B,G,R]=[i/255.0 for i in ([B,G,R])]
H=np.zeros((row,col))
I=(R+G+B)/3.0
S=np.zeros((row,col))
for i in range(row):
den = np.sqrt((R[i]-G[i])**2+(R[i]-B[i])*(G[i]-B[i]))
thetha=np.arccos(0.5*(R[i]-B[i]+R[i]-G[i])/den)
h=np.zeros(col)
h[B[i]<=G[i]]=thetha[B[i]<=G[i]]
h[G[i]<=B[i]]=2*np.pi-thetha[G[i]<=B[i]]
h[den==0]=0
H[i]=h/(2*np.pi)
for i in range(row):
min_=[]
for j in range(col):
arr=[B[i][j],G[i][j],R[i][j]]
min_.append(np.min(arr))
min_=np.array(min_)
S[i]=1-min_*3/(R[i]+B[i]+G[i])
S[i][R[i]+B[i]+G[i]==0]=0
hsi_img[:,:,0]=H*255
hsi_img[:,:,1]=S*255
hsi_img[:,:,2]=I*255
return hsi_img
cv_read=cv2.imread(source_path)
lena_hsi=RBG_to_HSI(cv_read)
cv2.imshow('lena',lena_hsi)
cv2.waitKey(0)
二、车牌数字分割
图片如下所示
代码如下:
import os
import shutil
import cv2
import numpy as np
file_path = "F:\\Anaconda3\\project\\jupyter\\plate\\car\\"
licenses = os.listdir(file_path)
for license in licenses:
path = file_path+license
output_path = "F:\\Anaconda3\\project\\jupyter\\plate\\"+license
if os.path.isdir(output_path):
shutil.rmtree(output_path)
os.mkdir(output_path)
src = cv2.imread(path)
img = src.copy()
cv2.circle(img, (145, 20), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (430, 20), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (145, 170), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (430, 170), 10, (255, 0, 0), thickness=-1)
cv2.circle(img, (180, 90), 10, (255, 0, 0), thickness=-1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
GSblurred = cv2.GaussianBlur(gray, (5, 5), 12)
ret, thresh = cv2.threshold(GSblurred , 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
print("ret",ret)
kernel = np.ones((3, 3), int)
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel,iterations=2)
ret, thresh = cv2.threshold(closed, 127, 255, cv2.THRESH_BINARY+ cv2.THRESH_OTSU)
white = []
black = []
height = thresh.shape[0]
width = thresh.shape[1]
white_max = 0
black_max = 0
for i in range(width):
s = 0
t = 0
for j in range(height):
if thresh[j][i] == 255:
s += 1
if thresh[j][i] == 0:
t += 1
white_max = max(white_max, s)
black_max = max(black_max, t)
white.append(s)
black.append(t)
arg = False
if black_max > white_max:
arg = True
def find_end(start_):
end_ = start_ + 1
for m in range(start_ + 1, width - 1):
if (black[m] if arg else white[m]) > (0.95 * black_max if arg else 0.95 * white_max):
end_ = m
break
return end_
n = 1
start = 1
end = 2
i=0;
cj=[]
while n < width - 2:
n += 1
if (white[n] if arg else black[n]) > (0.05 * white_max if arg else 0.05 * black_max):
start = n
end = find_end(start)
n = end
if end - start > 5:
cj.append(thresh[1:height, start:end])
cv2.imwrite(output_path + '/' + str(i) + '.jpg', cj[i])
i += 1;
运行结果:
|