# 将图片编码成rle格式
from torchvision.transforms import transforms
def rle_encode(img, min_max_threshold=1e-3, max_mean_threshold=None):
if np.max(img) < min_max_threshold:
return ''
if max_mean_threshold and np.mean(img) > max_mean_threshold:
return ''
pixels = img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x) for x in runs)
# 将rle格式进行解码为图片
def rle_decode(mask_rle, shape=(512, 512)):
s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape, order='F')
rle编码转换
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 网络上下载的好多文本是GBK的编码,在mac电脑上打开是乱码 除了下载有多种编码的文本编辑器外,通过终端也可以进行...
- public static string get_uft8(string unicodeString){U...
- 这篇文章主要是将go语言实现的版本改为C/C++版本实现,主要思路是一样的,具体思路请看:GO代码实现判断字符编码...
- 最近用golang采集网页中遇到了各种不能识别的的乱码字符串,他们大多编码是gbk、gb2312、big5、win...
- GitHub Flavored Markdown 今天研究了一天Markdown移动端和pc端统一实现方式,由于以...