当前位置: 首页 > 网络知识

OpenCV3.3深度神经网络DNN模块 实例3:SSD模型实现对象检测

时间:2026-01-29 09:25:33
1 #include <opencv2/opencv.hpp> 2 #include <opencv2/dnn.hpp> 3 #include <iostream> 4 5 using namespace cv; 6 using namespace cv::dnn; 7 using namespace std; 8 9 const size_t width = 300;//模型尺寸为300*300 10 const size_t height = 300; 11 //label文件 12 String labelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/labelmap_det.txt"; 13 //模型文件 14 String modelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/VGG_ILSVRC2016_SSD_300x300_iter_affemodel"; 15 //模型描述文件 16 String model_text_file = "D:/opencv3.3/opencv/sources/samples/data/dnn/deploy.prototxt"; 17 18 vector<String> readLabels(); 19 const int meanValues[3] = ; 20 static Mat getMean(const size_t &w, const size_t &h) 27 merge(channels, mean); 28 return mean; 29 } 30 31 static Mat preprocess(const Mat &frame) 39 40 int main(int argc, char** argv) 46 namedWindow("input image", CV_WINDOW_AUTOSIZE); 47 imshow("input image", frame); 48 49 vector<String> objNames = readLabels(); 50 // import Caffe SSD model 51 Ptr<dnn::Importer> importer; 52 try 55 catch (const cv::Exception &err) 58 //初始化网络 59 Net net; 60 importer>populateNet(net); 61 importer.release(); 62 Mat input_image = preprocess(frame);//获取输入图像 64 Mat blobImage = blobFrImage(input_image);//将图像转换为blob 65 66 net.setInput(blobImage, "data");//将图像转换的blob数据输入到网络的第一层“data”层,见deploy.protxt文件 67 Mat detection = net.forward("detection_out");//结果输出(最后一层“detection_out”层)输出给detection 68 Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>()); 69 float confidence_threshold = 0.2;//自信区间,可以修改,越低检测到的物体越多 70 for (int i = 0; i < detectionMat.rows; i++) 85 } 86 imshow("ssddemo", frame); 87 88 waitKey(0); 89 return 0; 90 } 91 92 vector<String> readLabels() 99 string name; 100 while (!fp.eof()) 107 } 108 return objNames; 109 }



上一篇:C++快速入门 第四讲:文件操作
下一篇:C++快速入门 第十四节:对象
OpenCV
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素