[OpenCV] Corner Detection

Let’s try to detect corner from an image.
In this sample, I am using my own picture = building.jpg.
You can edit the below source code and try to detect corner from different image.

I am implementing two types of function to detect corner :
1. cvCornerMinEigenVal
The function cvCornerMinEigenVal is to calculate and store the minimal eigen value of derivative covariation matrix for every pixel, i.e. min(λ1, λ2) in terms of the previous function.

2. cvCornerHarris
Harris edge detector. You can refer to the original paper = A Combined Corner and Edge Detector

You may also refer to the next article : click here (Harris Corner Detection).

Source code :

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main (void)
{
	int i, corner_count = 150;
	IplImage *dst_img1, *dst_img2, *src_img_gray;
	IplImage *eig_img, *temp_img;
	CvPoint2D32f *corners;

	//image file
	char imagePath[256] = "c:\\images\\building.jpg";
	printf("%s\n", imagePath);

	dst_img1 = cvLoadImage (imagePath, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
	dst_img2 = cvCloneImage (dst_img1);
	src_img_gray = cvLoadImage (imagePath, CV_LOAD_IMAGE_GRAYSCALE);
	eig_img = cvCreateImage (cvGetSize (src_img_gray), IPL_DEPTH_32F, 1);
	temp_img = cvCreateImage (cvGetSize (src_img_gray), IPL_DEPTH_32F, 1);
	corners = (CvPoint2D32f *) cvAlloc (corner_count * sizeof (CvPoint2D32f));

	// (1)Corner detection using cvCornerMinEigenVal
	cvGoodFeaturesToTrack (src_img_gray, eig_img, temp_img, corners, &corner_count, 0.1, 15);
	cvFindCornerSubPix (src_img_gray, corners, corner_count,
					  cvSize (3, 3), cvSize (-1, -1), cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03));
	// (2)Draw the detected corner
	for (i = 0; i < corner_count; i++)
	cvCircle (dst_img1, cvPointFrom32f (corners[i]), 3, CV_RGB (255, 0, 0), 2);

	//Message for debugging
	printf("MinEigenVal corner count = %d\n", corner_count);

	// (3)Corner detection using cvCornerHarris
	corner_count = 150;
	cvGoodFeaturesToTrack (src_img_gray, eig_img, temp_img, corners, &corner_count, 0.1, 15, NULL, 3, 1, 0.01);
	cvFindCornerSubPix (src_img_gray, corners, corner_count,
					  cvSize (3, 3), cvSize (-1, -1), cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03));
	// (4)Draw the detected corner
	for (i = 0; i < corner_count; i++)
	cvCircle (dst_img2, cvPointFrom32f (corners[i]), 3, CV_RGB (0, 0, 255), 2);

	//Message for debugging
	printf("Harris corner count = %d\n", corner_count);

	// (5)Display the result
	cvNamedWindow ("EigenVal", CV_WINDOW_AUTOSIZE);
	cvShowImage ("EigenVal", dst_img1);
	cvNamedWindow ("Harris", CV_WINDOW_AUTOSIZE);
	cvShowImage ("Harris", dst_img2);
	cvWaitKey (0);

	cvDestroyWindow ("EigenVal");
	cvDestroyWindow ("Harris");
	cvReleaseImage (&dst_img1);
	cvReleaseImage (&dst_img2);
	cvReleaseImage (&eig_img);
	cvReleaseImage (&temp_img);
	cvReleaseImage (&src_img_gray);

	return 0;
}

Result :

10 responses to this post.

  1. Posted by umek1 on May 5, 2011 at 12:31 am

    nice POST there.. thx b4.

    umek, student ee at ITS surabaya 07

    Reply

  2. […] You may refer the previous article : click here. […]

    Reply

  3. Posted by sachi on October 8, 2012 at 10:09 am

    which editor I can use to run this code?? there is a problem if I use Codeblock….

    Reply

    • Thank you for the comment. I am using Microsoft Visual Studio 2008.
      May I know what kind of error did you get from Codeblock?

      Reply

      • Posted by mezguich med amine on December 8, 2012 at 6:29 am

        bjr je suis un étudiant à esprit est mon PFE c’est la réalisation d’une application de reconnaissance de logo sous android , j’ai commencé a travailler avec neuroph studio mais la problème c’est que la base si elle construite je ne peux pas la modifié avez vous une idée sur la partie “reconnaissance” , cad après avoir détecter les points d’intérêt quelle est la phase suivantes et quelle sont les méthodes que je puisse les utilisées pour reconnaître l’image ou plus précisément le logo , merci d’avance

      • Hello, thank you for the comment. But I am sorry, i don’t know French. Would you let me know your comment in English?

  4. Posted by mezguich med amine on December 9, 2012 at 5:20 am

    I’m sorry because yesterday before reading the page I did the conversion, and I forgot that it in English. so I am a student at “ESPRIT” ,and my PFE is the realization of applications for recognition of logo in android and it is a theme in this is the first time I work ,so you have an idea on the recognition that is to say after having detect points of interest what is now the next phase?What methods are used for recognition? please help me because I am lost thank you in advance

    Reply

  5. Posted by Medo Ziad on March 29, 2013 at 11:55 pm

    if i want the upload image then apply the alogrthim >> how i do that ?

    Reply

  6. Posted by hana bouazizi on August 1, 2014 at 11:00 pm

    hi

    I need your help in the step using “opencv c++”: cut the video into shots using distance hist (i1, i2) ;this distance to calculate the intersection of color histograms gray levels of two frames i1 and i2 ;if this distance is greater than a threshold “S” we say that these two frames belong to two different shots…display these shots.

    thanks

    Reply

  7. Posted by Naseemsheikh on January 19, 2017 at 10:00 am

    can you please give me the code for video also

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: