OpenCV2.4.4实现Harris角点检测

  1. #include "opencv2/highgui/highgui.hpp"  
  2. #include "opencv2/imgproc/imgproc.hpp"  
  3. #include <iostream>  
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6.   
  7. using namespace cv;  
  8. using namespace std;  
  9.   
  10. //全局变量  
  11. Mat src, src_gray;//源图和灰度图  
  12. int thresh = 200;//默认阈值  
  13. int max_thresh = 255;//滑块条最大值  
  14.   
  15.   
  16. void cornerHarris_demo( intvoid* )  
  17. {  
  18.     Mat dst, dst_norm, dst_norm_scaled;  
  19.     dst = Mat::zeros( src.size(), CV_32FC1 );  
  20.   
  21.     //参数  
  22.     int blockSize = 2;  
  23.     int apertureSize = 3;  
  24.     double k = 0.04;  
  25.   
  26.     //Harris角点检测  
  27.     cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );  
  28.   
  29.     //像素归一化到[0,255]  
  30.     normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );  
  31.     convertScaleAbs( dst_norm, dst_norm_scaled );//去绝对值后变换为8位无符号类型  
  32.   
  33.     Mat src_copy = src.clone();  
  34.     //在角点上画圈  
  35.     forint j = 0; j < dst_norm.rows ; j++ )  
  36.     {  
  37.         forint i = 0; i < dst_norm.cols; i++ )  
  38.         {  
  39.             if( (int) dst_norm.at<float>(j,i) > thresh )  
  40.             {  
  41.                 circle(dst_norm_scaled,Point(i,j),3,Scalar(0));  
  42.                 circle(src_copy,Point(i,j),3,Scalar(0,255,0));  
  43.             }  
  44.         }  
  45.     }  
  46.     /// Showing the result  
  47.     namedWindow( "Corners", CV_WINDOW_AUTOSIZE );  
  48.     imshow( "Corners", dst_norm_scaled );  
  49.     imshow( "Src", src_copy );  
  50.   
  51. }  
  52.   
  53. int main()  
  54. {  
  55.     //加载源图像并转换为灰度图  
  56.     src = imread("horse.jpg");  
  57.     cvtColor( src, src_gray, CV_BGR2GRAY );  
  58.   
  59.     //显示源图  
  60.     namedWindow( "Src", CV_WINDOW_AUTOSIZE );  
  61.     imshow( "Src", src );  
  62.   
  63.     //创建滑块条,并指定回调函数为cornerHarris_demo(),每次滑块位置改变时都会调用此函数  
  64.     createTrackbar( "阈值:""Src", &thresh, max_thresh, cornerHarris_demo );  
  65.   
  66.     //调用函数,计算角点  
  67.     cornerHarris_demo( 0, 0 );  
  68.   
  69.     waitKey(0);  
  70.     return(0);  
  71. }  
  72. 效果图:

  73. 效果图自己测试的。
  74. 程序转载自:http://blog.csdn.net/masibuaa/article/details/8986098

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值