Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * detection.cpp
- *
- * Created on: 11/05/2017
- * Author: zubair
- */
- #include <iostream>
- #include <stdio.h>
- #include <opencv2/opencv.hpp>
- #include <opencv2/core.hpp>
- #include <opencv2/imgproc.hpp>
- #include <opencv2/highgui.hpp>
- #include <opencv2/videoio.hpp>
- #include <opencv2/imgcodecs.hpp>
- #include <opencv2/features2d.hpp>
- #include <opencv2/features2d/features2d.hpp>
- #include <opencv2/xfeatures2d.hpp>
- #include <opencv2/calib3d.hpp>
- //#include "opencv2/opencv_modules.hpp"
- using namespace cv;
- using namespace std;
- using namespace cv::xfeatures2d;
- int framecount;
- float nndrRatio = 0.7f;
- Mat tarframe;
- Mat tempframe;
- Mat result;
- // Contains the description of the match
- typedef struct Match_desc{
- bool init;
- double maxVal;
- Point maxLoc;
- double scale;
- Match_desc(): init(0){}
- } Match_desc;
- /** @function main */
- int main( int argc, char** argv )
- {
- VideoCapture cap(0);
- //Mat curr, prev;
- if(!cap.isOpened())return -1;
- cap>>tarframe;
- tarframe.copyTo(tempframe);
- while (1) {
- //cap.read(frame);
- cap>>tarframe;
- tarframe.copyTo(tempframe);
- char key = waitKey(33);
- if (key == 'q')
- {
- break;
- }
- namedWindow("DBG", WINDOW_AUTOSIZE );
- Mat tempgray;
- //template_mat = imread(tempframe, 1); // Read image
- cvtColor(tempframe, tempgray, COLOR_BGR2GRAY); // Convert to Gray
- Canny(tempframe, tempgray, 50, 50*4); // Find edges
- // Find size
- int tW, tH;
- tW = tempframe.cols;
- tH = tempframe.rows;
- namedWindow("Template Image", WINDOW_AUTOSIZE );
- imshow("Template Image", tempframe);
- Mat target_gray, target_resized, target_edged;
- //target_img = imread(tarframe, 1); // Read image
- cvtColor(tarframe, target_gray, COLOR_BGR2GRAY); // Convert to Gray
- const float SCALE_START = 1;
- const float SCALE_END = 0.2;
- const int SCALE_POINTS = 20;
- Match_desc found;
- for(float scale = SCALE_START; scale >= SCALE_END; scale -= (SCALE_START - SCALE_END)/SCALE_POINTS){
- resize(target_gray, target_resized, Size(0,0), scale, scale);// Resize
- // Break if target image becomes smaller than template
- if(tW > target_resized.cols || tH > target_resized.rows) break;
- Canny(target_resized, target_edged, 50, 50*4); // Find edges
- // Match template
- Mat result;
- matchTemplate(target_edged, tempframe, result, TM_CCOEFF);
- double maxVal; Point maxLoc;
- minMaxLoc(result, NULL, &maxVal, NULL, &maxLoc);
- // If better match found
- if( found.init == false || maxVal > found.maxVal ){
- found.init = true;
- found.maxVal = maxVal;
- found.maxLoc = maxLoc;
- found.scale = scale;
- }
- // START VISUALIZATION CODE
- Mat target_clone;
- resize(tarframe, target_clone, Size(0,0), scale, scale);// Resize
- rectangle(target_clone, Point(maxLoc.x, maxLoc.y), Point(maxLoc.x + tW, maxLoc.y + tH), Scalar(0, 255, 255), 3);
- imshow("DBG", target_clone);
- waitKey(200);
- // END VISUALIZATION CODE
- }
- int startX, startY, endX, endY;
- startX = found.maxLoc.x / found.scale;
- startY = found.maxLoc.y / found.scale;
- endX= (found.maxLoc.x + tW) / found.scale;
- endY= (found.maxLoc.y + tH) / found.scale;
- // draw a bounding box around the detected result and display the image
- rectangle(tarframe, Point(startX, startY), Point(endX, endY), Scalar(0, 0, 255), 3);
- imshow("DBG", tarframe);
- waitKey(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment