Obj-C-C ++中的OpenCV goodFeaturesToTrack

时间:2011-12-19 20:02:09

标签: c++ objective-c opencv

我正在尝试迭代从cv :: goodFeaturesToTrack获得的点数。

我使用此代码检索我的积分:

std::vector<std::vector<cv::Point2f> > corners;
cv::goodFeaturesToTrack(image,corners, 500, 0.01, 10);

我的想法,这不起作用:

for (size_t idx = 0; idx < corners.size(); idx++) {
        cv::circle(image,corners.at(idx),radius,color,thickness);
}

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

探测器goodFeaturesToTrack(实际上所有特征探测器)都填充了一个特征向量,同时您尝试向其传递一个特征向量的向量。您的代码的其余部分看起来很好,但您应该更改行

std::vector<std::vector<cv::Point2f>> corners;

std::vector<cv::Point2f> corners;

希望一切顺利。

答案 1 :(得分:1)

不确定你的意思是“不起作用”。但是,您可以尝试查看goodFeaturesToTrack_Demo.cpp以查看该用法是否可以解决您的问题。

使用代码进行更新:

/**
 * @function goodFeaturesToTrack_Demo.cpp
 * @brief Demo code for detecting corners using Shi-Tomasi method
 * @author OpenCV team
 */

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

/// Global variables
Mat src, src_gray;

int maxCorners = 23;
int maxTrackbar = 100;

RNG rng(12345);
const char* source_window = "Image";

/// Function header
void goodFeaturesToTrack_Demo( int, void* );

/**
 * @function main
 */
int main( int, char** argv )
{
  /// Load source image and convert it to gray
  src = imread( argv[1], 1 );
  cvtColor( src, src_gray, COLOR_BGR2GRAY );

  /// Create Window
  namedWindow( source_window, WINDOW_AUTOSIZE );

  /// Create Trackbar to set the number of corners
  createTrackbar( "Max  corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo );

  imshow( source_window, src );

  goodFeaturesToTrack_Demo( 0, 0 );

  waitKey(0);
  return(0);
}

/**
 * @function goodFeaturesToTrack_Demo.cpp
 * @brief Apply Shi-Tomasi corner detector
 */
void goodFeaturesToTrack_Demo( int, void* )
{
  if( maxCorners < 1 ) { maxCorners = 1; }

  /// Parameters for Shi-Tomasi algorithm
  vector<Point2f> corners;
  double qualityLevel = 0.01;
  double minDistance = 10;
  int blockSize = 3;
  bool useHarrisDetector = false;
  double k = 0.04;

  /// Copy the source image
  Mat copy;
  copy = src.clone();

  /// Apply corner detection
  goodFeaturesToTrack( src_gray,
               corners,
               maxCorners,
               qualityLevel,
               minDistance,
               Mat(),
               blockSize,
               useHarrisDetector,
               k );


  /// Draw corners detected
  cout<<"** Number of corners detected: "<<corners.size()<<endl;
  int r = 4;
  for( size_t i = 0; i < corners.size(); i++ )
     { circle( copy, corners[i], r, Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255)), -1, 8, 0 ); }

  /// Show what you got
  namedWindow( source_window, WINDOW_AUTOSIZE );
  imshow( source_window, copy );
}