我需要一个方向来执行此操作。我遍历所有像素并通过getpixel函数获取值。接下来我应该做什么?如何从图像中获得最常用的rgb颜色?
2
A
回答
2
将它们汇总在Dictionary<Color, int>
中,您可以在其中保留每种颜色的计数。在对所有这些进行迭代之后,提取Value(count)排序的前5个。
一个不太顺利执行,但简单的解决方法是这样的:
(from c in allColors
group c by c into g
order by g.Count() descending
select g.Key).Take(5)
1
我不会写代码的你,但给你所需要的一般描述:
- 一种数据结构,保存每种颜色及其出现的次数
- 对于每个像素,如果颜色存在于您的数据结构中,则增加数字 2.a如果颜色不存在,请将其添加1计数
- 一旦你通过所有像素了,排序的计数结构,并获得前5
0
创建这样一个字典:
Dictionary<Color, int> dictColors = new Dictionary<Color, int>();
那么当你通过每个像素迭代,为此
Color color =GetPixel(x,y);
if(!dictColors.Contains(color))
{
dictColors.Add(color,0);
}
else
{
dictColors[color]++;
}
then take first five:
var top5 = dictColors.Take(5);
+0
你有没有试过这段代码? – 2012-07-29 20:45:48
4
这里是一个辅助函数来获取所有像素:
public static IEnumerable<Color> GetPixels(Bitmap bitmap)
{
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
Color pixel = bitmap.GetPixel(x, y);
yield return pixel;
}
}
}
如果你只需要的颜色(不含计数器):
using (var bitmap = new Bitmap(@"..."))
{
var mostUsedColors =
GetPixels(bitmap)
.GroupBy(color => color)
.OrderByDescending(grp => grp.Count())
.Select(grp => grp.Key)
.Take(5);
foreach (var color in mostUsedColors)
{
Console.WriteLine("Color {0}", color);
}
}
顺便说一句,这里是前5个最常用的颜色与柜台的选择:
using (var bitmap = new Bitmap(@"..."))
{
var colorsWithCount =
GetPixels(bitmap)
.GroupBy(color => color)
.Select(grp =>
new
{
Color = grp.Key,
Count = grp.Count()
})
.OrderByDescending(x => x.Count)
.Take(5);
foreach (var colorWithCount in colorsWithCount)
{
Console.WriteLine("Color {0}, count: {1}",
colorWithCount.Color, colorWithCount.Count);
}
}
相关问题
- 1. 获得从调色板图像RGB颜色和适用于二值图像
- 2. iphone:如何从图像中移除红色? (RGB颜色)
- 3. 从不同颜色的图像中获取RGB值
- 4. 在RGB图像中使用的最大颜色
- 5. 如何从RGB颜色创建位图
- 6. 如何将图像颜色从RGB 24位量化为RGB 6位(64色)?
- 7. 如何获得RGB数值相当于vb.net中的颜色
- 8. 24位图图像和16位图图像中的RGB颜色
- 9. 如何将图像从索引转换为RGB颜色usinh matlab?
- 10. 图像读取的RGB颜色?
- 11. OpenCV:如何填充每个像素的每个RGB颜色值的RGB图像?
- 12. 如何获得接近另一种颜色的颜色的rgb值?
- 13. 噪声kinect颜色(rgb)图像
- 14. RGB图像CMYK和软化颜色
- 15. 使用python检测图像中最常用的颜色
- 16. 以颜色对象的形式获得缓冲图像中像素的颜色
- 17. 如何获得matplotlib rgb颜色,给定colormap名称,BoundryNorm和'c ='?
- 18. 如何获取图像中像素的颜色(加载灰色)?
- 19. 从图片中获取像素颜色?
- 20. 获取色轮的图像的RGB值
- 21. 将RGB颜色转换为C中最接近的ACI颜色#
- 22. 将图像的颜色空间从RGB转换为HSV
- 23. 如何使用WinRT中的WriteableBitmapEx从图像中的点获取像素颜色?
- 24. java如何从图像大小约800x600获得平均RGB值
- 25. iOS:如何检测图像是否具有某种RGB颜色
- 26. 如何从js中的透明像素获得rgb
- 27. 查找最近的RGB颜色
- 28. RGB最接近预定义的颜色
- 29. 如何从像素获取颜色? OpenGL
- 30. OpenCV中的2个RGB图像之间的颜色一致
[什么都有你试过](http://whathaveyoutried.com)?你有什么方法考虑?堆栈溢出不是一个能够帮助您思考或为您工作的地方 - 请向我们展示您已经完成的工作,并解释您卡在哪里。 – Oded 2012-07-29 20:25:53