迎儿2329 2019-05-25 07:39 采纳率: 0%
浏览 312

交通标志图像处理,已知感兴趣区域.csv文件,如何循环使图片与.csv中每一行对应,从而进行mask处理?

已知感兴趣区域.csv文件,想通过循环使图片与.csv中每一行对应,从而进行mask处理,将不感兴趣区域变成黑色,但是好像循环的不对,恳请大家帮助

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:46
    关注

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

    import numpy as np
    from PIL import Image
    
    # Load the CSV file and extract the image paths and GT data
    csv_file = 'your_csv_file.csv'
    data = pd.read_csv(csv_file)
    
    # Define the directory where your images are stored
    image_directory = 'your_image_directory'
    
    # Iterate through each row of the CSV file
    for index, row in data.iterrows():
        # Extract the image path and GT data from the current row
        image_path = row['Image Path']
        gt_data = row['GT Data'].split(';')
    
        # Open the image using Pillow library
        img = Image.open(image_path)
    
        # Create a new image with transparent background
        new_img = Image.new("RGBA", img.size, (0, 0, 0, 0))
    
        # Paste the original image onto the new image
        new_img.paste(img, (0, 0), img.convert("RGBA"))
    
        # Get the width and height of the image
        w, h = img.size
    
        # Calculate the center of the image
        center_x = w / 2
        center_y = h / 2
    
        # Set the ROI to be the area specified by the GT data
        roi = (center_x - int(gt_data[1]), center_y - int(gt_data[2]),
               center_x + int(gt_data[3]) - int(gt_data[1]),
               center_y + int(gt_data[4]) - int(gt_data[2]))
    
        # Crop the image to the specified ROI
        cropped_img = new_img.crop(roi)
    
        # Save the cropped image with transparency
        cropped_img.save(f'{image_directory}/{row["ID"]}.png', "PNG")
    
    print("Done!")
    

    注意:这个脚本使用了Pillow库来操作图像。如果你还没有安装Pillow,请先在命令行中运行以下命令:

    pip install pillow
    

    然后替换your_csv_file.csvyour_image_directory为你的实际CSV文件路径和图像目录路径。

    评论

报告相同问题?