第一种
#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 thresh = 200;
int max_thresh = 255;
char* source_window = "Source image";
char* corners_window = "Corners detected";
/// Function header
void cornerHarris_demo( int, void* );
/** @function main */
int main( int argc, char** argv )
{
/// Load source image and convert it to gray
src = imread( argv[1], 1 );
cvtColor( src, src_gray, CV_BGR2GRAY );
/// Create a window and a trackbar
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
createTrackbar( "Threshold: ", source_window, &thresh, max_thresh, cornerHarris_demo );
imshow( source_window, src );
cornerHarris_demo( 0, 0 );
waitKey(0);
return(0);
}
/** @function cornerHarris_demo */
void cornerHarris_demo( int, void* )
{
Mat dst, dst_norm, dst_norm_scaled;
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, Point( i, j ), 5, Scalar(0), 2, 8, 0 );
}
}
}
/// Showing the result
namedWindow( corners_window, CV_WINDOW_AUTOSIZE );
imshow( corners_window, dst_norm_scaled );
}
第二种
#include "cv.h"
#include "highgui.h"
#include<iostream>
int main(int argc,char** argv)
{
IplImage* pImg;
IplImage* pHarrisImg;
IplImage* grayImage;
IplImage* dst8;
double minVal=0.0, maxVal=0.0;
double scale, shift;
double min=0, max=255;
if((pImg=cvLoadImage("/home/dz/study/7class/a.jpg",1))!=NULL)
{
cvNamedWindow("source",1);
cvShowImage("source",pImg);
pHarrisImg=cvCreateImage(cvGetSize(pImg),IPL_DEPTH_32F,1);
//there we should define IPL_DEPTH_32F rather than IPL_DEPTH_8U
grayImage=cvCreateImage(cvGetSize(pImg),IPL_DEPTH_8U,1);
dst8=cvCreateImage(cvGetSize(pImg),IPL_DEPTH_8U,1);//this is for the result image
grayImage->origin=pImg->origin; //there make sure the same origin between grayImage and pImg
cvCvtColor(pImg,grayImage,CV_BGR2GRAY);//cause harris need gray scale image,we should convert RGB 2 gray
int block_size=7;
//do harris algorithm
cvCornerHarris(grayImage,pHarrisImg,block_size,3,0.04);
//convert scale so that we see the clear image
cvMinMaxLoc(pHarrisImg,&minVal,&maxVal,NULL,NULL,0);
std::cout<<minVal<<std::endl;
std::cout<<maxVal<<std::endl;
scale=(max-min)/(maxVal-minVal);
shift=-minVal*scale+min;
cvConvertScale(pHarrisImg,dst8,scale,shift);
cvNamedWindow("Harris",1);
cvShowImage("Harris",dst8);
cvWaitKey(0);
cvDestroyWindow("source");
cvDestroyWindow("Harris");
cvReleaseImage(&dst8);
cvReleaseImage(&pHarrisImg);
return 0;
}
return 1;
}