0

I have a NumPy array (1513,256,256,1) which actually contains 1513 gray images with size (256,256). Each image contains 4 classes (values are 0,1,2,3). 0 means background. But values are not an integer, they are float 32. I want to count the number of pixels or elements belong to each class. Data in unbalanced so I'm trying to balance it. I tried some methods but they didn't work. One thing is that the numbers are not integer so they belong to several classes, not just 4 classes. Does anyone have any idea how to get the number of elements of each class?

I used this code here, but it didn't work.

y_integers = np.argmax(masks, axis=1)
class_weights = compute_class_weight('balanced', np.unique(y_integers), y_integers)
d_class_weights = dict(enumerate(class_weights))

Mahyar
  • 57
  • 2
  • 4
  • 9

1 Answers1

0

Instead of testing equality, you can search for values i in the interval [i-0.5, i+0.5]. This will manage with the data being floats and you'll be able to perform your task pretty easily using numpy.histogram. Here is a small code:

import numpy as np

n, h, w = 10, 255, 255
arr = np.random.randint(0,4,(n,h,w,1)).astype('float')
counts = np.zeros((n, 4), dtype='int')
for i in range(n):
  counts[i] = np.histogram(arr[i].ravel(), bins=[-0.5, 0.5, 1.5, 2.5, 3.5])[0]

print('counts:\n', counts)

Output:

% python3 script.py
counts:
 [[16110 16041 16464 16410]
 [16258 16112 16397 16258]
 [16328 16270 16121 16306]
 [16142 16315 16330 16238]
 [16264 16269 16237 16255]
 [16159 16338 16224 16304]
 [16198 16285 16204 16338]
 [16298 16270 16381 16076]
 [16230 16281 16336 16178]
 [16385 16320 16198 16122]]

Note that each of the lines should sum up to 256*256. You can also modify bins=[-0.5, 0.5, 1.5, 2.5, 3.5] depending on your data.

If you want to balance the values, your problem is called histogram equalization. Here is a question that you may be able to adapt to solve your problem.

bousof
  • 1,241
  • 11
  • 13