设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间18 现在时间18 缓存数据 你我最后竟然也平凡到自命不凡。

你我最后竟然也平凡到自命不凡。 -- 像我这样的人

查看: 296|回复: 0

基于Python实现RLE格式分割标注文件的格式转换

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:236
  • 打卡月天数:0
  • 打卡总奖励:3557
  • 最近打卡:2025-04-19 09:40:05
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
404
主题
384
精华
0
金钱
4801
积分
864
注册时间
2023-1-6
最后登录
2025-4-19

发表于 2023-2-10 22:59:37 | 显示全部楼层 |阅读模式
1.Airbus Ship Detection Challenge

url: https://www.kaggle.com/competitions/airbus-ship-detection
Find ships on satellite images as quickly as possible
Data Description
In this competition, you are required to locate ships in images, and put an aligned bounding box segment around the ships you locate. Many images do not contain ships, and those that do may contain multiple ships. Ships within and across images may differ in size (sometimes significantly) and be located in open sea, at docks, marinas, etc.
For this metric, object segments cannot overlap. There were a small percentage of images in both the Train and Test set that had slight overlap of object segments when ships were directly next to each other. Any segments overlaps were removed by setting them to background (i.e., non-ship) encoding. Therefore, some images have a ground truth may be an aligned bounding box with some pixels removed from an edge of the segment. These small adjustments will have a minimal impact on scoring, since the scoring evaluates over increasing overlap thresholds.
The train_ship_segmentations.csv file provides the ground truth (in run-length encoding format) for the training images. The sample_submission files contains the images in the test images.
Please click on each file / folder in the Data Sources section to get more information about the files.
  1. kaggle competitions download -c airbus-ship-detection
复制代码
2.数据展示


2.1 标注数据

该数据以csv格式存储,具体如下:
000113p20ddjsliei6n6ed.png


2.2 图象文件

000113pg63o4g0v34gda44.jpeg

000114o5uv2eofculcv33e.jpeg

000114ie3z3accydevsnbn.jpeg


3.格式转换

由于图太多,暂时转换10个
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-

  3. import numpy as np  # linear algebra
  4. import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
  5. from PIL import Image


  6. # ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode
  7. # 将图片编码成rle格式
  8. def rle_encode(img, min_max_threshold=1e-3, max_mean_threshold=None):
  9.     '''
  10.     img: numpy array, 1 - mask, 0 - background
  11.     Returns run length as string formated
  12.     '''
  13.     if np.max(img) < min_max_threshold:
  14.         return ''  ## no need to encode if it's all zeros
  15.     if max_mean_threshold and np.mean(img) > max_mean_threshold:
  16.         return ''  ## ignore overfilled mask
  17.     pixels = img.T.flatten()
  18.     pixels = np.concatenate([[0], pixels, [0]])
  19.     runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
  20.     runs[1::2] -= runs[::2]
  21.     return ' '.join(str(x) for x in runs)


  22. # 将图片从rle解码
  23. def rle_decode(mask_rle, shape=(768, 768)):
  24.     '''
  25.     mask_rle: run-length as string formated (start length)
  26.     shape: (height,width) of array to return
  27.     Returns numpy array, 1 - mask, 0 - background
  28.     '''
  29.     s = mask_rle.split()
  30.     starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
  31.     starts -= 1
  32.     ends = starts + lengths
  33.     img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
  34.     for lo, hi in zip(starts, ends):
  35.         # img[lo:hi] = 1
  36.         img[lo:hi] = 255 #方便可视化
  37.     return img.reshape(shape).T  # Needed to align to RLE direction


  38. def masks_as_image(in_mask_list):
  39.     # Take the individual ship masks and create a single mask array for all ships
  40.     all_masks = np.zeros((768, 768), dtype=np.uint8)
  41.     for mask in in_mask_list:
  42.         if isinstance(mask, str):
  43.             all_masks |= rle_decode(mask)
  44.     return all_masks


  45. # 将目标路径下的rle文件中所包含的所有rle编码,保存到save_img_dir中去
  46. def rle_2_img(train_rle_dir, save_img_dir):
  47.     masks = pd.read_csv(train_rle_dir)
  48.     not_empty = pd.notna(masks.EncodedPixels)
  49.     print(not_empty.sum(), 'masks in', masks[not_empty].ImageId.nunique(), 'images')
  50.     print((~not_empty).sum(), 'empty images in', masks.ImageId.nunique(), 'total images')
  51.     all_batchs = list(masks.groupby('ImageId'))
  52.     train_images = []
  53.     train_masks = []
  54.     i = 0
  55.     for img_id, mask in all_batchs[:10]:
  56.         c_mask = masks_as_image(mask['EncodedPixels'].values)
  57.         im = Image.fromarray(c_mask)
  58.         im.save(save_img_dir + img_id.split('.')[0] + '.png')
  59.         print(i, img_id.split('.')[0] + '.png')
  60.         i += 1

  61.     return train_images, train_masks


  62. if __name__ == '__main__':
  63.     rle_2_img('train_ship_segmentations_v2.csv',
  64.               'mask/')
复制代码
其中为了方便查看,原计划0为背景,1为mask,为了方便显示,设置为255为mask。

4.转换结果

000114d6m7ygsjmsb7nmme.png

000114zkhs1vds90kfkvh1.png

000114oi4qeskidqdnpisp.png

000115mv7m5diyy3vwwved.png

000115q069k2z77o2r2ko5.png

000115wlnjumkxznwxxnz8.png

000115lnn4hqsx9az6ipsb.png

000116hplcvpz81vdvqzvv.png

000116khl6owg6ygowq9vo.png

000116wzfvddrvjnd8vvv1.png

到此这篇关于基于Python实现RLE格式分割标注文件的格式转换的文章就介绍到这了,更多相关Python RLE文件内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表