EMGU.CV入门(十六、图像直方图)

一、图像直方图

1.1 计算直方图

        //
        // 摘要:
        //     Calculates a histogram of a set of arrays.
        //
        // 参数:
        //   images:
        //     Source arrays. They all should have the same depth, CV_8U or CV_32F , and the
        //     same size. Each of them can have an arbitrary number of channels.
        //
        //   channels:
        //     List of the channels used to compute the histogram.
        //
        //   mask:
        //     Optional mask. If the matrix is not empty, it must be an 8-bit array of the same
        //     size as images[i] . The non-zero mask elements mark the array elements counted
        //     in the histogram.
        //
        //   hist:
        //     Output histogram
        //
        //   histSize:
        //     Array of histogram sizes in each dimension.
        //
        //   ranges:
        //     Array of the dims arrays of the histogram bin boundaries in each dimension.
        //
        //   accumulate:
        //     Accumulation flag. If it is set, the histogram is not cleared in the beginning
        //     when it is allocated. This feature enables you to compute a single histogram
        //     from several sets of arrays, or to update the histogram in time.
        public static void CalcHist(IInputArrayOfArrays images, int[] channels, IInputArray mask, IOutputArray hist, int[] histSize, float[] ranges, bool accumulate)

1.2 直方图均衡

用来提高图像的对比度

        //
        // 摘要:
        //     The algorithm normalizes brightness and increases contrast of the image
        //
        // 参数:
        //   src:
        //     The input 8-bit single-channel image
        //
        //   dst:
        //     The output image of the same size and the same data type as src
        public static void EqualizeHist(IInputArray src, IOutputArray dst)

1.3 自适应均衡

        //
        // 摘要:
        //     Contrast Limited Adaptive Histogram Equalization (CLAHE)
        //
        // 参数:
        //   src:
        //     The source image
        //
        //   clipLimit:
        //     Clip Limit, use 40 for default
        //
        //   tileGridSize:
        //     Tile grid size, use (8, 8) for default
        //
        //   dst:
        //     The destination image
        public static void CLAHE(IInputArray src, double clipLimit, Size tileGridSize, IOutputArray dst)

1.4 比较直方图

比较两个图像的相似程度

        //
        // 摘要:
        //     Compares two histograms.
        //
        // 参数:
        //   h1:
        //     First compared histogram.
        //
        //   h2:
        //     Second compared histogram of the same size as H1 .
        //
        //   method:
        //     Comparison method
        //
        // 返回结果:
        //     The distance between the histogram
        public static double CompareHist(IInputArray h1, IInputArray h2, HistogramCompMethod method)

1.5 直方图反投影

应用于图像分割

        //
        // 摘要:
        //     Calculates the back projection of a histogram.
        //
        // 参数:
        //   images:
        //     Source arrays. They all should have the same depth, CV_8U or CV_32F , and the
        //     same size. Each of them can have an arbitrary number of channels.
        //
        //   channels:
        //     Number of source images.
        //
        //   hist:
        //     Input histogram that can be dense or sparse.
        //
        //   backProject:
        //     Destination back projection array that is a single-channel array of the same
        //     size and depth as images[0] .
        //
        //   ranges:
        //     Array of arrays of the histogram bin boundaries in each dimension.
        //
        //   scale:
        //     Optional scale factor for the output back projection.
        public static void CalcBackProject(IInputArrayOfArrays images, int[] channels, IInputArray hist, IOutputArray backProject, float[] ranges, double scale = 1.0

二、代码与效果

2.1 效果

EMGU.CV入门(十六、图像直方图)_第1张图片

2.2 代码

 //  1.加载原图
var image1 = new Image<Bgr, byte>("bird1.png");
var image0 = image1.Mat.Clone();
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(Text(image1.Bitmap, "原图")));

// 2. 原图转灰度
var imgGray = new Mat();
CvInvoke.CvtColor(image0, imgGray, ColorConversion.Bgr2Gray);
PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(Text(imgGray.Bitmap, "灰度")));

// 3. 计算直方图
var hist = new Mat();
int[] channels = new int[] { 0 };  //初始化数组
float[] ranges = new float[] { 0, 255 };
int[] histSize = new int[] { 256 };
VectorOfMat vMatImgs = new VectorOfMat();
vMatImgs.Push(imgGray);
CvInvoke.CalcHist(vMatImgs, channels, new Mat(),hist, histSize, ranges, false);
var m = new Matrix<float>(256, 1);
hist.CopyTo(m);
// 3.1 简单的做个显示
Point[] points = new Point[256];
for (int i=0; i< m.Rows; i++) {
    points[i] = new Point(i*10, imgGray.Height * 3-(int)m[i, 0]);
}
var img = new Image<Gray,byte>(255*10, imgGray.Height*3,new Gray(255));
CvInvoke.Polylines(img, points, true,new MCvScalar(0,0,0),10);
PreviewImage3 = new WriteableBitmap(Bitmap2BitmapImage(Text(img.Bitmap, "直方图")));

// 4. 直方图均衡化
var img4 = new Mat();
CvInvoke.EqualizeHist(imgGray, img4);
PreviewImage4 = new WriteableBitmap(Bitmap2BitmapImage(Text(img4.Bitmap, "均衡")));

// 5. 自适应均衡
var img5= new Mat();
CvInvoke.CLAHE(imgGray,2.0,new Size(8,8),img5);
PreviewImage5 = new WriteableBitmap(Bitmap2BitmapImage(Text(img5.Bitmap, "自适应均衡")));

// 6. 彩色图
var img6 = image1.Split();
var imgB = new Image<Gray, byte>(image1.Size);
var imgG = new Image<Gray, byte>(image1.Size);
var imgR = new Image<Gray, byte>(image1.Size);
CvInvoke.EqualizeHist(img6[0], imgB);
CvInvoke.EqualizeHist(img6[1], imgG);
CvInvoke.EqualizeHist(img6[2], imgR);
var imgC = new Image<Bgr, byte>(new Image<Gray, byte>[] { imgB, imgG, imgR }) ;
PreviewImage6 = new WriteableBitmap(Bitmap2BitmapImage(Text(imgC.Bitmap, "均衡")));

// 7. 彩色图自适应均衡
CvInvoke.CLAHE(img6[0], 2.0, new Size(8, 8), imgB);
CvInvoke.CLAHE(img6[1], 2.0, new Size(8, 8), imgG);
CvInvoke.CLAHE(img6[2], 2.0, new Size(8, 8), imgR);
var imgD = new Image<Bgr, byte>(new Image<Gray, byte>[] { imgB, imgG, imgR });
PreviewImage7 = new WriteableBitmap(Bitmap2BitmapImage(Text(imgD.Bitmap, "自适应均衡")));

你可能感兴趣的:(EMGU.CV,C#,WPF,opencv,计算机视觉,算法,EMGU.CV,c#)