如何在C#中使用EMGU CV查找图片中出现的最大颜色?
我有一个“windows控件”的图像,比如说一个文本框,我想通过像素颜色比较查找图片中出现的最大颜色来获取文本框中写入的文本的背景颜色。 我在谷歌上搜索,发现每个人都在谈论直方图,并且给出了一些代码来找出图像的直方图,但没有人描述找到直方图后的过程 我在一些网站上找到的代码如下如何在C#中使用EMGU CV查找图片中出现的最大颜色?,c#,opencv,emgucv,C#,Opencv,Emgucv,我有一个“windows控件”的图像,比如说一个文本框,我想通过像素颜色比较查找图片中出现的最大颜色来获取文本框中写入的文本的背景颜色。 我在谷歌上搜索,发现每个人都在谈论直方图,并且给出了一些代码来找出图像的直方图,但没有人描述找到直方图后的过程 我在一些网站上找到的代码如下 // Create a grayscale image Image<Gray, Byte> img = new Image<Gray, byte>(bmp);
// 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);
//创建灰度图像
图像img=新图像(bmp);
//用随机值填充图像
img.SetRandUniform(new MCvScalar(),new MCvScalar(255));
//创建并初始化直方图
DenseShistogram hist=新的DenseShistogram(256,新范围(0.0f,255.0f));
//直方图计算
计算(新图像[]{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;
图像图像=新图像(临时);
内部高度=温度高度/2;
字典颜色=新字典();
对于(int i=0;i<(image.Width);i++)
{
Bgr像素=新的Bgr();
像素=图像[高度,i];
if(颜色。容器(像素))
颜色[像素]+=1;
其他的
颜色。添加(像素,1);
}
Bgr result=colors.FirstOrDefault(x=>x.Value==colors.Values.Max()).Key;
如果有人知道如何得到它,请帮助我。将此图像作为输入==>我在stackoverflow中找到了一个代码片段,它完成了我的工作。 代码是这样的
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;
intbluehist;
国际绿色主义者;
int RedHist;
图像img=新图像(bmp);
DenseShistogram Histo=新的DenseShistogram(255,新范围f(0255));
图像img2Blue=img[0];
图像img2Green=img[1];
图像img2Red=img[2];
历史计算(新图像[]{img2Blue},真,空);
双[]最小值,最大值;
点[]最小,最大;
历史最小最大值(输出最小值、输出最大值、输出最小值、输出最大值);
BlueHist=maxL[0].Y;
透明组织();
历史计算(新图像[]{img2Green},真,空);
历史最小最大值(输出最小值、输出最大值、输出最小值、输出最大值);
GreenHist=maxL[0].Y;
透明组织();
历史计算(新图像[]{img2Red},真,空);
历史最小最大值(输出最小值、输出最大值、输出最小值、输出最大值);
RedHist=maxL[0].Y;
Emgu.CV公开了查找直方图最大和最小仓位的方法
因此,在像第一个代码片段中那样计算直方图之后:
// 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.Height
上添加嵌套for循环)您的图像的类型是什么?灰度还是彩色?RGB或其他什么?有时这种方法也无法识别主色。请建议我做同样的事情的适当方法。有时这种方法也无法识别主色。请建议我做同样的事情的适当方法。提前感谢“无法识别主色调”的确切含义是什么?你能提供一个例子的屏幕截图或原始数据吗?上面的问题末尾有图片,有颜色组合rgb(224226226226),但在执行下面的代码(自答代码。看看最后的解决方案)后,我得到了颜色代码rgb(42,34,38)
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;