import tensorflow as tf
import os
import numpy as np
import cv2
#打开路径
openpath2='D:\\pythonprocedure\\FineTuningAlexNet\\data\\Lym\\'
#保存路径
savepath2='D:\\pythonprocedure\\FineTuningAlexNet\\data\\Lymfull\\'
#载入图片 处理后保存到一个文件夹
def read__image(open_path,save_path):
nums=0
images=[]
for dir_image in os.listdir(open_path): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
full_path = os.path.abspath(os.path.join(open_path,dir_image))
if dir_image.endswith('.bmp'):
image = cv2.imread(full_path)
image_path = save_path+'%s-%s.jpg' % ('Cell',str(nums)) # 注意这里图片名一定要加上扩展名,否则后面imwrite的时候会报错
cv2.imwrite(image_path, image)
nums=nums+1
#载入图片,处理后保存到一个列表中
def GetImg(open_path):
patch=[]
for dir_image in os.listdir(open_path): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
full_path = os.path.abspath(os.path.join(open_path,dir_image))
if dir_image.endswith('.jpg'):
image = cv2.imread(full_path)
resImg=cv2.resize(image,(227,227))
patch.append(resImg)
return patch
#输入一张图片,产生旋转,缩放,长宽调整等形式
def DateArgutation(images):
img=images
imgInfo=img.shape
height=imgInfo[0]
width=imgInfo[1]
agutationimg=[]
#旋转3张
angle=[90,180,280]
for a in angle:
matRotate=cv2.getRotationMatrix2D((height*0.5,width*0.5),a,0.5)
rotateImg=cv2.warpAffine(img,matRotate,(height,width))
agutationimg.append( rotateImg)
#缩放3张
scale=[0.5,2,4]
for s in scale:
dstHeight=int(height*s)
dstWidth=int(width*s)
resImg=cv2.resize(img,(dstWidth,dstHeight))
agutationimg.append(resImg)
#长宽
change=[[40,50],[60,50],[60,40]]
for h,w in change:
reshwImg=cv2.resize(img,(h,w))
agutationimg.append( reshwImg)
print('success!')
return agutationimg
#打开路径
openpath1='D:\\pythonprocedure\\FineTuningAlexNet02\\data\\test3\\test\\'
#保存路径
savepath1='D:\\pythonprocedure\\FineTuningAlexNet02\\data\\test3\\testori\\'
read__image(openpath1,savepath1)