在pytorch的深度学习中,imgplt.imread()与cv2.imread()读取图片,并且如何使用class类和定义def函数

在pytorch1.11中进行图像的深度学习时,数据增强这块记录一下。主要通过“旋转”变换来学习class类的使用和def函数的写法。

import random
import scipy.ndimage as ndi
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as imgplt


def transform_matrix_offset_center(matrix, x, y):
    o_x = float(x) / 2 + 0.5
    o_y = float(y) / 2 + 0.5
    offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
    reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
    transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
    return transform_matrix


def apply_transform(x, 
					transform_matrix, 
					channel_index=2, 
					fill_mode='nearest', 
					cval=0., 
					order=1):
    x = np.rollaxis(x, channel_index, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]
    channel_images = [ndi.interpolation.affine_transform(x_channel,
                                                         final_affine_matrix,
                                                         final_offset,
                                                         order=order,
                                                         mode=fill_mode,
                                                         cval=cval) for x_channel in x]
    x = np.stack(channel_images, axis=0)
    x = np.rollaxis(x, 0, channel_index + 1)
    return x


'''旋转变换'''
def rotation(x, 
			rg=20, 
			is_random=False, 
			row_index=0, 
			col_index=1, 
			channel_index=2, 
			fill_mode='nearest', 
			cval=0.,
            order=1):
    if is_random:
        theta = np.pi / 180 * np.random.uniform(-rg, rg)
    else:
        theta = np.pi / 180 * rg
    rotation_matrix = np.array(
        [[np.cos(theta), -np.sin(theta), 0], 
        [np.sin(theta), np.cos(theta), 0], 
        [0, 0, 1]])

    h, w = x.shape[row_index], x.shape[col_index]
    
    # 调用函数transform_matrix_offset_center()
    transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)
    
	# 调用函数apply_transform()
    x = apply_transform(
        x,
        transform_matrix,
        channel_index,
        fill_mode,
        cval,
        order)
    return x

'''数据增强'''
def image_augmentation(img, 
					   row_index=0, 
					   col_index=1, 
					   channel_index=2, 
					   is_rotate=False, 
					   is_crop=False,
                       is_flip=False, 
                       is_shift=False):
    row = img.shape[row_index]
    col = img.shape[col_index]
    channel = img.shape[channel_index]
    if is_rotate:
        rint = random.randint(0, 1)
        if rint == 0:
            img = rotation(img, 
            			   rg=10, 
            			   is_random=True, 
            			   row_index=row_index, 
            			   col_index=col_index,
                           channel_index=channel_index)
    
    return img


def fun(img):
    image = image_augmentation(img, is_rotate=True)
    # plt.imshow(image)
    # plt.show()
    # print(image.shape)
    return image


# 读取原始图像
img_0 = imgplt.imread('C:\\Users\\Administrator\\Desktop\\梅花.jpg', 1)
print('img_0.shape:', '\n', img_0.shape)
plt.imshow(img_0)
plt.show()


# 旋转
img_1 = fun(img=img_0)
print('img_1.shape:', '\n', img_1.shape)
plt.imshow(img_1)
plt.show()


# resize
img_2 = cv2.resize(img_0, (668, 668), (0.5, 0.25))
print('img_2.shape:', '\n', img_2.shape)
plt.imshow(img_2)
plt.show()


# 改变通道数
img_3 = np.transpose(img_0, (2, 0, 1))
print('img_3.shape:', '\n', img_3.shape)
# plt.imshow(img_3)
plt.show()

程序运行后的结果:
在这里插入图片描述

总结

1 图像变换的基本操作

在运行过程中,如果用cv2.imread()读取图片、且通过plt.imshow()plt.show()来显示图片,图片显示有误,如下图:

import cv2
import matplotlib.pyplot as plt

# 读取原始图像
img_0 = cv2.imread('C:\\Users\\Administrator\\Desktop\\1.jpg', 1)
print('img_0.shape:', '\n', img_0.shape)
plt.imshow(img_0)
plt.show()

在这里插入图片描述
解决方法:
参考链接:matplotlib读取图片并显示
引入import matplotlib.image as imgplt,并用imgplt.imread读取图片,则显示正常,如下图:

import matplotlib.pyplot as plt
import matplotlib.image as imgplt

# 读取原始图像
img_0 = imgplt.imread('C:\\Users\\Administrator\\Desktop\\1.jpg', 1)
print('img_0.shape:', '\n', img_0.shape)
plt.imshow(img_0)
plt.show()

在这里插入图片描述

2 通过“旋转”变换来学习class类的使用和def函数的写法

import random
import scipy.ndimage as ndi
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as imgplt


def transform_matrix_offset_center(matrix, x, y):
    o_x = float(x) / 2 + 0.5
    o_y = float(y) / 2 + 0.5
    offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
    reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
    transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
    return transform_matrix


def apply_transform(x,
                    transform_matrix,
                    channel_index=2,
                    fill_mode='nearest',
                    cval=0.,
                    order=1):
    x = np.rollaxis(x, channel_index, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]
    channel_images = [ndi.interpolation.affine_transform(x_channel,
                                                         final_affine_matrix,
                                                         final_offset,
                                                         order=order,
                                                         mode=fill_mode,
                                                         cval=cval) for x_channel in x]
    x = np.stack(channel_images, axis=0)
    x = np.rollaxis(x, 0, channel_index + 1)
    return x


def rotation(x,
             rg=20,
             is_random=False,
             row_index=0,
             col_index=1,
             channel_index=2,
             fill_mode='nearest',
             cval=0.,
             order=1):
    if is_random:
        theta = np.pi / 180 * np.random.uniform(-rg, rg)
    else:
        theta = np.pi / 180 * rg
    rotation_matrix = np.array(
                                [[np.cos(theta), -np.sin(theta), 0],
                                [np.sin(theta), np.cos(theta), 0],
                                [0, 0, 1]])
    h, w = x.shape[row_index], x.shape[col_index]
    transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)
    x = apply_transform(
        x,
        transform_matrix,
        channel_index,
        fill_mode,
        cval,
        order)
    return x

''' 定义类'''
class MyReader:
    def __init__(self, iscolor=True):
        self.iscolor = iscolor

    def img_augmentation(self, img, row_index=0, col_index=1, channel_index=2, is_rotate=False):
        if is_rotate:
            rint = random.randint(0, 1)
            if rint == 0:
                img = rotation(img,
                               rg=10,
                               is_random=True,
                               row_index=row_index,
                               col_index=col_index,
                               channel_index=channel_index)
        return img

    def train_reader(self, img):
        self.img = img
        image = cv2.cvtColor(self.img, cv2.IMREAD_COLOR)
        image = self.img_augmentation(image, is_rotate=True)
        plt.imshow(image)
        plt.show()
        print(image.shape)
        return image


iscolor = True
myreader = MyReader(iscolor=iscolor)  # 初始化类
img_0 = imgplt.imread('C:\\Users\\Administrator\\Desktop\\梅花.jpg')  
myreader.train_reader(img=img_0)  # 调用类中的函数

运行完后,图片显示如下:
在这里插入图片描述

3.注意:如果用cv2.imread()读取文件时,文件及文件名最好是英文,否则会报错!!!

img_0 = cv2.imread('C:\\Users\\Administrator\\Desktop\\梅花.jpg')

在这里插入图片描述
改为英文状态后,除了颜色与原图不一样外,还是基本能显示出图像的。

img_0 = cv2.imread('C:\\Users\\Administrator\\Desktop\\meihua.jpg')

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
在这个代码,`img=cv2.imread(r"C:\Users\feifei\Desktop\222\6Q4A1934.JPG",1)`的参数`1`表示读取彩色图像。在OpenCV,当我们使用`cv2.imread()`函数读取图像时,可以通过不同的参数来指定读取模式。参数`1`表示读取彩色图像,而参数`0`表示读取灰度图像。因此,使用`img=cv2.imread(r"C:\Users\feifei\Desktop\222\6Q4A1934.JPG",1)`将会读取并返回一张彩色图像。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [在pytorch深度学习imgplt.imread()与cv2.imread()读取图片,并且如何使用class定义def函数](https://blog.csdn.net/weixin_44359479/article/details/123625918)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [cv2.imread(filename, 0)和先img=cv2.imread(filename, 1)再cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)得出...](https://blog.csdn.net/weixin_54555607/article/details/124760269)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明德zhuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值