欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-28 14:24:38
...
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')
    #修改R通道的像素值
    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
    #修改B通道的像素值           
    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])

 

相关标签: python