迎儿2329 2019-05-25 09:00 采纳率: 0%
浏览 157

图像处理问题,mask已知图片集和对应的excel表,我想同时遍历图和表中每一行,实现与运算,?

图像处理问题,已知图片集和对应的excel表,每一行存有一个图片感兴趣区域,我想同时遍历图和表中每一行,实现与运算,从而留下感兴趣区域,使不感兴趣区域变成黑色,但是此程序循环不对?


def Image_Resize(dir):    
    for parent,dirnames,filenames in os.walk(dir):      
        for filename in filenames:

            save_path = os.path.join('D:/Testing_mask/00000', filename)
            csv_data = pd.read_csv('D:/BLS_Test_Images_jpg/00000/GT-00000.csv')
            csv_data_array = np.array(csv_data)
            for i in range(csv_data_array.shape[0]):
                csv_data_list = np.array(csv_data)[i, :].tolist()[0].split(";")
                sample_dir = os.path.join(save_path, csv_data_list[0])
                img = cv2.imread(sample_dir)
                mask = np.zeros(img.shape[:2], np.uint8)
                mask[int(csv_data_list[3]):int(csv_data_list[5]), int(csv_data_list[4]):int(csv_data_list[6])] = 255

                cv2.imwrite(sample_dir, masked_img)


Image_Resize('D:/Testing_mask/00000')

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-09 19:52
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    import numpy as np
    from PIL import Image
    
    # Load the image and apply a threshold to it
    img = Image.open('path_to_image_file.jpg').convert('L') # convert to grayscale
    threshold = 127 # set the threshold value (adjust according to your needs)
    
    # Apply the threshold
    img = np.where(img < threshold, 0, 255).astype(np.uint8) 
    
    # Display the original and modified images side by side
    original = Image.fromarray(img)
    modified = Image.fromarray(255 - img)
    combined = Image.alpha_composite(original, modified)
    combined.show()
    

    这个代码会显示原始图像和经过阈值化后的图像。你可以根据你的需求调整阈值。

    评论

报告相同问题?