在累积直方图中绘制条形位移

时间:2015-10-08 10:47:04

标签: c++ opencv

转换后我计算了ycbcr视频帧的Y通道直方图

来自RGB。然后我计算了Y通道的累积直方图。

我计算了累积

的位移

直方图条我使用了存储位移值的整数变量(taw)。

我想在直方图和累积直方图条中绘制taw的值。我认为int数据类型存在问题,任何帮助赞赏

 // Draw histogram
  drawHistogram(hist, 400, 1024, hist.rows, Scalar(255, 255, 255), 2);

 // draw cumulative histogram
    drawHistogram(c_hist, 400, 1024, c_hist.rows, Scalar(0, 0, 255), 2, "cumulativeHistogram ");

How to draw taw ?
 drawHistogram(taw, 400, 1024, c_hist.rows, Scalar(0, 0, 255), 2, "cumulativeHistogram ");

谢谢

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

using namespace std;
using namespace cv;

  Mat drawHistogram(Mat &hist, int hist_h = 300, int hist_w = 500, int  hist_size = 256, Scalar color = Scalar(255, 255, 255), int type = 2, string title = "Histogram")
{
      int bin_w = cvRound( (double) hist_w/hist_size );

      Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );

/// Normalize the result to [ 0, histImage.rows ]
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

switch (type) {
case 1:
    for(int i = 0; i < hist_size; i++)
    {
        const unsigned x = i;
        const unsigned y = hist_h;

        line(histImage, Point(bin_w * x, y),
             Point(bin_w * x, y - cvRound(hist.at<float>(i))),
             color);
    }

    break;
case 2:
    for( int i = 1; i < hist_size; ++i)
    {
        Point pt1 = Point(bin_w * (i-1), hist_h);
        Point pt2 = Point(bin_w * i, hist_h);
        Point pt3 = Point(bin_w * i, hist_h - cvRound(hist.at<float>(i)));
        Point pt4 = Point(bin_w * (i-1), hist_h - cvRound(hist.at<float>(i-1)));
        Point pts[] = {pt1, pt2, pt3, pt4, pt1};

        fillConvexPoly(histImage, pts, 5, color);
    }
    break;
default:
    for( int i = 1; i < hist_size; ++i)
    {
        line( histImage, Point( bin_w * (i-1), hist_h - cvRound(hist.at<float>(i-1))) ,
                         Point( bin_w * (i), hist_h - cvRound(hist.at<float>(i))),
                         color, 1, 8, 0);
    }

    break;
}

imshow(title, histImage);

return histImage;
}
  int main()
  {
    /// Load image
    VideoCapture cap("eye.mp4"); // open video

   if(!cap.isOpened())  // check if we succeeded

   return -1;

while(1)
    {
Mat frame,ycbcr;

cap >> frame; // get a new frame


  namedWindow("video_frame",1);


   namedWindow("Y_channel",1);


  cvtColor(frame, ycbcr, CV_RGB2YCrCb);

 vector <Mat> planes;

 split(ycbcr,planes);

 medianBlur( planes[0], planes[0], 3);



    /// Establish the number of bins
    int histSize = 256;

    /// Set the range
    float range[] = { 0, 256 } ;
    const float* histRange = { range };

    bool uniform = true; bool accumulate = false;

    // compute the histogram
    Mat hist;
    calcHist( &planes[0], 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

    Mat c_hist(hist.size(), hist.type());


    float pixel_count= planes[0].size().width * planes[0].size().height;

    c_hist.at<float>(0) = hist.at<float>(0)/ pixel_count ;

     int taw=-1;

    for(size_t k = 1; k < hist.rows; ++k)
    {
    c_hist.at<float>(k) = c_hist.at<float>(k-1) + (hist.at<float>(k) /pixel_count) ;

    cout<<"c_hist       "<<c_hist.at<float>(k)<<endl;

    cout<<"pixel count"<<pixel_count<<endl;

    if(c_hist.at<float>(k) > 0.15)

    {
        taw=k-1;

        break;

    }

       }

       imshow( "video_frame", frame );
       imshow( "Y_channel", planes[0] );
    // draw histogram
    drawHistogram(hist, 400, 1024, hist.rows, Scalar(255, 255, 255), 2);
    // draw cumulative histogram
    drawHistogram(c_hist, 400, 1024, c_hist.rows, Scalar(0, 0, 255), 2, "cumulativeHistogram ");


    waitKey(50);


}
  return 0;

}

0 个答案:

没有答案