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

前端 未结 1 1379
滥情空心
滥情空心 2020-11-30 15:22

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 channe

相关标签:
1条回答
  • 2020-11-30 15:58

    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]])
    
    0 讨论(0)
提交回复
热议问题