Harries 角点检测子

2017-01-13  本文已影响100人  沙琪玛dd

理论

工作原理
4061513A-863B-48CE-9E38-9CA9EEA30969.png

其中w(x,y)是滑动窗口的位置
I(x,y)是(x,y)坐标的灰度值

为了寻找带角点的窗口,我们搜索像素灰度变化最大的窗口,于是我们最大化公式,并使用泰勒展开式,对公式进行展开,再表示为一个矩阵表达式,


B9B9DC06-9168-49A1-AD00-ED5A9DF552ED.png

最后得出最终的等式。


1E80B8E6-C681-4A57-AB12-632BBA828643.png 7601712B-5649-4E79-B4BB-D2F2062B0529.png
![43AA6E40-E5CB-4A64-AFB8-BF3E54FAAB90.png](https://img.haomeiwen.com/i2089290/91daa38711f92680.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 4CDD965C-03A4-4F50-ACBB-78F53C9E1512.png

其中


DD125D28-007A-4A07-A550-3E67A783D0F7.png

一个窗口,它的分数 R 大于一个特定值,这个窗口就可以被认为是”角点”

代码

#import "ViewController.h"
#import <opencv2/opencv.hpp>
#import <opencv2/imgproc/types_c.h>
#import <opencv2/highgui/ios.h>
using namespace cv;
using namespace std;

Mat src,src_gray;

Mat dst, dst_norm, dst_norm_scaled;
int thresh = 200;
int max_thresh = 255;
int min_thresh = 100;
@interface ViewController ()
@property (nonatomic, retain) UIImageView *imgView1;
@property (nonatomic, retain) UIImage *img;
@property (nonatomic, retain) UISlider *slider;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  [self initMat];
    cvtColor( src, src_gray, CV_BGR2GRAY );
    [self createSlider];
    [self refresh];
}
- (void)initMat {
    self.img = [UIImage imageNamed:@"test.jpg"];
    UIImageToMat(_img, src);
}

- (void)refresh
{
    cornerHarris_demo( 0, 0 );
    [self createImageView];
}

void cornerHarris_demo( int, void* )
{
    
    dst = Mat::zeros( src.size(), CV_32FC1 );
    
    /// Detector parameters
    int blockSize = 2;
    int apertureSize = 3;
    double k = 0.04;
    
    /// Detecting corners
    cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );
    
    /// Normalizing
    normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
    convertScaleAbs( dst_norm, dst_norm_scaled );
    
    /// Drawing a circle around corners
    for( int j = 0; j < dst_norm.rows ; j++ )
    { for( int i = 0; i < dst_norm.cols; i++ )
    {
        if( (int) dst_norm.at<float>(j,i) > thresh )
        {
            circle( dst_norm_scaled, cv::Point( i, j ), 5,  Scalar(0), 2, 8, 0 );
        }
    }
    }
}
- (void)createSlider {
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height - 50, self.view.frame.size.width - 20, 40)];
    [self.view addSubview:_slider];
    [_slider addTarget:self action:@selector(sliderAction:) forControlEvents:(UIControlEventValueChanged)];
}

- (void)createImageView {
    CGFloat h = self.view.frame.size.width * 2 / 3;
    if (self.imgView1 == nil) {
        self.imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, h)];
        [self.view addSubview:_imgView1];
    }
    _imgView1.image = MatToUIImage(dst_norm_scaled);
    _imgView1.backgroundColor = [UIColor lightGrayColor];

}

- (void)sliderAction:(UISlider *)slider {
    slider.maximumValue = max_thresh;
    slider.minimumValue = min_thresh;
    thresh = slider.value;
    [self refresh];
}
@end

实验结果

上一篇下一篇

猜你喜欢

热点阅读