【问题标题】:How to find the max occurred color in the picture using EMGU CV in C#?如何在 C# 中使用 EMGU CV 找到图片中出现的最大颜色?
【发布时间】:2016-03-30 15:35:54
【问题描述】:

我有一个“windows 控件”的图像,可以说是一个文本框,我想通过按像素颜色比较查找该图片中出现的最大颜色来获取文本框中所写文本的背景颜色。 我在谷歌搜索,我发现每个人都在谈论直方图,并且还给出了一些代码来找出图像的直方图,但没有人描述找到直方图后的过程。

我在一些网站上找到的代码是这样的

        // Create a grayscale image
        Image<Gray, Byte> img = new Image<Gray, byte>(bmp);
        // Fill image with random values
        img.SetRandUniform(new MCvScalar(), new MCvScalar(255));
        // Create and initialize histogram
        DenseHistogram hist = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
        // Histogram Computing
        hist.Calculate<Byte>(new Image<Gray, byte>[] { img }, true, null);

目前我使用了从图像中获取线段并找到最大颜色的代码,但这不是正确的方法。 目前使用的代码如下

        Image<Bgr, byte> image = new Image<Bgr, byte>(temp);
        int height = temp.Height / 2;
        Dictionary<Bgr, int> colors = new Dictionary<Bgr, int>();
        for (int i = 0; i < (image.Width); i++)
        {
            Bgr pixel = new Bgr();
            pixel = image[height, i];
            if (colors.ContainsKey(pixel))
                colors[pixel] += 1;
            else
                colors.Add(pixel, 1);                
        }
        Bgr result = colors.FirstOrDefault(x => x.Value == colors.Values.Max()).Key;

如果有人知道如何获得它,请帮助我。将此图像作为输入 ==>

【问题讨论】:

  • 您的第二种方法在这里是正确的,只需遍历所有像素(在 image.Height 上添加一个嵌套的 for 循环)
  • 你的图片是什么类型的?灰度还是彩色? RGB 还是别的什么?

标签: c# opencv emgucv


【解决方案1】:

Emgu.CV 的 DenseHistogram 公开了方法 MinMax(),它可以找到直方图的最大和最小 bin。

所以在你的第一个代码 sn-p 中计算你的直方图之后:

// Create a grayscale image
Image<Gray, Byte> img = new Image<Gray, byte>(bmp);
// Fill image with random values
img.SetRandUniform(new MCvScalar(), new MCvScalar(255));
// Create and initialize histogram
DenseHistogram hist = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
// Histogram Computing
hist.Calculate<Byte>(new Image<Gray, byte>[] { img }, true, null);

...用这种方法找到直方图的峰值:

float minValue, maxValue;
Point[] minLocation;
Point[] maxLocation;
hist.MinMax(out minValue, out maxValue, out minLocation, out maxLocation);

// This is the value you are looking for (the bin representing the highest peak in your
// histogram is the also the main color of your image).
var mainColor = maxLocation[0].Y;

【讨论】:

  • 有时这种方法也无法识别主色。请建议我适当的方法来做同样的事情。提前致谢
  • “无法识别主色”到底是什么意思?能否提供示例案例的屏幕截图或原始数据?
  • 上述问题的末尾有图片,颜色组合为 rgb(224, 226, 226) 但在执行以下代码后(自答代码。查看最后一个解决方案)我得到颜色代码 rgb( 42, 34, 38)
【解决方案2】:

我在 stackoverflow 中找到了一个代码 sn-p,它可以完成我的工作。 代码是这样的

        int BlueHist;
        int GreenHist;
        int RedHist;

        Image<Bgr, Byte> img = new Image<Bgr, byte>(bmp);
        DenseHistogram Histo = new DenseHistogram(255, new RangeF(0, 255));

        Image<Gray, Byte> img2Blue = img[0];
        Image<Gray, Byte> img2Green = img[1];
        Image<Gray, Byte> img2Red = img[2];


        Histo.Calculate(new Image<Gray, Byte>[] { img2Blue }, true, null);

        double[] minV, maxV;
        Point[] minL, maxL;
        Histo.MinMax(out minV, out  maxV, out minL, out maxL);

        BlueHist = maxL[0].Y;

        Histo.Clear();

        Histo.Calculate(new Image<Gray, Byte>[] { img2Green }, true, null);           

        Histo.MinMax(out minV, out  maxV, out minL, out maxL);

        GreenHist = maxL[0].Y;
        Histo.Clear();

        Histo.Calculate(new Image<Gray, Byte>[] { img2Red }, true, null);

        Histo.MinMax(out minV, out  maxV, out minL, out maxL);

        RedHist = maxL[0].Y;

【讨论】:

  • 有时这种方法也无法识别主色。请建议我适当的方法来做同样的事情。提前致谢
猜你喜欢
  • 2011-08-07
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
相关资源
最近更新 更多