image

 

   讀取影像格式與轉換           

var filePath = "D:\\test.png";
Image<Bgr, Byte> imgOri = new Image<Bgr, Byte>(filePath);
Image<Gray, byte> grayImg = imgOri.Convert<Gray, byte>();
Mat imgMat = CvInvoke.Imread(filePath);
xImage<Hsv, byte> imgHsv = new Image<Hsv, byte>(imgMat.Width, imgMat.Height);
CvInvoke.CvtColor(imgMat, imgHsv, Emgu.CV.CvEnum.ColorConversion.Bgr2Hsv);
imageBox1.Image = imgOri;

image

   檢視色彩分布

Image<Gray, Byte>[] imgHsvs = imgHsv.Split(); //分離成H S V 3個通道
var imgHsvH = imgHsvs[0];
var imgHsvS = imgHsvs[1];
var imgHsvV = imgHsvs[2];

int[] Huebins = { 180 };  //Hue 10bit為0-360 計算時採8bit 所以為0-180
RangeF[] HueRanges = new RangeF[] { new RangeF(0, 180) }; //H
DenseHistogram hsHist = new DenseHistogram(Huebins, HueRanges);
hsHist.Calculate(new Image<Gray, Byte>[] { imgHsvH }, false, null);
histogramBox1.AddHistogram("Blue", Color.Blue, hsHist, 180, new float[] { 0, 180});

從下圖可以看到4個顏色分布的位置,可以很直觀判斷有4種顏色.           

image

取得色彩位置及計算色彩數(此處設大於10)

var colors = new List<int>();
var colorIndex = 0;
foreach (var val in hsHist.GetBinValues())
{
  if (val > 10) colors.Add(colorIndex);
  colorIndex++;
}
lbl_colors_count.Text = colors.Count.ToString();

imageimage

 

   透過色彩分布檢視飽和度與亮度

 

foreach (var color in colors) {
                
                Mat imgRange = new Mat(imgHsv.Size, DepthType.Cv8U, 3);
                CvInvoke.InRange(imgHsv, new ScalarArray(new MCvScalar(color-2, 200, 200)), new ScalarArray(new MCvScalar(color + 2, 255, 255)), imgRange);
                Mat imgOutput = new Mat(imgHsv.Size, DepthType.Cv8U, 3);
                imgHsv.Mat.CopyTo(imgOutput, imgRange);
                var newImgHsv = imgOutput.ToImage<Hsv, byte>();
                Image<Gray, byte>[] channels = newImgHsv.Copy().Split();
                var vmsH = channels[0];
                var vmsS = channels[1];
                var vmsV = channels[2];


                imageBox1.Image = imgRange;
                imageBox2.Image = newImgHsv;

                Huebins[0] = 180;
                HueRanges=new RangeF[] { new RangeF(0, 180) };
                histHSV = new DenseHistogram(Huebins, HueRanges);
                histHSV.Calculate(new Image<Gray, Byte>[] { vmsH }, false, null);
                histogramBox2.AddHistogram("Hue", Color.Red, histHSV, Huebins[0], new float[] { 0, 256 });
                histogramBox2.Refresh();
                Huebins[0] = 256;
                HueRanges = new RangeF[] { new RangeF(0, 256) };
                histHSV = new DenseHistogram(Huebins, HueRanges);
                histHSV.Calculate(new Image<Gray, Byte>[] { vmsS }, false, null);
                histogramBox2.AddHistogram("Sat", Color.Green, histHSV, Huebins[0], new float[] { 0, 256 });
                histogramBox2.Refresh();
                histHSV = new DenseHistogram(Huebins, HueRanges);
                histHSV.Calculate(new Image<Gray, Byte>[] { vmsV }, false, null);
                histogramBox2.AddHistogram("Val", Color.Blue, histHSV, Huebins[0], new float[] { 0, 256 });

                histogramBox2.Refresh();
                histogramBox2.Show();

                break;//只做第一次Color
              
            }

imageimgRange

imagenewImgHsv

image

 

 

arrow
arrow
    全站熱搜

    門外漢 發表在 痞客邦 留言(0) 人氣()