python-修改图像的尺寸和RGB三个通道的像素值(opencv)

def image2label(path,size_):
    w = size_[0]
    h = size_[1]
    label_im=cv2.imread(path)
    #修改图像的尺寸大小
    new_array = cv2.resize(label_im, (w, h), interpolation=cv2.INTER_CUBIC)
    data=np.array(new_array,dtype='int32')
    #修改B通道的像素值
    for i in range(data[:,:,0].shape[0]):
        for j in range(data[:,:,0].shape[1]):
            if data[:,:,0][i][j]>155:
                data[:,:,0][i][j]=255
            else:
                data[:,:,0][i][j]=0
    #修改G通道的像素值
    for i in range(data[:,:,1].shape[0]):
        for j in range(data[:,:,1].shape[1]):
            if data[:,:,1][i][j]>155:
                data[:,:,1][i][j]=255
            else:
                data[:,:,1][i][j]=0
    #修改R通道的像素值           
    for i in range(data[:,:,2].shape[0]):
        for j in range(data[:,:,2].shape[1]):
            if data[:,:,2][i][j]>155:
                data[:,:,2][i][j]=255
            else:
                data[:,:,2][i][j]=0
    
    return data

if __name__ =='__main__':
    import cv2
    import numpy as np
    from PIL import Image
    #修改的尺寸大小    
    size_=[320,480]
    img_path='/root/reid/reid_tutorial/train/0002_c1s1_000451_03.png'
    label = image2label(img_path,size_)
    #修改后的尺寸和修改后的像素值保存下来
    save_img='./0002_c1s1_000451_03.png'
    cv2.imwrite(save_img, label)
    print(label[100])

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值