【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)

 

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值