1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <iostream> #include <string> #include <vector> #include<opencv2/opencv.hpp> #include <opencv2/dnn.hpp> using namespace std; void readImagesInFolder(const std::string& folderPath, std::vector<cv::Mat>& images) { cv::String path(folderPath + "/*.jpg"); // 这里假设你的图片格式是.jpg,如果是其他格式请相应修改 std::vector<cv::String> fileNames; cv::glob(path, fileNames, true); // 通过glob函数获取文件夹内所有符合格式的文件名 for (const auto& fileName : fileNames) { // 使用imread函数读取图片 cv::Mat bgrImage = cv::imread(fileName, cv::IMREAD_COLOR); // 图片格式转化bgr-->rgb if (!bgrImage.empty()) { cv::Mat rgbImage; cv::cvtColor(bgrImage, rgbImage, cv::COLOR_BGR2RGB); images.push_back(rgbImage); } } }cv::Mat transformation(const cv::Mat& image, const cv::Size & targetSize, const cv::Scalar& mean, const cv::Scalar& std) { cv::Mat resizedImage; //图片尺寸缩放 cv::resize(image, resizedImage, targetSize, 0, 0, cv::INTER_AREA); cv::Mat normalized; resizedImage.convertTo(normalized, CV_32F); cv::subtract(normalized / 255.0, mean, normalized); cv::divide(normalized, std, normalized); return normalized; } cv::dnn::Net loadModel(const string& onnx_path) { cv::dnn::Net net = cv::dnn::readNetFromONNX(onnx_path); return net; } int main() { // 图片存放文件路径 string folderPath = "D:/C++_demo/opencv_onnx_gpu/CAMO/c"; std::vector<cv::Mat> rgbImages; readImagesInFolder(folderPath, rgbImages); // string image_path = "./animal-1.jpg"; // 加载ONNX模型 string onnx_path = "D:/C++_demo/opencv_onnx_gpu/PFNet.onnx"; cv::dnn::Net net = loadModel(onnx_path); // 设置CUDA为后端 net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA); net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA); cv::Mat output_prob; std::vector<cv::Mat> output_probs; std::vector<cv::String> output_layer_names = net.getUnconnectedOutLayersNames(); // 定义目标图像大小 cv::Size targetSize(416, 416); // 定义每个通道的归一化参数 cv::Scalar mean(0.485, 0.456, 0.406); // 均值 cv::Scalar std(0.229, 0.224, 0.225); // 标准差 // 开始计时 auto start = chrono::high_resolution_clock::now(); for (const auto& rgbImage : rgbImages) { // 获取图像的大小 cv::Size originalSize(rgbImage.cols, rgbImage.rows); //cv::imshow("输入窗口", rgbImage); //cv::waitKey(0); //cv::destroyAllWindows(); // 图片归一化 cv::Mat normalized = transformation(rgbImage, targetSize, mean, std); std::cout << normalized.size() << std::endl; cv::Mat blob = cv::dnn::blobFromImage(normalized); // 将Blob设置为模型的输入 net.setInput(blob); // 运行前向传播 net.forward(output_probs, output_layer_names); // 获取最完整的预测 cv::Mat prediction = output_probs[3]; // 预测图变mask cv::Mat mask; cv::resize(prediction.reshape(1, 416) * 255.0, mask, originalSize, 0, 0, cv::INTER_AREA); } auto end = std::chrono::high_resolution_clock::now(); // 计算耗时 std::chrono::duration<double> elapsed = end - start; double elapsedTime = elapsed.count(); // 打印耗时 std::cout << "Elapsed time: " << elapsedTime << " seconds" << std::endl; return 0; }
|