np.unique函数获取mask图片中不同像素数

np.unique函数获取mask图片中不同像素数

@Data 2022/12/9

代码:

import os
import numpy as np
from PIL import Image
def unique_values(root):
imgs = os.listdir(root)
concat_unique = np.empty(1)
for imgpath in imgs:
img = np.asarray(Image.open(os.path.join(root, imgpath)))
# 得到像素中的不同数
unique = np.unique(img)
# 对其进行拼接
concat_unique = np.concatenate([concat_unique, unique])
# 对拼接后的图片进行再次求不同像素,即全部文件中不同像素数,排序后返回
return list(sorted(np.unique(concat_unique)))
if __name__ == '__main__':
root = r'./out'
unique = unique_values(root)
print(unique)

输出:

>>[0.0 15.0]

posted @   chendsome  阅读(9)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示