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

22 篇文章 29 订阅
21 篇文章 0 订阅
该博客介绍了OpenCV中用于图像处理的几个关键步骤,包括计算图像直方图、直方图均衡化和自适应均衡。通过示例代码展示了如何使用这些方法来提高图像的对比度,以及比较两个图像的相似度。直方图均衡化和自适应均衡尤其适用于改善图像的视觉效果。
摘要由CSDN通过智能技术生成

一、图像直方图

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 效果

在这里插入图片描述

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, "自适应均衡")));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值