C#找到图像的主要颜色 [英] C# find the majority color of an image

查看:482
本文介绍了C#找到图像的主要颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C#找到图像的大多数颜色。请有人帮助我。谢谢...

解决方案

对于每像素24位图像,您可以尝试下面的代码,这将提供最常用的颜色,有多少使用它的时间,并提供十大最常用颜色的列表,以及使用它们的次数。



免责声明:此代码已呈现仅用于教育目的;我很快就开发了它(在几个小时内),并没有对其准确性进行全面测试。您使用此代码需要您自担风险。



要使用此代码,您需要拥有一个24位/像素图像的文件路径:

 private string filep = @C:\ Users \You \Desktop / SomeFileName.jpg; 

然后将文件引用转换为Bitmap对象并调用类的main方法,如下所示:

位图bMap = Bitmap.FromFile(filep)为Bitmap; 

if(bMap == null)抛出新的FileNotFoundException(无法打开图片文件进行分析);

YourNameSpace.PictureAnalysis.GetMostUsedColor(bMap);

在调用'GetMostUsedColor后设置断点,并检查Class公开的四个公共变量中的结果:MostUsedColor,MostUsedColorIncidence ,TenMostUsedColors,TenMostUsedColors,TenMostUsedColorIncidences。

  using  System.Collections.Generic; 
使用 System.Linq;
使用 System.Drawing;

命名空间 YourNameSpace
{
public static class PictureAnalysis
{
public 静态列表<颜色> TenMostUsedColors { get ; private set ;}
public 静态列表< int> TenMostUsedColorIncidences { get ; private set ;}

public static Color MostUsedColor { get ; private set ;}
public static int MostUsedColorIncidence { get ; private set ;}

private static int pixelColor;

private static 字典< int,int> dctColorIncidence;

public static void GetMostUsedColor(Bitmap theBitMap)
{
TenMostUsedColors = new List< Color>();
TenMostUsedColorIncidences = new List< int>();

MostUsedColor = Color.Empty;
MostUsedColorIncidence = 0 ;

// 使用Dictionary< int,int>这里
// 与使用
相比真正获得回报 // Dictionary< Color,int> ?

// 使用SortedDictionary要慢得多,还是? / span>

dctColorIncidence = new Dictionary< int,int>();

// 这就是你想用非托管代码加速的原因
for int row = 0 ; row < theBitMap.Size.Width; row ++)
{
for int col = 0 ; col < theBitMap.Size.Height; col ++)
{
pixelColor = theBitMap.GetPixel(row,col).ToArgb();

if (dctColorIncidence.Keys.Contains(pixelColor))
{
dctColorIncidence [pixelColor] ++;
}
else
{
dctColorIncidence.Add(pixelColor, 1 );
}
}
}

// note有人认为
// .NET Generic Dictionary永远不会保证
// 按照此类方法排序
var dctSortedByValueHighToLow = dctColorIncidence.OrderByDescending(x = > x.Value).ToDictionary(x = > x.Key,x = > x.Value);

// 这应该替换为优雅的Linq?
foreach (KeyValuePair< int,int> kvp in dctSortedByValueHighToLow.Take( 10 ))
{
TenMostUsedColors.Add(Color.FromArgb(kvp.Key));
TenMostUsedColorIncidences.Add(kvp.Value);
}

MostUsedColor = Color.FromArgb(dctSortedByValueHighToLow.First()。Key);
MostUsedColorIncidence = dctSortedByValueHighToLow.First()。Value;
}

}
}

注意:有关订购通用词典的可能问题,请参阅此处的讨论:[ ^ ]。


试试这个:一个简单的直方图显示控件 [ ^ ]


查看此页面,它适合您的问题

http://www.vcskicks.com/dominant-color.php

I need to find the majority color of an image using C#. Please anybody help me. Thank you...

解决方案

For 24-bit-per-pixel images you can try the code below, which will make available the most frequently used color, how many times it is used, and also make available a list of the top ten most frequently used colors, and how many times they are used.

Disclaimer: this code is presented for educational purposes only; I developed it quickly (in a few hours), and it is not fully tested for accuracy. You use this code at your own risk.

To use this code you need to have a file-path to a 24 bit-per-pixel image:

private string filep = @"C:\Users\You\Desktop/SomeFileName.jpg";

And then turn the file reference into a Bitmap object and call the main method of the Class like this:

Bitmap bMap = Bitmap.FromFile(filep) as Bitmap;

if (bMap == null) throw new FileNotFoundException("Cannot open picture file for analysis");

YourNameSpace.PictureAnalysis.GetMostUsedColor(bMap);

Set a breakpoint after the call to 'GetMostUsedColor, and examine the results in the four public variables the Class exposes: MostUsedColor, MostUsedColorIncidence, TenMostUsedColors, TenMostUsedColors, TenMostUsedColorIncidences.

using System.Collections.Generic;
using System.Linq;
using System.Drawing;

namespace YourNameSpace
{
    public static class PictureAnalysis
    {
        public static List<Color> TenMostUsedColors {get; private set;}
        public static List<int> TenMostUsedColorIncidences {get; private set;}

        public static Color MostUsedColor {get; private set;}
        public static int MostUsedColorIncidence {get; private set;}

        private static int pixelColor;

        private static Dictionary<int, int> dctColorIncidence;

        public static void GetMostUsedColor(Bitmap theBitMap)
        {
            TenMostUsedColors = new List<Color>();
            TenMostUsedColorIncidences = new List<int>();

            MostUsedColor = Color.Empty;
            MostUsedColorIncidence = 0;

            // does using Dictionary<int,int> here
            // really pay-off compared to using
            // Dictionary<Color, int> ?

            // would using a SortedDictionary be much slower, or ?

            dctColorIncidence = new Dictionary<int, int>();

            // this is what you want to speed up with unmanaged code
            for (int row = 0; row < theBitMap.Size.Width; row++)
            {
                for (int col = 0; col < theBitMap.Size.Height; col++)
                {
                    pixelColor = theBitMap.GetPixel(row, col).ToArgb();

                    if (dctColorIncidence.Keys.Contains(pixelColor))
                    {
                        dctColorIncidence[pixelColor]++;
                    }
                    else
                    {
                        dctColorIncidence.Add(pixelColor, 1);
                    }
                }
            }

            // note that there are those who argue that a
            // .NET Generic Dictionary is never guaranteed
            // to be sorted by methods like this
            var dctSortedByValueHighToLow = dctColorIncidence.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

            // this should be replaced with some elegant Linq ?
            foreach (KeyValuePair<int, int> kvp in dctSortedByValueHighToLow.Take(10))
            {
                TenMostUsedColors.Add(Color.FromArgb(kvp.Key));
                TenMostUsedColorIncidences.Add(kvp.Value);
            }

            MostUsedColor = Color.FromArgb(dctSortedByValueHighToLow.First().Key);
            MostUsedColorIncidence = dctSortedByValueHighToLow.First().Value;
        }

    }
}

Note: For possible issues with ordering a Generic Dictionary see discussion here: [^].


Try this : A simple histogram displaying control[^]


Check out this page, it fits to your question
http://www.vcskicks.com/dominant-color.php


这篇关于C#找到图像的主要颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆