我一直在网上寻找一种算法,它可以分析一幅图像,并返回最不同和最常用的颜色--但没有成功。
例如:
如果我有一个RGB,50% fully blue (0,0,255)和25% pink (255,0,255)格式的25% red (255,0,0)位图
我希望算法返回这三种(或更多,取决于可用颜色)在使用中排序的颜色,因此:
1. Blue
2. Red / Pink
3. Red / Pink谁知道有什么方法可以让我开始吗?也许一些文章可以阅读,等等。我从来没有在C#中使用过这样的图片。
发布于 2014-04-21 16:16:09
如果我正确理解了问题,这可能会对您有所帮助(当然,您必须根据自己的需要阅读数据):
/// <summary>
/// Gets the bitmap image color statistics
/// </summary>
/// <param name="bit">The bitmap image you want to analyze</param>
/// <returns></returns>
public static List<KeyValuePair<Color, int>> GetStatistics(Bitmap bit)
{
Dictionary<Color, int> countDictionary = new Dictionary<Color, int>();
for (int wid = 0; wid < bit.Width; wid++)
{//for every column in the image
for (int he = 0; he < bit.Height; he++)
{//for every row in the image
//Get the color of the pixel reached (i.e. at the current column and row)
Color currentColor = bit.GetPixel(wid, he);
//If a record already exists for this color, set the count, otherwise just set it as 0
int currentCount = (countDictionary.ContainsKey(currentColor) ? countDictionary[currentColor] : 0);
if (currentCount == 0)
{//If this color doesnt already exists in the dictionary, add it
countDictionary.Add(currentColor, 1);
}
else
{//If it exists, increment the value and update it
countDictionary[currentColor] = currentCount + 1;
}
}
}
//order the list from most used to least used before returning
List<KeyValuePair<Color, int>> l = countDictionary.OrderByDescending(o => o.Value).ToList();
return l;
}
}发布于 2014-04-21 16:21:03
只要用一点谷歌的努力,你就能找到直方图。如果你想使用阴影作为单独的颜色,那么你可以选择256^3颜色。因此,要么使用一些动态列表,要么忽略一些最低有效位,以将数字降低一点。您还可以通过归一化颜色来更改动态范围。
- black is black
- and for everything else change vector size to `Max` for example Max = 2^5-1 = 31归一化颜色= color * Max / | color |
现在的算法是:
对于Max = 31,
1. create a counter table `cnt` for all combinations of colors
的大小为2^15 = 32768。将整个表格设置为零。
int cnt32768;对于(int i=0;i<32768;i++) cnti=0;
2.遍历整个图像,并为每个像素
- normalize its color
- convert it to address (for example `adr = (R) | (G<<5) | (B<<10)`)
- increment its counter `cnt[adr]++;`
在此之后,您将在cnt[]中获得直方图。因此,现在按cnt的值对其进行索引排序,您就获得了按其使用情况排序的颜色
你是如何定义它的?我会使用直方图中的数据,并搜索其中2种颜色之间的max距离(在归一化之后)
D= |color1 -Color2
不需要sqrt它...如果您使用d^2,您将获得相同的结果。忽略cnt[adr]==0处的所有条目(即未使用的颜色)。这仍然是O(n^2) ..。更像是~T(n*n/2) ...在运行时术语中,n不是图像中的像素数。相反,它只是在图像中使用的不同颜色的数量要少得多…此外,在对直方图进行索引排序之后,删除/忽略所有的cnt[adr]==0条目,甚至是lover。
https://stackoverflow.com/questions/23192482
复制相似问题