# 将图片编码成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端统一实现方式,由于以...