使用OpenCV / EmguCV计算RGB图像的独特颜色

时间:2015-09-18 07:24:28

标签: opencv colors histogram emgucv

如何计算图像ROI的独特颜色。 我尝试使用直方图,但我不知道如何处理Bin值。这是我到目前为止使用EMguCV的代码:

image.ROI= new Rectangle(10,10,300,500);
Image<Gray, Byte>[] gray = image.Split(); 
DenseHistogram hist = new DenseHistogram(256, new RangeF(0f, 256f));
hist.Calculate(new Image<Gray, byte>[] { gray[0] }, false, null);
float[] r = hist.GetBinValues();
hist.Calculate(new Image<Gray, byte>[] { gray[1] }, false, null);
float[] g = hist.GetBinValues();
hist.Calculate(new Image<Gray, byte>[] { gray[2] }, false, null);
float[] b = hist.GetBinValues();

如何计算r,g,b值以获得使用过的颜色数量?

1 个答案:

答案 0 :(得分:0)

我不知道你是否会对此感到高兴。但蛮力方法是

 Image<Bgr, byte> test = new Image<Bgr, byte>(@"C:\Lena.jpeg");
        List<Bgr> thePixelList = new List<Bgr>();
        for (int j = 0; j < test.Cols; j++)
        {
            for (int i = 0; i < test.Rows; i++)
            {                  
                thePixelList.Add(test[i, j]);

            }
        }


        int theCount = thePixelList.Distinct().Count();