• Opencv 图片直方图


    #include <iostream>
    #include <opencv2/opencv.hpp>

    using namespace std;
    using namespace cv;

    Mat img1, img2, img3, img_gray, map_x, map_y;

    char win1[] = "window1";
    char win2[] = "window2";
    char win3[] = "window3";
    char win4[] = "window4";

    int threshold_value = 0;
    int max_value = 255;
    RNG rng(12345);

    int Demo_Histogram();

    int index = 0;

    //Remap
    int Demo_Histogram()
    {
      img1 = imread("D://images//1//9.jpg");
      if (img1.empty())
      {
        cout << "could not load image..." << endl;
        return 0;
      }
      imshow(win1, img1);

      vector<Mat> bgr_planes;

      //把多通道图像分为多个单通道图像
      split(img1,bgr_planes);
      //imshow(win2,bgr_planes);
      int histSize = 256;
      float range[] = {0,256};

      const float *histRanges = {range};
      Mat b_hist, g_hist, r_hist;
      calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRanges, true, false);
      calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRanges, true, false);
      calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRanges, true, false);


      int hist_h = 400;
      int hist_w = 512;
      int bin_w = hist_w / histSize;
      Mat histImage(hist_w,hist_h,CV_8UC3,Scalar(0,0,0));

      //归一化处理
      normalize(b_hist, b_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
      normalize(g_hist, g_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
      normalize(r_hist, r_hist, 0, hist_h, NORM_MINMAX, -1, Mat());

      for (int i=1;i<histSize;i++)
      {
        line(histImage,
          Point((i-1)*bin_w,hist_h-cvRound(b_hist.at<float>(i-1))),
          Point((i)*bin_w, hist_h - cvRound(b_hist.at<float>(i))),
          Scalar(255,0,2),2,LINE_AA);

        line(histImage,
          Point((i - 1)*bin_w, hist_h - cvRound(g_hist.at<float>(i - 1))),
          Point((i)*bin_w, hist_h - cvRound(g_hist.at<float>(i))),
          Scalar(0, 255, 2), 2, LINE_AA);

        line(histImage,
          Point((i - 1)*bin_w, hist_h - cvRound(r_hist.at<float>(i - 1))),
          Point((i)*bin_w, hist_h - cvRound(r_hist.at<float>(i))),
          Scalar(0, 0, 255), 2, LINE_AA);
      }

      imshow(win2, histImage);
      return 0;
    }

    int main()
    {
      Demo_Histogram();

      waitKey(0);
      return 0;
    }

     

  • 相关阅读:
    懒加载与预加载
    Javascript之 Ajax 与 JSON
    Q&A:为什么prototype中的方法不能赋值给变量,再调用?
    Javascript之 浅克隆 与 深克隆
    Javascript之 __proto__ 与 prototype
    React入门(二)—— Props属性
    React入门(一)—— State属性
    Flask调用layer-iframe后,如何从服务器端关闭弹出窗
    Python从入门到项目实践(明日科技 吉林大学出版社)
    从实例到数理来解析感知机学习算法(PLA)
  • 原文地址:https://www.cnblogs.com/herd/p/9736894.html
Copyright © 2020-2023  润新知