python放大_Python opencv的图像放大操作,pythonopencv,扩增

import cv2

import numpy as np

import os

img = cv2.imread('E:/pycharm_workspace/python_data_process/pic/fox.jpg')

def rotate_bound(image, angle):

# grab the dimensions of the image and then determine the

# center

(h, w) = image.shape[:2]

(cX, cY) = (w // 2, h // 2)

# grab the rotation matrix (applying the negative of the

# angle to rotate clockwise), then grab the sine and cosine

# (i.e., the rotation components of the matrix)

M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)

cos = np.abs(M[0, 0])

sin = np.abs(M[0, 1])

# compute the new bounding dimensions of the image

nW = int((h * sin) + (w * cos))

nH = int((h * cos) + (w * sin))

# adjust the rotation matrix to take into account translation

M[0, 2] += (nW / 2) - cX

M[1, 2] += (nH / 2) - cY

# perform the actual rotation and return the image

return cv2.warpAffine(image, M, (nW, nH))

def process_train_set(data_dir, save_dir, list_file_name):

file_list = os.path.join(data_dir, list_file_name)

with open(file_list, "r+") as flist:

read_data = flist.read()

flist.truncate()#清空文件

num=0

#对单张图像进行处理

for eachline in read_data.split('\n'):

img_path, label = os.path.split(eachline)

img_full_path = os.path.join(data_dir, img_path)

if(os.path.exists(img_full_path) == False):

continue

# 翻转操作(2种)

img_path_temp = FlipOperation(eachline,label, save_dir)

# 旋转90和180度

img_path_temp = Rotation(eachline, label, save_dir)

#对比度增强

#直方图正规化

img_path_temp = Normalize_image(eachline, label, save_dir)

# 伽马变换

img_path_temp = gamma_image(eachline, label, save_dir)

num=num+1

print("The "+str(num)+" th")

print("Image argument finished!")

def FlipOperation(eachline,label, save_dir):

middle_name = 'Flip'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img=cv2.imread(eachline)

#水平翻转

flipped1=cv2.flip(img,1)

cv2.imwrite(new_file_path + "/"+"Hor_" + label,flipped1)

#垂直翻转

flipped2 = cv2.flip(img, 0)

cv2.imwrite(new_file_path + "/" + "Vec_" + label, flipped2)

return new_file_path

#旋转90和180度

def Rotation(eachline,label, save_dir):

middle_name = 'Rotation'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img = cv2.imread(eachline)

rows,cols=img.shape[:2]

R_image1=cv2.getRotationMatrix2D((rows/2,cols/2),90,1)

dst = cv2.warpAffine(img, R_image1, (cols, rows))

dst = cv2.convertScaleAbs(img, alpha=1.5, beta=0.2)

R_image2 = cv2.getRotationMatrix2D((rows / 2, cols / 2), 180, 1)

dst1 = cv2.warpAffine(img, R_image2, (cols, rows))

cv2.imwrite(new_file_path + "/" + "R180_" + label, dst1)

return new_file_path

#缩放

def Resize(eachline,label, save_dir):

middle_name = 'Resize'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img = cv2.imread(eachline)

result=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)

cv2.imwrite(new_file_path + "/" + "2times_" + label, result)

return new_file_path

#任意角度翻转

def rotate_bound(image, angle):

# grab the dimensions of the image and then determine the

# center

(h, w) = image.shape[:2]

(cX, cY) = (w // 2, h // 2)

M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)

cos = np.abs(M[0, 0])

sin = np.abs(M[0, 1])

# compute the new bounding dimensions of the image

nW = int((h * sin) + (w * cos))

nH = int((h * cos) + (w * sin))

# adjust the rotation matrix to take into account translation

M[0, 2] += (nW / 2) - cX

M[1, 2] += (nH / 2) - cY

# perform the actual rotation and return the image

return cv2.warpAffine(image, M, (nW, nH))

#线性增强

def linear_enhance(eachline,label, save_dir):

middle_name = 'linear'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img = cv2.imread(eachline)

dst = cv2.convertScaleAbs(img, alpha=1.5, beta=0)

cv2.imwrite(new_file_path + "/" + label, dst)

#直方图正规化--增强亮度

def Normalize_image(eachline,label, save_dir):

middle_name = 'normalize'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img = cv2.imread(eachline)

dst = cv2.normalize(img, dst=None, alpha=350, beta=10, norm_type=cv2.NORM_MINMAX)

cv2.imwrite(new_file_path + "/" + label, dst)

#伽马变换--能提高对比度

def gamma_image(eachline,label, save_dir):

middle_name = 'gamma'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img = cv2.imread(eachline)

img_norm = img / 255.0 # 注意255.0得采用浮点数

img_gamma = np.power(img_norm, 0.4) * 255.0

dst = img_gamma.astype(np.uint8)

cv2.imwrite(new_file_path + "/" + label, dst)

#全局直方图均衡化--灰度图

def EqualizeHist(eachline,label, save_dir):

middle_name = 'EqualizeHist-gray'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img = cv2.imread(eachline,0)

dst = cv2.equalizeHist(img)

cv2.imwrite(new_file_path + "/" + label, dst)

#全局直方图均衡化--彩色图像

def EqualizeHist(eachline,label, save_dir):

middle_name = 'EqualizeHist-color'

new_file_path = os.path.join(save_dir, middle_name)

if not os.path.exists(new_file_path):

os.makedirs(new_file_path)

img = cv2.imread(eachline)

img0 = cv2.equalizeHist(img[:, :, 0]) # 各个通道分别均衡化

img1 = cv2.equalizeHist(img[:, :, 1])

img2 = cv2.equalizeHist(img[:, :, 2])

dst = cv2.merge([img0, img1, img2])

cv2.imwrite(new_file_path + "/" + label, dst)

data_dir="E:/pycharm_workspace/python_data_process/"

list_file_name ="image.txt"

save_dir="E:/pycharm_workspace/python_data_process/pic_argument1/"

process_train_set(data_dir,save_dir,list_file_name)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值