简体   繁体   English

colors skimage 的计数

[英]count number of colors skimage

I want to be able to count the number of colors in a part of an image.我希望能够在图像的一部分中计算 colors 的数量。

One way I thought about doing is plotting a histogram for the greyscale image and then counting the number of peaks, and that's the number of colors.我想到的一种方法是为灰度图像绘制直方图,然后计算峰值的数量,这就是 colors 的数量。


histogram, bin_edges = np.histogram(gray_image, bins = 256, range=(0,1))
plt.plot(histogram)
peaks = signal.find_peaks_cwt(histogram, 10)

When i do it this way, the signal.find_peaks_cwt says there too many indices for array.当我这样做时, signal.find_peaks_cwt 说数组索引太多。

How do I go about this or is there a better way to count the number of colors?我如何 go 关于这个或者有更好的方法来计算 colors 的数量?

This answer got a little to long for a comment:这个答案有点太长了,需要评论:

It is not quite clear to me what you are trying to do.我不太清楚您要做什么。 Do you want to count all distinguishable elements in an image, or do you want to count all shades of eg red as one color?您想计算图像中所有可区分的元素,还是要将所有色调(例如红色)计算为一种颜色?

Also images can be represented using different kind of datatypes (eg. float ∈ [-1, 1] or uint8 ∈ [0, 255]).也可以使用不同类型的数据类型来表示图像(例如float ∈ [-1, 1] 或uint8 ∈ [0, 255])。 In your first line you apparently expect to have an image of type float according to range=(0,1) .在您的第一行中,您显然希望根据range=(0,1)获得 float 类型的图像。 With binning you might loose the information about distinguishable elements and therefore not be able to count the number of distinguishable elements.通过分箱,您可能会丢失有关可区分元素的信息,因此无法计算可区分元素的数量。

Counting distinguishable colors计数可区分 colors

For counting all available colors (= distinguishable elements) in a gray scale image you can use the following one-liner.要计算灰度图像中所有可用的 colors(= 可区分元素),您可以使用以下单线。 (This will of course also work for float images. If you really want to distinguish between every color this is perfect, but if not your command np.histogram is a good idea.) (这当然也适用于float图像。如果你真的想区分每种颜色,这是完美的,但如果不是你的命令np.histogram是个好主意。)

len(set(gray_image.flatten()))

As for the error you got with scipy.signal.find_peaks_cwt(histogram, 10) , find_peaks_cwt() expects a 1D array as second argument.至于您使用scipy.signal.find_peaks_cwt(histogram, 10)得到的错误, find_peaks_cwt()需要一个一维数组作为第二个参数。 If you provide a numpy array it will work just fine.如果您提供 numpy 阵列,它将正常工作。

Counting clusters of similar colors计数相似的簇 colors

In case you want to cluster similar colors and not count them twice, there are different approaches you can choose.如果您想对类似的 colors 进行聚类而不计算两次,您可以选择不同的方法。 The keyword here is "color quantization".这里的关键词是“颜色量化”。 As shown in this post you can use clustering algorithms to quantize the colors used in the image.本文所示,您可以使用聚类算法来量化图像中使用的 colors。 After color quantization you can simply reshape the image to preserve the RGB tuples and use numpys unique method like that:颜色量化后,您可以简单地重塑图像以保留 RGB 元组并使用 numpys unique方法,如下所示:

import numpy as np

len(np.unique(color_image.reshape(-1, 3), axis = 0))

There are a lot of methods to reduce the number of colors.有很多方法可以减少colors的数量。 Pillow has two functions for that posterize and quantize . posterize有两个功能,分别是后处理和quantize a method with k-means using Scikit learn was shown in the post i mentioned earlier and also you could try to use a distance metric when using a predefined set of colors.我前面提到的帖子中展示了一种使用 Scikit learn 的 k-means 方法,您也可以在使用一组预定义的 colors 时尝试使用距离度量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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