Advertisement
Guest User

brisk_orient.cpp

a guest
Jul 17th, 2013
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. // read an RGB picture file, run BRISK feature detector on it
  2. // create a picture of the feature points with their orientations, and write it out
  3.  
  4. #include <iostream>
  5. #include <stdlib.h>
  6.  
  7. #include <opencv2/core/core.hpp>              // for cv::Mat
  8. #include <opencv2/features2d/features2d.hpp>  // for cv::BRISK
  9. #include <opencv2/highgui/highgui.hpp>        // for cv::imread
  10.  
  11. static void draw_dot(cv::Mat &pic, float x, float y, int radius, const cv::Vec3b &color) {
  12.   int x0 = floor(x+.5);
  13.   int y0 = floor(y+.5);
  14.   assert(pic.type()==CV_8UC3);
  15.   for (int y=y0-radius; y<=y0+radius; y++) {
  16.     for (int x=x0-radius; x<=x0+radius; x++) {
  17.       if (x>=0 && y>=0 && x<pic.cols && y<pic.rows)
  18.         pic.at<cv::Vec3b>(y, x) = color;
  19.     }
  20.   }
  21. }
  22.  
  23. static void draw_line_segment(cv::Mat &pic, float x0, float y0, float x1, float y1,
  24.     const cv::Vec3b &color) {
  25.   assert(pic.type()==CV_8UC3);
  26.   float dx = abs(x1-x0);
  27.   float dy = abs(y1-y0);
  28.   // crude jaggy line drawing
  29.   if (dx>=dy) {
  30.     if (dx==0) return;
  31.     int ix0 = ceil(std::min(x0, x1));
  32.     int ix1 = ceil(std::max(x0, x1));
  33.     for (int x=ix0; x<ix1; x++) {
  34.       int y = floor(y0+(x+.5-x0)*(y1-y0)/(x1-x0)+.5);
  35.       if (x>=0 && y>=0 && x<pic.cols && y<pic.rows)
  36.         pic.at<cv::Vec3b>(y, x) = color;
  37.     }
  38.   } else {
  39.     int iy0 = ceil(std::min(y0, y1));
  40.     int iy1 = ceil(std::max(y0, y1));
  41.     for (int y=iy0; y<iy1; y++) {
  42.       int x = floor(x0+(y+.5-y0)*(x1-x0)/(y1-y0)+.5);
  43.       if (x>=0 && y>=0 && x<pic.cols && y<pic.rows)
  44.         pic.at<cv::Vec3b>(y, x) = color;
  45.     }
  46.   }
  47. }
  48.  
  49. int main(int argc, char **argv) {
  50.   if (argc<=2) {
  51.     std::cerr << "Usage: " << argv[0] << " INPIC OUTPIC [THRESH OCTAVES]" << std::endl;
  52.     exit(1);
  53.   }
  54.   std::string in_filename = argv[1];
  55.   std::string out_filename = argv[2];
  56.   int thresh = argc>3 ? atoi(argv[3]) : 30;
  57.   int octaves = argc>4 ? atoi(argv[4]) : 3;
  58.   float pattern_scale = 1;
  59.  
  60.   // read image file
  61.   cv::Mat in_pic = cv::imread(in_filename);
  62.   std::cout << "thresh=" << thresh << " octaves=" << octaves << " pattern_scale=" << pattern_scale
  63.       << std::endl;
  64.   std::cout << "in_pic is " <<  in_pic.cols << "x" << in_pic.rows
  65.       << " type=" << in_pic.type() << " elemSize1=" << in_pic.elemSize1()
  66.       << " channels=" << in_pic.channels() << std::endl;
  67.  
  68.   // initialize brisk detector
  69.   cv::BRISK brisk(thresh, octaves, pattern_scale);
  70.   brisk.create("Feature2D.BRISK");
  71.  
  72.   // run BRISK on input image to compute keypoints
  73.   std::vector<cv::KeyPoint> keypoints;
  74.   brisk.detect(in_pic, keypoints);
  75.   std::cout << "detected " << keypoints.size() << " keypoints" << std::endl;
  76.  
  77.   // create annotated image with keypoint locations and orientations
  78.   cv::Mat out_pic = in_pic.clone();
  79.   for (int i=0; i<int(keypoints.size()); i++) {
  80.     float x = keypoints[i].pt.x;
  81.     float y = keypoints[i].pt.y;
  82.     float len = keypoints[i].size/2.;
  83.     float angle = M_PI/180.*keypoints[i].angle;  // convert degrees to radians
  84.     draw_dot(out_pic, x, y, 2, cv::Vec3b(0,255,0));
  85.     draw_line_segment(out_pic, x, y, x+len*cos(angle), y+len*sin(angle), cv::Vec3b(0,0,255));
  86.   }
  87.  
  88.   // write annotated image to file
  89.   imwrite(out_filename, out_pic);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement