# -*- 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
pytorch中利用图像处理库transform将图像经过变换转换成张量,并可视化处理后的图像
最新推荐文章于 2023-03-23 12:59:00 发布