opencv可视化图片-----c++

时间:2024-04-25 07:13:39
#include <opencv2/opencv.hpp> #include <opencv2/core.hpp> #include <filesystem> // 将数据类型转换为字符串 std::string opencvTool::type2str(int type) { std::string r; uchar depth = type & CV_MAT_DEPTH_MASK; uchar chans = 1 + (type >> CV_CN_SHIFT); switch (depth) { case CV_8U: r = "8U"; break; case CV_8S: r = "8S"; break; case CV_16U: r = "16U"; break; case CV_16S: r = "16S"; break; case CV_32S: r = "32S"; break; case CV_32F: r = "32F"; break; case CV_64F: r = "64F"; break; default: r = "User"; break; } r += "C"; r += (chans + '0'); //3 个颜色通道(C3) return r; } bool opencvTool::showImage(std::string image_p) { cv::Mat image = cv::imread(image_p.c_str()); if (image.empty()) { std::cout << "Error: empyt mat " << std::endl; return false; } // 打印图像信息 std::cout << "Image size: " << image.cols << " x " << image.rows << std::endl; std::cout << "Number of channels: " << image.channels() << std::endl; // 通道数 std::cout << "Data type: " << type2str(image.type()) << std::endl; // 自定义函数,用于将数据类型转换为字符串 cv::imshow("test", image); cv::waitKey(0); //持续显示窗口 cv::destroyAllWindows(); //用于关闭所有由 OpenCV 创建的窗口 return true; } bool opencvTool::showImage(cv::Mat image) { if (image.empty()) { std::cout << "Error: empty mat " << std::endl; return false; } // 打印图像信息 std::cout << "Image size: " << image.cols << "x" << image.rows << std::endl; std::cout << "Number of channels: " << image.channels() << std::endl; // 通道数 std::cout << "Data type: " << type2str(image.type()) << std::endl; // 自定义函数,用于将数据类型转换为字符串 // 显示图像 cv::imshow("test", image); cv::waitKey(0); // 持续显示窗口 cv::destroyAllWindows(); // 用于关闭所有由 OpenCV 创建的窗口 return true; }