kzbr93

detection

May 22nd, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. /*
  2.  * detection.cpp
  3.  *
  4.  *  Created on: 11/05/2017
  5.  *      Author: zubair
  6.  */
  7.  
  8. #include <iostream>
  9. #include <stdio.h>
  10. #include <opencv2/opencv.hpp>
  11. #include <opencv2/core.hpp>
  12. #include <opencv2/imgproc.hpp>
  13. #include <opencv2/highgui.hpp>
  14. #include <opencv2/videoio.hpp>
  15. #include <opencv2/imgcodecs.hpp>
  16. #include <opencv2/features2d.hpp>
  17. #include <opencv2/features2d/features2d.hpp>
  18. #include <opencv2/xfeatures2d.hpp>
  19. #include <opencv2/calib3d.hpp>
  20.  
  21. //#include "opencv2/opencv_modules.hpp"
  22.  
  23.  
  24.  
  25. using namespace cv;
  26. using namespace std;
  27. using namespace cv::xfeatures2d;
  28.  
  29. int framecount;
  30. float nndrRatio = 0.7f;
  31.  
  32.      Mat tarframe;
  33.      Mat tempframe;
  34.      Mat result;
  35.  
  36.      // Contains the description of the match
  37.      typedef struct Match_desc{
  38.         bool init;
  39.         double maxVal;
  40.         Point maxLoc;
  41.         double scale;
  42.         Match_desc(): init(0){}
  43.      } Match_desc;
  44.  
  45. /** @function main */
  46. int main( int argc, char** argv )
  47. {
  48.  
  49.  
  50.      VideoCapture cap(0);
  51.      //Mat curr, prev;
  52.      if(!cap.isOpened())return -1;
  53.  
  54.      cap>>tarframe;
  55.      tarframe.copyTo(tempframe);
  56.  
  57.     while (1) {
  58.          //cap.read(frame);
  59.  
  60.          cap>>tarframe;
  61.          tarframe.copyTo(tempframe);
  62.  
  63.         char key = waitKey(33);
  64.         if (key == 'q')
  65.         {
  66.             break;
  67.         }
  68.  
  69.         namedWindow("DBG", WINDOW_AUTOSIZE );
  70.  
  71.  
  72.             Mat tempgray;
  73.             //template_mat = imread(tempframe, 1); // Read image
  74.             cvtColor(tempframe, tempgray, COLOR_BGR2GRAY); // Convert to Gray
  75.             Canny(tempframe, tempgray, 50, 50*4); // Find edges
  76.  
  77.             // Find size
  78.             int tW, tH;
  79.             tW = tempframe.cols;
  80.             tH = tempframe.rows;
  81.  
  82.             namedWindow("Template Image", WINDOW_AUTOSIZE );
  83.             imshow("Template Image", tempframe);
  84.  
  85.  
  86.             Mat target_gray, target_resized, target_edged;
  87.             //target_img = imread(tarframe, 1); // Read image
  88.             cvtColor(tarframe, target_gray, COLOR_BGR2GRAY); // Convert to Gray
  89.  
  90.             const float SCALE_START = 1;
  91.             const float SCALE_END = 0.2;
  92.             const int SCALE_POINTS = 20;
  93.  
  94.             Match_desc found;
  95.             for(float scale = SCALE_START; scale >= SCALE_END; scale -= (SCALE_START - SCALE_END)/SCALE_POINTS){
  96.                 resize(target_gray, target_resized, Size(0,0), scale, scale);// Resize
  97.  
  98.                 // Break if target image becomes smaller than template
  99.                 if(tW > target_resized.cols || tH > target_resized.rows) break;
  100.  
  101.  
  102.                 Canny(target_resized, target_edged, 50, 50*4); // Find edges
  103.  
  104.                 // Match template
  105.                 Mat result;
  106.                 matchTemplate(target_edged, tempframe, result, TM_CCOEFF);
  107.  
  108.                 double maxVal; Point maxLoc;
  109.                 minMaxLoc(result, NULL, &maxVal, NULL, &maxLoc);
  110.  
  111.                 // If better match found
  112.                 if( found.init == false || maxVal > found.maxVal ){
  113.                     found.init = true;
  114.                     found.maxVal = maxVal;
  115.                     found.maxLoc = maxLoc;
  116.                     found.scale = scale;
  117.                 }
  118.  
  119.                 // START VISUALIZATION CODE
  120.                 Mat target_clone;
  121.                 resize(tarframe, target_clone, Size(0,0), scale, scale);// Resize
  122.                 rectangle(target_clone, Point(maxLoc.x, maxLoc.y), Point(maxLoc.x + tW, maxLoc.y + tH), Scalar(0, 255, 255), 3);
  123.                 imshow("DBG", target_clone);
  124.                 waitKey(200);
  125.                 // END VISUALIZATION CODE
  126.             }
  127.  
  128.             int startX, startY, endX, endY;
  129.             startX = found.maxLoc.x / found.scale;
  130.             startY = found.maxLoc.y / found.scale;
  131.  
  132.             endX= (found.maxLoc.x + tW) / found.scale;
  133.             endY= (found.maxLoc.y + tH) / found.scale;
  134.  
  135.             // draw a bounding box around the detected result and display the image
  136.             rectangle(tarframe, Point(startX, startY), Point(endX, endY), Scalar(0, 0, 255), 3);
  137.  
  138.             imshow("DBG", tarframe);
  139.  
  140.             waitKey(0);
  141.         }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment