pytorch中利用图像处理库transform将图像经过变换转换成张量,并可视化处理后的图像

# -*- coding: utf-8 -*-
# -----------------------------------------------------
# Time :  2021/7/14 上午8:39
# Auth :  Written by zuofengyuan
# File :  transform_.py
# Copyright (c) Shenyang Pedlin Technolofy Co., Ltd.
# -----------------------------------------------------
"""
 Description: TODO
"""
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import moco
from torch.autograd import  Variable
import numpy
from cv2 import imread
from torch.utils.data import Dataset
from mydataset import defectdataset
#from my_model_resnet50 import Model
from tqdm import tqdm
import torchvision
from resnet_model import all_pretrained_models
norm_mean = [0.485, 0.456, 0.406]
norm_std = [0.229, 0.224, 0.225]
import numpy as np
from PIL import  Image
data='/mnt/classify/test/1.bmp'##input path
img = Image.open(data)  # .convert('RGB')     # 0~255
# img=cv2.imread('/mnt/sda1/zfy/gai-yolo/classify/test/lf/XQⅡ-DJ021-M027-01_8.bmp')
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
###define process
image_transforms = {
    'test': transforms.Compose([transforms.Resize((448, 448)),                        transforms.RandomHorizontalFlip(p=0.5),                         transforms.RandomVerticalFlip(p=0.5),
                             transforms.RandomApply([moco.GaussianBlur([.1, 2.])], p=0.5),
                                transforms.ToTensor(),
                                #transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5),
transforms.Normalize(norm_mean, norm_std)])
}
transform = image_transforms['test']
img2 = transform(img)###image process
print(img2) ####
print(img2.size())
print(img2.numpy())
##tensor-PIL
img3=transforms.ToPILImage()(img2)
print(img3)
##PIL-array
re_img = np.asarray(img3)###
print(re_img)
import cv2
cv2.imwrite('/mnt/sda1/zfy/gai-yolo/lianxi /2/3.bmp',re_img)###save
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值