1. 程式人生 > >目標檢測之一(傳統演算法和深度學習的原始碼學習)

目標檢測之一(傳統演算法和深度學習的原始碼學習)

#include<opencv2\opencv.hpp> #include<opencv2\core\core.hpp> #include<opencv2\highgui\\highgui.hpp> #include <iostream> using namespace std; using namespace cv; int main() { string xmlPath = "car_model.xml"; //訓練好的分類器xml檔案 CascadeClassifier ccf; //建立分類器物件 Mat img; if (!ccf.load(xmlPath)) //載入訓練檔案 { cout << "不能載入指定的xml檔案" << endl; return 0; } namedWindow("car"); bool stop = false; //獲取攝像頭影象 //VideoCapture cap(0); //獲取資料夾圖片序列 string img_path = "data";//把圖片放到當前目錄的data資料夾下即可,檔名隨便都可以 vector<cv::String> vec_img; glob(img_path, vec_img); if (vec_img.empty()) { std::cout << "there is no pictures." << endl; return -1; } //寫入視訊檔案,我處理的是序列圖片,所以我把檢測結果寫成視訊檔案了,便於觀察 //VideoWriter writer; //string video_name = "car1.avi"; //Mat temp = imread(vec_img[0]); //int frame_fps = 15;//幀率 //writer = VideoWriter(video_name, CV_FOURCC('X', 'V', 'I', 'D'), frame_fps, Size(temp.cols, temp.rows)); while (!stop) { int64 start = getTickCount(); for (int i = 0; i < vec_img.size(); ++i) { img = imread(vec_img[i]); //if (!cap.read(img)) // break; //cap >> img; vector<Rect> cars; //建立一個容器儲存檢測出來的車輛 Mat gray; cvtColor(img, gray, CV_BGR2GRAY); //轉換成灰度圖,因為harr特徵從灰度圖中提取 //equalizeHist(gray, gray); //直方圖均衡行 ccf.detectMultiScale(gray, cars, 1.1, 3, 0, Size(10, 10), Size(100, 100)); //檢測車輛 //cout << cars.size() << endl; for (vector<Rect>::const_iterator iter = cars.begin(); iter != cars.end(); iter++) { rectangle(img, *iter, Scalar(0, 0, 255), 2, 8); //畫出矩形 } imshow("car", img); //writer.write(img); if (waitKey(2) == 'q')//按q退出 { stop = true; break; } } //計算執行時間 cout << (getTickCount() - start) / getTickFrequency() << endl; break; } return 1; }