码迷,mamicode.com
首页 > 其他好文 > 详细

Opencv 图片直方图

时间:2018-10-02 17:57:58      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:lan   str   for   ace   point   ons   技术   out   als   

#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;
}

 技术分享图片

Opencv 图片直方图

标签:lan   str   for   ace   point   ons   技术   out   als   

原文地址:https://www.cnblogs.com/herd/p/9736894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!