【1】图像处理常用函数


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import  cv2
import numpy as np
from numpy.core.fromnumeric import resize

#创建一个不存在的文件夹
def  makefilepath(folder_path):   
    if not os.path.exists(folder_path): 
        os.makedirs(folder_path)


 
#从文件夹载入多种图片,处理后将结果保存到一个文件夹
#处理范围包括:图片类型的转换,名字转换,文件名导出
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('.jpg'):
            image = cv2.imread(full_path)
            p,f = os.path.split(full_path)    #切分文件名和路径
            n,ext = os.path.splitext(f)       #切分后缀和路径       
            image= cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC)
            image_path = (save_path+'%s.png' % (n))  # 注意这里图片名一定要加上扩展名,否则后面imwrite的时候会报错
            cv2.imwrite(image_path, image)
            nums=nums+1
    print("Output over!The total of image are %s ."%nums)

#导出图片的名字,写入到文件夹中
def Outputimgname(imgpath):
    nums=0
    images=[]
    for dir_image in os.listdir(imgpath): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
        full_path = os.path.abspath(os.path.join(imgpath,dir_image))
        if dir_image.endswith('.jpg'):
            image = cv2.imread(full_path)
            p,f = os.path.split(full_path)    #切分文件名和路径
            n,ext = os.path.splitext(f)       #切分后缀和路径 
            #将图片名写入txt文档
            with open("testdata.txt","a") as f:
                f.write(n)           
                f.write('\n')
            nums=nums+1
    print("Output over!The total of image are %s ."%nums)

#载入图片,处理后保存到一个列表中
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 


if __name__=='__main__':
    #打开路径
    openpath1="F:/pytorchdeeplearnbook/program/programs/data/VOC2012/01mylittletestdata/testdata01/"
    #保存路径
    savepath1="F:/pytorchdeeplearnbook/program/programs/data/VOC2012/01mylittletestdata/testdatalabel01/"
    read__image(openpath1,savepath1)


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值