一、图像直方图
1.1 计算直方图
public static void CalcHist(IInputArrayOfArrays images, int[] channels, IInputArray mask, IOutputArray hist, int[] histSize, float[] ranges, bool accumulate)
1.2 直方图均衡
用来提高图像的对比度
public static void EqualizeHist(IInputArray src, IOutputArray dst)
1.3 自适应均衡
public static void CLAHE(IInputArray src, double clipLimit, Size tileGridSize, IOutputArray dst)
1.4 比较直方图
比较两个图像的相似程度
public static double CompareHist(IInputArray h1, IInputArray h2, HistogramCompMethod method)
1.5 直方图反投影
应用于图像分割
public static void CalcBackProject(IInputArrayOfArrays images, int[] channels, IInputArray hist, IOutputArray backProject, float[] ranges, double scale = 1.0
二、代码与效果
2.1 效果
2.2 代码
var image1 = new Image<Bgr, byte>("bird1.png");
var image0 = image1.Mat.Clone();
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(Text(image1.Bitmap, "原图")));
var imgGray = new Mat();
CvInvoke.CvtColor(image0, imgGray, ColorConversion.Bgr2Gray);
PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(Text(imgGray.Bitmap, "灰度")));
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);
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, "直方图")));
var img4 = new Mat();
CvInvoke.EqualizeHist(imgGray, img4);
PreviewImage4 = new WriteableBitmap(Bitmap2BitmapImage(Text(img4.Bitmap, "均衡")));
var img5= new Mat();
CvInvoke.CLAHE(imgGray,2.0,new Size(8,8),img5);
PreviewImage5 = new WriteableBitmap(Bitmap2BitmapImage(Text(img5.Bitmap, "自适应均衡")));
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, "均衡")));
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, "自适应均衡")));
|