【1】背景:如图所示,左边为图片的索引(默认为第一列),右边为图片名(默认为第二列)。根据左边的类别完成对右边图片的分类。
【2】代码
# -*- coding: utf-8 -*-
"""
author:songjian"""
import xlrd
import cv2
import os
#读取数据存入字典
def read_excel(name):
#打开excel表,填写路径
book = xlrd.open_workbook(name)
#找到sheet页
table = book.sheet_by_name(sheetname)
row_Num = table.nrows
col_Num = table.ncols
s =[]
key=table.col_values(1)
values=table.col_values(0)
print(key)
print(values)
#print(key)
if row_Num <= 1:
print("没数据")
else:
for i in range(row_Num):
d ={}
# 把key值对应的value赋值给key,每行循环
d[round(int(key[i]))]=round(int(values[i]))
# 把字典加到列表中
s.append(d)
return s
#读入图片进行分类
def read__image(open_path,index_name,sheetname,save_path):
nums=0
images=[]
#文件分类索引。键为待处理的图片名,值为被划分的类别数
picindex=read_excel(index_name)
#print(picindex)
for dir_image in os.listdir(open_path): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
full_path = os.path.abspath(os.path.join(open_path,dir_image))
#图片的类别分为123
class_image=0
if dir_image.endswith('.bmp'):
image = cv2.imread(full_path)
#判断图片的类别
#分离文件路径和文件名
(filepath, filename) = os.path.split(full_path)
#分离文件名和后缀,[0]表示选择文件名,[1]表示选择后缀
imagename = os.path.splitext(filename)[0]
for list_index in range(len(picindex)):
for key, value in picindex[list_index].items():
if str(key)==str(imagename):
class_image=value
print(class_image)
#粒细胞
sheet_image=str(sheetname)+str(imagename)
if class_image==1:
image_path = save_path[class_image-1]+'%s-%s.jpg' % ('Neu',sheet_image)
print(image_path)
cv2.imwrite(image_path, image)
#print("save successful")
#淋巴细胞
elif class_image==2:
image_path = save_path[class_image-1]+'%s-%s.jpg' % ('Lym',sheet_image)
cv2.imwrite(image_path, image)
#print("save successful")
#其他细胞
elif class_image==3:
image_path = save_path[class_image-1]+'%s-%s.jpg' % ('Other',sheet_image)
cv2.imwrite(image_path, image)
#print("save successful")
else:
#print("this image need to be delete")
print("delete successful")
nums=nums+1
info="nums:"+str(nums)
print(info)
if __name__ == '__main__':
#图片打开路径
open_path="D:\\pythonprocedure\\classimage\\4820\\"
#excel表格路径
name="4820.xls"
#excel表名
sheetname="4820"
#三个文件夹路径123
save_path=[]
path1="D:\\pythonprocedure\\classimage\\train\\"
path2="D:\\pythonprocedure\\classimage\\train\\"
path3="D:\\pythonprocedure\\classimage\\train\\"
save_path.append(path1)
save_path.append(path2)
save_path.append(path3)
read__image(open_path,name,sheetname,save_path)