如何在matplotlibcpp中绘制c ++键?

问题描述 投票:0回答:1
有一段时间,我一直将数据保存在CSV文件中,并在Python中绘制它。但是我想要通过C ++直接绘制。我遇到了这个

C++包装器。太好了,但是我找不到文档中Imshow()的示例。我无法理解此功能所需的参数。谁能帮我吗?

P.S

我也想将图扩展到2D动画。因此,我也对任何其他方法开放。

我不确定您是专门要求的,但是我认为您需要一种通过C ++图形方式查看Matplotlib图的方法?
c++ plot
1个回答
0
投票
simply使用

matplotlibcpp

标头文件(从其GitHub获得),并确保安装了OpenCV。

BELOW是一个C ++代码示例,演示了如何执行某些Matplotlib操作,将图作为图片保存到本地磁盘,然后使用OpenCV图形地查看图片:

/* How to compile on Windows 10: cl /EHsc /std:c++17 /I C:\Python312\include /I F:\OpenCV_Course\myenv\Lib\site-packages\numpy\_core\include /I F:\AI_Componets\OpenCV\build\install\include matplotlib.cpp /link /LIBPATH:F:\AI_Componets\OpenCV\build\install\x64\vc17\lib /LIBPATH:C:\Python312\libs python312.lib opencv_core500.lib opencv_imgcodecs500.lib opencv_highgui500.lib opencv_imgproc500.lib */ #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <opencv2/opencv.hpp> #include "F:\OpenCV_Course\matplotlib-cpp\matplotlibcpp.h" #include <vector> using namespace std; namespace plt = matplotlibcpp; int main() { // Prepare data int ncols = 500, nrows = 300; std::vector<float> z(ncols * nrows); for (int j=0; j<nrows; ++j) { for (int i=0; i<ncols; ++i) { z.at(ncols * j + i) = std::sin(std::hypot(i - ncols/2, j - nrows/2)); } } const float* zptr = &(z[0]); const int colors = 1; plt::title("My matrix"); plt::imshow(zptr, nrows, ncols, colors); // Show plots plt::save("imshow.png"); std::cout << "Result saved to 'imshow.png'.\n"; // OpenCV // Read the image from file cv::Mat image = cv::imread("imshow.png", cv::IMREAD_COLOR); // Check if the image was successfully loaded if (image.empty()) { std::cerr << "Error: Could not open or find the image." << std::endl; return -1; } // Create a window to display the image cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE); // Show the image in the window cv::imshow("Display Image", image); // Wait for a keystroke in the window cv::waitKey(0); // Destroy the window cv::destroyAllWindows(); }

示例输出:

希望这帮助

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.