简体   繁体   English

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

[英]Add another point to existing graph Opencv

[ [ 在此处输入图片说明

So far I know that there are limitation regarding to Opencv Plotting real time graph. 到目前为止,我知道关于Opencv绘制实时图存在一些限制。 So I currently modifying the code from histogram graph to plot a real time graph. 因此,我目前正在修改直方图的代码以绘制实时图。 Below will be my code. 下面是我的代码。 But the result is not as I expected as using loop to draw the graph, every time it will plot a NEW graph instant of plot on existing graph. 但是结果并不像我期望的那样使用循环绘制图形,每次它都会在现有图形上绘制新的图形瞬时图。 Again, the code below work just fine. 同样,下面的代码也可以正常工作。

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;

}

Your problem is that every iteration of the for(;;) you mentioned in the comments you are creating a new Mat histImage . 您的问题是,在注释中提到的for(;;)每次迭代都在创建新的 Mat histImage You need to create it outside of the function, and pass it as an argument(by reference) to the function, make the changes and show the image. 您需要在函数外部创建它,并将其作为参数(通过引用)传递给函数,进行更改并显示图像。

Delete these lines from the body of the function, and put them outside the for(;;) loop: 从函数的主体中删除这些行,并将其放在for(;;)循环之外:

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

Change the function signature to be: 将功能签名更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM