补边不失真的resize(2)

图像补边不失真的resize

import cv2
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
import copy
from nets.deeplabv3_plus import DeepLab
import colorsys
from matplotlib import pyplot as plt

def preprocess_input(image):
    image /= 255.0
    return image

#---------------------------------------------------#
#   对输入图像进行resize
#---------------------------------------------------#

def resize_image(image, size):
    iw, ih  = image.size
    w, h    = size

    scale   = min(w/iw, h/ih)
    nw      = int(iw*scale)
    nh      = int(ih*scale)

    image   = image.resize((nw,nh), Image.BICUBIC)
    new_image = Image.new('RGB', size, (128,128,128))
    new_image.paste(image, ((w-nw)//2, (h-nh)//2))

    return new_image, nw, nh

#---------------------------------------------------------#
#   将图像转换成RGB图像,防止灰度图在预测时报错。
#   代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB
#---------------------------------------------------------#
def cvtColor(image):
    if len(np.shape(image)) == 3 and np.shape(image)[2] == 3:
        return image
    else:
        image = image.convert('RGB')
        return image

#---------------------------------------------------#
#   检测图片
#---------------------------------------------------#
def detect_image(image, count=False, name_classes=None):

    input_shape = [512, 512]
    # ---------------------------------------------------------#
    #   在这里将图像转换成RGB图像,防止灰度图在预测时报错。
    #   代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB
    # ---------------------------------------------------------#
    image = cvtColor(image)
    # ---------------------------------------------------#
    #   对输入图像进行一个备份,后面用于绘图
    # ---------------------------------------------------#
    old_img = copy.deepcopy(image)  # 备份
    orininal_h = np.array(image).shape[0]  # 图片原始的h
    orininal_w = np.array(image).shape[1]  # # 图片原始的w
    # ---------------------------------------------------------#
    #   给图像增加灰条,实现不失真的resize
    #   也可以直接resize进行识别
    # ---------------------------------------------------------#
    image_data, nw, nh = resize_image(image, (input_shape[1], input_shape[0]))
    pr = np.array(image_data)
    pr = pr[int((input_shape[0] - nh) // 2): int((input_shape[0] - nh) // 2 + nh), \
         int((input_shape[1] - nw) // 2): int((input_shape[1] - nw) // 2 + nw)]
    # ---------------------------------------------------#
    #   进行图片的resize
    # ---------------------------------------------------#
    pr = cv2.resize(pr, (orininal_w, orininal_h), interpolation=cv2.INTER_LINEAR)

    return old_img, image_data, pr, nw, nh


if __name__ == '__main__':
    img = "F:/rock_project/deeplabv3-plus-pytorch-main/img/street.jpg"
    # img = "F:/rock_data/Mars_Rover/MarsRocks/MarsData/MarsRocks/JPEGImages/331_0331MR0013390000301024E01_DXXX.jpg"
    # img = "./VOCdevkit/VOC2007/JPEGImages/000005.jpg"
    img = Image.open(img)
    old_img,image_data,pr, nw, nh =  detect_image(img, count=False, name_classes=None)
    print("old_img,image_data:",old_img.size, image_data.size)
    plt.figure(figsize=(10, 10))
    plt.subplot(1, 3, 1)
    plt.imshow(old_img)
    plt.title("original ground")

    plt.subplot(1, 3, 2)
    plt.imshow(image_data)
    plt.title("RGB img")

    plt.subplot(1, 3, 3)
    plt.imshow(pr)
    plt.title("pr img")
    plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值