Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

numpy - How to count RGB or HSV channel combination in an image?

I use python opencv load an image which has shape (30, 100, 3), now want to count the frequency for all the colors, by color, I don't mean individual channel, I mean channel combination. Meaning 3 channel list, e.g. [255, 0, 0] for red, [255, 255, 0] for yellow, [100, 100, 100] for another color. So I want the last axis(channel) to be treated as a whole and count its frequency.

Is there any built-in function in opencv or numpy which can easily treat the 3 channel list as one element and count its frequency?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use np.unique with its new axis argument functionality that does grouping -

np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)]

Sample run -

In [56]: im
Out[56]: 
array([[[255, 255, 255],
        [255,   0,   0]],

       [[255,   0, 255],
        [255, 255, 255]]])

In [57]: np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)]
Out[57]: 
array([[255,   0,   0,   1],
       [255,   0, 255,   1],
       [255, 255, 255,   2]])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...