当前位置:   article > 正文

OpenCVSharp 4.5 直方图均衡与直方图计算_opencvsharp绘制图像bgr直方图

opencvsharp绘制图像bgr直方图

用OpenCVSharp 4.5 跑一遍 OpenCV官方教程

原官方教程链接:

  1. OpenCV: Histogram Equalization
  2. OpenCV: Histogram Calculation

核心函数:equalizeHistsplitcalcHistnormalize

  1. using System;
  2. using OpenCvSharp;
  3. namespace ConsoleApp1
  4. {
  5. class tutorial20 : ITutorial
  6. {
  7. public void Run()
  8. {
  9. using (Mat src = new Mat("I:\\csharp\\images\\lena.png", ImreadModes.Color))
  10. using(Mat dst = new Mat())
  11. {
  12. Cv2.CvtColor(src, src, ColorConversionCodes.BGR2GRAY);
  13. Cv2.EqualizeHist(src, dst);
  14. using( new Window("Source image", src))
  15. using (new Window("Equalized Image", dst))
  16. Window.WaitKey();
  17. }
  18. }
  19. }
  20. }

  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ConsoleApp1
  8. {
  9. class tutorial19 : ITutorial
  10. {
  11. public void Run()
  12. {
  13. using (Mat src = new Mat("I:\\csharp\\images\\lena.png", ImreadModes.Color))
  14. {
  15. Mat[] bgr_planes = new Mat[3];
  16. Cv2.Split(src, out bgr_planes);
  17. int[] histSize = { 256 };
  18. Rangef[] histRange = { new Rangef(0, 256) }; //the upper boundary is exclusive
  19. bool uniform = true, accumulate = false;
  20. Mat b_hist = new Mat();
  21. Mat g_hist = new Mat();
  22. Mat r_hist = new Mat();
  23. Cv2.CalcHist(bgr_planes, new int[] { 0 }, null, b_hist, 1, histSize, histRange, uniform, accumulate);
  24. Cv2.CalcHist(bgr_planes, new int[] { 1 }, null, g_hist, 1, histSize, histRange, uniform, accumulate);
  25. Cv2.CalcHist(bgr_planes, new int[] { 2 }, null, r_hist, 1, histSize, histRange, uniform, accumulate);
  26. int hist_w = 512, hist_h = 400;
  27. int bin_w = (int)Math.Round((double)hist_w / histSize[0]);
  28. Mat histImage = new Mat(hist_h, hist_w, MatType.CV_8UC3, new Scalar(0, 0, 0));
  29. Cv2.Normalize(b_hist, b_hist, 0, histImage.Rows, NormTypes.MinMax, -1, null);
  30. Cv2.Normalize(g_hist, g_hist, 0, histImage.Rows, NormTypes.MinMax, -1, null);
  31. Cv2.Normalize(r_hist, r_hist, 0, histImage.Rows, NormTypes.MinMax, -1, null);
  32. for (int i = 1; i < histSize[0]; i++)
  33. {
  34. Cv2.Line(histImage,
  35. new Point(bin_w * (i - 1), hist_h - Math.Round(b_hist.At<float>(i - 1))),
  36. new Point(bin_w * (i), hist_h - Math.Round(b_hist.At<float>(i))),
  37. new Scalar(255, 0, 0),
  38. 2,
  39. LineTypes.Link8,
  40. 0);
  41. Cv2.Line(histImage,
  42. new Point(bin_w * (i - 1), hist_h - Math.Round(g_hist.At<float>(i - 1))),
  43. new Point(bin_w * (i), hist_h - Math.Round(g_hist.At<float>(i))),
  44. new Scalar(0, 255, 0),
  45. 2,
  46. LineTypes.Link8,
  47. 0);
  48. Cv2.Line(histImage,
  49. new Point(bin_w * (i - 1), hist_h - Math.Round(r_hist.At<float>(i - 1))),
  50. new Point(bin_w * (i), hist_h - Math.Round(r_hist.At<float>(i))),
  51. new Scalar(0, 0, 255),
  52. 2,
  53. LineTypes.Link8,
  54. 0);
  55. }
  56. using (new Window("Source image", src))
  57. using (new Window("calcHist Demo", histImage))
  58. Window.WaitKey();
  59. }
  60. }
  61. }
  62. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46688
推荐阅读
相关标签
  

闽ICP备14008679号