【9】pthon-opencv批量讀取圖像並處理保存

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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章