如何在C#中使用EMGU CV查找图片中出现的最大颜色? [英] How to find the max occurred color in the picture using EMGU CV in C#?

查看:91
本文介绍了如何在C#中使用EMGU CV查找图片中出现的最大颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个"Windows控件"的图像,可以说是一个文本框",我想通过逐像素颜色比较来查找该图片中出现的最大颜色,从而获得写在文本框中的文本的背景色.我在google上搜索时发现,每个人都在谈论直方图,并且给出了一些代码来查找图像的直方图,但没有人在找到直方图后描述了该过程.

I have an image of a "windows control" lets say a Text-box and I want to get background color of the text written within the text box by finding max color occurred in that picture by pixel color comparison. I searched in google and I found that every one is talking about histogram and also some code is given to find out histogram of an image but no one described the procedure after finding histogram.

我在某些网站上找到的代码就像

the code I found on some sites is like

        // 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);

目前,我已经使用了从图像中提取一条线段并找到最大颜色的代码,但这并不是正确的方法.当前使用的代码如下

Currently I have used the code which takes a line segment from the image and finds the max color but which is not the right way to do it. the currently used code is as follows

        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;

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

please help me if any one knows how to get it. Take this image as input ==>

推荐答案

Emgu.CV的

Emgu.CV's DenseHistogram exposes the method MinMax() which finds the maximum and minimum bin of the histogram.

因此,像在您的第一个代码段中一样计算完直方图之后:

So after computing your histogram like in your first code snippet:

// 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);

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

...find the peak of the histogram with this method:

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;

这篇关于如何在C#中使用EMGU CV查找图片中出现的最大颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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