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









