zoukankan      html  css  js  c++  java
  • 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;
    }

     

  • 相关阅读:
    【django admin】外键关联中,如何选择一种属性后展示另外一种属性
    【UNI-APP】跳转外部链接 保留顶部
    【django 组件】admin修改界面
    【django基础】GET带参数与接口接收
    【Pythoin爬虫】使用网页copy xpath的坑==>tbody定位不到
    【Python小随笔】三行生成词云图
    【UNIAPP】接入导航系统完整版
    【UNIAPP】微信小程序接入腾讯地图 路线规划时,setData报错
    【部署接口】这里接口+admin后台管理,需要注意
    【UNI-APP】分页请求数据列表
  • 原文地址:https://www.cnblogs.com/herd/p/9736894.html
Copyright © 2011-2022 走看看