简体   繁体   中英

Count all different pixel values given a set of images

In order to sanity check masks for the semantic segmentation task, I would like to know how I can find all different pixel values, given a set of images.
I tried:

l = []
for img in glob.glob('/content/Maschere/*png'):
  im = Image.open(img)
  data = torch.from_numpy(np.asarray(im))
  v = torch.unique(data)
  l.append(v)

print(set(l))

The aforementioned code displays the unique pixel values per image, instead, I want get the unique for the whole set of images

NOTE:
I get this output format:

{tensor([  2, 255], dtype=torch.uint8), tensor([  2, 255], dtype=torch.uint8), tensor([  2, 255], dtype=torch.uint8), tensor([  2, 255], dtype=torch.uint8), tensor([  3, 255], dtype=torch.uint8), tensor([  9, 255], dtype=torch.uint8)

I would get this kind of result instead :

tensor([  2, 3, 9 255], dtype=torch.uint8)

I didnt' test it, but something along the lines of:

l = set()
for img in glob.glob('/content/Maschere/*png'):
  im = Image.open(img)
  data = torch.from_numpy(np.asarray(im))
  v = set(torch.unique(data))
  l.update(v)

print(l)

It maintains a single set which you update with any new values you encounter.

Solved using .getdata()

l = []
for img in glob.glob('/content/Maschere/*png'):
  pxls_values = set(list(Image.open(img).getdata()))
  for i in pxls_values:
    l.append(i)

l = set(l)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM