首先:我正在使用 OpenCV C++ 进行图像处理。
要求 是根据一维图像的 vector 值绘制图形。我提到了http://answers.opencv.org/question/73233/how-do-you-plot-graphs-in-opencv-projects/并找到了解决方案。这是一个可以在 OpenCV 中制作的精彩图,但是 plot
不被 OpenCV 原始库识别。
为了找到解决方案,我查看了所有库文件并得出结论,头文件
#include opencv2/plot.hpp
不在原始 OpenCV 库(最新版本)中。它必须在一些额外的库中实现,但我想还没有上线。
这是我的代码供您引用:
#include <opencv2/plot.hpp>
int PlotGraph(Mat & data) {
Mat plot_result;
Ptr<plot::Plot2d> plot = plot::createPlot2d(data);
//Set Background color
plot->setPlotBackgroundColor(Scalar(50, 50, 50));
//Set plot line color
plot->setPlotLineColor(Scalar(50, 50, 255));
plot->render(plot_result);
imshow("plot", plot_result);
waitKey();
plot->setPlotLineColor(Scalar(50, 255, 255));
data = data / 3;
plot->render(plot_result);
imshow("plot", plot_result);
waitKey();
plot->setPlotGridColor(Scalar(255, 0, 255));
data = data * 4;
plot->render(IMREAD_UNCHANGEDplot_result);
imshow("plot", plot_result);
waitKey();
plot->setPlotTextColor(Scalar(255, 0, 0));
randu(data, 100, 400);
plot->render(plot_result);
imshow("plot", plot_result);
waitKey();
return 0;
}
在 C++ 和其他库(例如 GNU)中有很多方法可以绘制图形,但是我有点好奇并且不想放弃这个。
如果有人给我一个解决方案或建议一种在 OpenCV C++ 中绘制图形的方法,我将不胜感激。
最佳答案
OpenCV 不支持绘图。您可以使用 plot contrib module , 但这是非常基础的。
您可以尝试 Profactor CvPlot https://github.com/Profactor/cv-plot . (我是开发人员)。它非常容易集成,完全基于 opencv,可以使用自定义控件进行扩展。这是您可以绘制到 cv::Mat 或使用交互式查看器显示图表的方式:
#include <CvPlot/cvplot.h>
std::vector<double> x(20*1000), y1(x.size()), y2(x.size()), y3(x.size());
for (size_t i = 0; i < x.size(); i++) {
x[i] = i * CV_2PI / x.size();
y1[i] = std::sin(x[i]);
y2[i] = y1[i] * std::sin(x[i]*50);
y3[i] = y2[i] * std::sin(x[i]*500);
}
auto axes = CvPlot::makePlotAxes();
axes.create<CvPlot::Series>(x, y3, "-g");
axes.create<CvPlot::Series>(x, y2, "-b");
axes.create<CvPlot::Series>(x, y1, "-r");
//plot to a cv::Mat
cv::Mat mat = axes.render(300, 400);
//or show with interactive viewer
CvPlot::show("mywindow", axes);
您可能还想尝试 Leonardvandriel's cvplot .它的工作原理类似,但不能使用自定义可绘制对象进行扩展。
关于c++ - 有没有办法在 OpenCV 中绘制图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46195828/