numpy: unique list of colors in the image

前端 未结 5 1171
眼角桃花
眼角桃花 2020-12-16 15:54

I have an image img:

>>> img.shape
(200, 200, 3)

On pixel (100, 100) I have a nice color:

>>>         


        
5条回答
  •  忘掉有多难
    2020-12-16 16:17

    The question about unique colors (or more generally unique values along a given axis) has been also asked here (in particular, see this answer). If you're seeking for the fastest available option then "void view" would be your weapon of choice:

    axis=2
    np.unique(
            img.view(np.dtype((np.void, img.dtype.itemsize*img.shape[axis])))
            ).view(img.dtype).reshape(-1, img.shape[axis])
    

    For any questions related to what the script actually does, I refer the reader to the links above.

提交回复
热议问题