向现有图形Opencv添加另一个点(Add another point to existing graph Opencv)

编程入门 行业动态 更新时间:2024-10-24 14:22:50
向现有图形Opencv添加另一个点(Add another point to existing graph Opencv)

[

到目前为止,我知道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; }

[

So far I know that there are limitation regarding to Opencv Plotting real time graph. 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; }

最满意答案

您的问题是您在评论中提到的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)

Your problem is that every iteration of the for(;;) you mentioned in the comments you are creating a new 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:

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)

更多推荐

本文发布于:2023-07-23 12:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形   Opencv   Add   graph   existing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!