向现有图形Opencv添加另一个点

时间:2016-05-08 09:42:34

标签: c++ opencv graph

[enter image description here

到目前为止,我知道Opencv Plotting实时图存在限制。所以我目前正在修改直方图图中的代码以绘制实时图。下面是我的代码。但结果并不像我预期的那样使用循环来绘制图形,每次它都会在现有图形上绘制一个新的图形瞬间。再次,下面的代码工作得很好。

int hist_w = 700; int hist_h = 700; //image size
void show_histogram(std::string const& name, cv::Mat1b const& image, cv::Mat &histImage)
{
double seconds = difftime( time(0), start);


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

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

  bool uniform = true; bool accumulate = false;

  Mat hist;

  /// Compute the histograms:
  calcHist( &image, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );

  //Draw the histograms
  //int bin_w = cvRound( (double) hist_w/histSize );
   int bin_w = cvRound( (double) seconds);//x-axis



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

  /// Draw for points,for y-axis
  for( int i = 1; i < histSize; i++ )
  {
    Point ptPrev =  Point( bin_w*(seconds-1), hist_h - cvRound(hist.at<float>(0,255)));
    Point ptNew =    Point( bin_w*(seconds), hist_h - cvRound(hist.at<float>(0,255)));
    line( histImage,  ptPrev,ptNew,cv::Scalar::all(255), 5, 8, 0  );
    ptPrev = ptNew;
  }

  /// Display
   imshow(name, histImage);

 }

int main( int argc, char** argv )
{

//On spot video 
VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;

Mat edges,gray;
  namedWindow("edges",1);
  Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
  for(;;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera    
    imshow("Frame",frame);        
    cvtColor(frame, gray, CV_BGR2GRAY);
    GaussianBlur(gray, edges, Size(7,7), 1.5, 1.5);
    Canny(edges, edges, 0, 30, 3);
    cv::namedWindow("edges",0);  
    cv::resizeWindow("edges", 100, 100);       
    imshow("edges", edges);
    show_histogram("White Pixel",edges);
    if(waitKey(30) >= 0) break;
}

cv::waitKey();
return 0;

}

1 个答案:

答案 0 :(得分:0)

您的问题是,您在评论中提到的for(;;)的每次迭代都会创建 Mat histImage。 您需要在函数外部创建它,并将其作为参数(通过引用)传递给函数,进行更改并显示图像。

从函数体中删除这些行,并将它们放在for(;;)循环之外:

int hist_w = 700; int hist_h = 700; //image size
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );

将功能签名更改为:

void show_histogram(std::string const& name, cv::Mat1b const& image, cv::Mat &histImage)