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])

 

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以使用以下步骤找到每个通道的矩形外框并得到相应的分割阈值: 1. 将图像分离为RGB三个通道,可以使用OpenCV中的split函数。 2. 对于每个通道,使用递归函数来进行分割。在递归函数中,首先计算当前图像的直方图,并获取直方图中出现频率最高的像素值及其对应的像素数量。 3. 根据直方图中出现频率最高的像素值,使用threshold函数来将当前图像分割成两个部分。分别计算这两个部分的矩形外框。 4. 如果当前矩形外框的大小小于某个阈值,则停止递归,否则对每个矩形继续递归进行分割。 5. 在递归过程中,记录每个矩形外框的大小及对应的分割阈值。最终选择矩形外框最大的那个作为最终的分割结果。 下面是一个简单的示例代码,可以作为参考: ``` import cv2 import numpy as np def get_rect(img): h, w = img.shape[:2] mask = np.zeros((h+2, w+2), np.uint8) _, _, rect, _ = cv2.floodFill(img, mask, (0, 0), 255) return rect def get_threshold(img): hist = cv2.calcHist([img], [0], None, [256], [0, 256]) max_val = np.argmax(hist) _, thresh = cv2.threshold(img, max_val, 255, cv2.THRESH_BINARY) return get_rect(thresh), max_val def recursive_split(img): h, w = img.shape[:2] if h < 10 or w < 10: return None rects = [] thresholds = [] for i in range(3): channel = img[:,:,i] rect, threshold = get_threshold(channel) rects.append(rect) thresholds.append(threshold) max_rect = max(rects, key=lambda x: x[2]*x[3]) max_index = rects.index(max_rect) threshold = thresholds[max_index] _, max_thresh = cv2.threshold(img[:,:,max_index], threshold, 255, cv2.THRESH_BINARY) sub_rects = [recursive_split(max_thresh[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]) for rect in rects] return (max_thresh, rects[max_index], threshold, sub_rects) img = cv2.imread('test.jpg') result = recursive_split(img) cv2.imshow('result', result[0]) cv2.waitKey(0) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值