项目实训 No.7
修改批处理算法
1.实现自动创建文件夹,然后按照对应路径自动保存
2.实现保存中文路径
3.实现绘制对比图,并保存
4.解决plt保存多张图片出错问题
for imcounts in range(image_len):
path=dir_path + f_namelist[imcounts]
sPath=save_path + f_namelist[imcounts]
cPath = c_save_path + f_namelist[imcounts]
skePath = ske_save_path + f_namelist[imcounts]
print("spath:"+save_path)
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#-------------------------------------迭代法---------------------------------#
img_bin=iteration_fun(img)
#cv2.imshow("iada_mean", img_bin)
tPath=R_save_path+'迭代法/'+f_pathlist[essaycounts]
if not os.path.exists(tPath):
os.makedirs(tPath)
sPath=R_save_path+'迭代法/'+f_pathlist[essaycounts] + "/"+f_namelist[imcounts]
print(sPath)
cv2.imencode('.png', img_bin)[1].tofile(sPath)
#cv2.imwrite("img_bin.png",img_bin)
#cv2.waitKey(0)
tPath = Ske_save_path + '迭代法/' + f_pathlist[essaycounts]
if not os.path.exists(tPath):
os.makedirs(tPath)
ske=sktest3(img_bin)
skePath = Ske_save_path + '迭代法/' + f_pathlist[essaycounts] + "/" + f_namelist[imcounts]
print(skePath)
#cv2.imencode('.png', ske)[1].tofile(skePath)
plt.clf()
plt.imshow(ske, plt.cm.gray)
plt.xticks([]), plt.yticks([]) # 隐藏坐标轴
fig1 = plt.gcf()
plt.show()
fig1.savefig(skePath, dpi=100)
tPath = C_save_path + '迭代法/' + f_pathlist[essaycounts]
if not os.path.exists(tPath):
os.makedirs(tPath)
cPath= C_save_path + '迭代法/' + f_pathlist[essaycounts]+ "/" + f_namelist[imcounts]
plt.clf()
titles = ['original', 'segment','skeleton']
images = [img,img_bin,ske ]
# 使用Matplotlib显示
for i in range(3):
plt.subplot(1, 3, i + 1)
plt.imshow(images[i],plt.cm.gray)
plt.title(titles[i], fontsize=20)
plt.xticks([]), plt.yticks([]) # 隐藏坐标轴
fig = plt.gcf()
#plt.show()
fig.savefig(cPath, dpi=100)
完成整个批处理算法
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import morphology,data,color
def file_name(dir_path):
global f_name
global f_namelist#文件或文件夹名称(图片)
f_name = []
f_namelist = []
for files in os.listdir(dir_path):
f_namelist.append(files)
for i in f_namelist:#分割后缀
index = i.rfind('.')
f_name.append(i[:index])
print(f_namelist)
return f_name,f_namelist
def iteration_fun(img):
#迭代法
T = img.mean()
while True:
t0 = img[img < T].mean()
t1 = img[img >= T].mean()
t = (t0 + t1) / 2
if abs(T - t) < 1:
break
T = t
T = int(T)
print(f"Best threshold = {T}")
#th, img_bin = cv2.threshold(img, T, 255, 0)
th, img_bin = cv2.threshold(img, T, 255, cv2.THRESH_BINARY_INV)
return img_bin
def Adaptive_Mean(img):
#自适应均值
ada_mean = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 4)
return ada_mean
def Adaptive_Gaussian(img):
#自适应高斯
ada_Gaussian = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 17, 6)
return ada_Gaussian
def simple_fun(img):
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, th2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, th3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, th4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, th5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
return th1,th2,th3,th4,th5
def sktest3(img):
img[img == 255] = 1
# 实施骨架算法
skeleton = morphology.skeletonize(img)
return skeleton
if __name__ == "__main__":
# img = cv2.imread("8888.png")
R_dir_path='./image/'
R_save_path='./result/'
C_save_path = './compare/'
Ske_save_path = './skeleton/'
f_name, f_pathlist = file_name(R_dir_path)
print(len(f_pathlist))
for essaycounts in range(len(f_pathlist)):
dir_path = R_dir_path + f_pathlist[essaycounts] + "/"
save_path = R_save_path + f_pathlist[essaycounts] + "/"
c_save_path = C_save_path + f_pathlist[essaycounts] + "/"
ske_save_path = Ske_save_path + f_pathlist[essaycounts] + "/"
f_name, f_namelist = file_name(dir_path)
image_len = len(f_name)
for imcounts in range(image_len):
path=dir_path + f_namelist[imcounts]
sPath=save_path + f_namelist[imcounts]
cPath = c_save_path + f_namelist[imcounts]
skePath = ske_save_path + f_namelist[imcounts]
print("spath:"+save_path)
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#-------------------------------------迭代法---------------------------------#
img_bin=iteration_fun(img)
#cv2.imshow("iada_mean", img_bin)
tPath=R_save_path+'迭代法/'+f_pathlist[essaycounts]
if not os.path.exists(tPath):
os.makedirs(tPath)
sPath=R_save_path+'迭代法/'+f_pathlist[essaycounts] + "/"+f_namelist[imcounts]
print(sPath)
cv2.imencode('.png', img_bin)[1].tofile(sPath)
#cv2.imwrite("img_bin.png",img_bin)
#cv2.waitKey(0)
tPath = Ske_save_path + '迭代法/' + f_pathlist[essaycounts]
if not os.path.exists(tPath):
os.makedirs(tPath)
ske=sktest3(img_bin)
skePath = Ske_save_path + '迭代法/' + f_pathlist[essaycounts] + "/" + f_namelist[imcounts]
print(skePath)
#cv2.imencode('.png', ske)[1].tofile(skePath)
plt.clf()
plt.imshow(ske, plt.cm.gray)
plt.xticks([]), plt.yticks([]) # 隐藏坐标轴
fig1 = plt.gcf()
plt.show()
fig1.savefig(skePath, dpi=100)
tPath = C_save_path + '迭代法/' + f_pathlist[essaycounts]
if not os.path.exists(tPath):
os.makedirs(tPath)
cPath= C_save_path + '迭代法/' + f_pathlist[essaycounts]+ "/" + f_namelist[imcounts]
plt.clf()
titles = ['original', 'segment','skeleton']
images = [img,img_bin,ske ]
# 使用Matplotlib显示
for i in range(3):
plt.subplot(1, 3, i + 1)
plt.imshow(images[i],plt.cm.gray)
plt.title(titles[i], fontsize=20)
plt.xticks([]), plt.yticks([]) # 隐藏坐标轴
fig = plt.gcf()
#plt.show()
fig.savefig(cPath, dpi=100)
基于上面的批处理算法,跑所有图片,得到对比图,图像分割图,骨干图