Eigenvalues of an image – OpenCV

OpenCV has great resources for beginners as well as for advanced users. But for those caught in the intermediate stage, you might call it an image processor’s angsty teenage years, there are little options other than scouring the messageboards and the helpful people at StackOverflow.

So I decided to address two issues that had held me up initially when starting out with OpenCV. The first one: converting an image (specifically a grayscale image) into a 2D array is something that comes naturally. Further operations at the pixel level become really simplified, the trade-off (as in everything) being some amount of computing efficiency. The second point is something that is directly related to my present project – that of computing the eigenvectors of an image.

I decided to punch both these concepts into a working function. All of this is self-evident really. Hopefully someone will waste less time than i did in a)figuring it out oneself or b)searching with Zen-like dedication until discovering someone else who did.

void calcEigen(IplImage* src) //input a 32 floating point image

	if(src->height != src->width)
		printf("\nDimensions must be same for eigen!!");
	int j,i;
	float **p;
	CvScalar se;

        //creating the 2D array - this part will work for unequal dimensions also
	p = (float**)malloc(src->height*sizeof(float*));
	for(i=0; i<src->height; i++)
		p[i] = (float*)malloc(src->width*sizeof(float));

	float *a;  //1D array reqd to convert IplImage to CvMat (grrrr)
	a = (float*)malloc( (src->height) * (src->width) * sizeof(float) );
	long int k=0;
	//image pixels into 2D array
	for(i = 0; i < src->height; ++i)
		for(j = 0; j <src->width; ++j)
			se = cvGet2D(src, i, j);
			p[i][j] = (float)se.val[0];


	//2D array into 1D array
	for(i = 0; i < src->height; ++i)
		for(j = 0; j <src->width; ++j)
			a[k++] = p[i][j];
	//1D datapoints into CvMat
	CvMat mat = cvMat(src->height,src->height,CV_32FC1, a); 
/*the resulting CvMat is used for many computations*/
		for(i = 0; i < src->height; ++i)
			for(j = 0; j <src->width; ++j)
				float t = (float)cvmGet(&mat,i,j); // Get M(i,j)
				//a[k++] = p[i][j];

/*Eigenvalue specific code begins*/
                CvScalar scal;
		CvMat* evec  = cvCreateMat(src->height,src->height,CV_32FC1); //eigenvectors
		CvMat* eval  = cvCreateMat(1,src->height,CV_32FC1);  //eigenvalues (1xN)
		cvEigenVV(&mat, evec, eval, 1);
			for( j = 0; j < eval->cols; j++ )
				/*access the obtained eigenvalues*/
                                scal = cvGet2D( eval, 0, j );
				printf( "\n%f\n", scal.val[0]);
		cvReleaseMat(&evec); //house-cleaning :P

About Aruni RC

Engineering student, IT.Programming and fantasy, sci-fi enthusiast.

Posted on April 26, 2011, in C, image processing, OpenCV, programming. Bookmark the permalink. 3 Comments.

  1. full fledged code 🙂 …helpful to many ! 😉

  2. hi i study with opencv and i have problem somone can help me please

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: