Opencv 圖片長條圖

來源:互聯網
上載者:User

標籤: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 圖片長條圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.