image sharpening

hi

i am trying to find a way to sharpen a rgb image, which i get from the video grabber.

i used opencv blur before but there seems to be no direct sharpen function.

i found this code, that sharpens a greyscale image.
i am now trying to either split my rgb image in to its 3 separate channels.
this could do done by looping through all the pixel, but is probably too performance heavy.

or i find a way to re-write the following code to work with a 3 channel image.

any ideas?

thanks, stephan.

  
void testApp::doGraySharpening(IplImage* img ) {  
	//[http://permalink.gmane.org/gmane.comp.lib.opencv/35015](http://permalink.gmane.org/gmane.comp.lib.opencv/35015)  
	int w = img->width;  
	  
	int h = img->height;  
	  
	IplImage* gray2 = cvCreateImage( cvGetSize(img) , 32, 1 );  
	  
	cvConvertScale( img, gray2, 1.0 );  
	  
	IplImage* lapl = cvCreateImage( cvGetSize(img) , 32, 1 );  
	  
	CvMat* ker = cvCreateMat( 3, 3, CV_32FC1);  
	  
	cvSet( ker, cvScalarAll( -1.0 ) );  
	  
	cvSet2D( ker, 1, 1, cvScalarAll( 15.0 ) );  
	  
	cvFilter2D( gray2, lapl, ker );  
	  
	cvReleaseMat( &ker );  
	  
	double maxv = 0.0;  
	  
	double minv = DBL_MAX;  
	  
	cvMinMaxLoc( lapl, &minv, &maxv );  
	  
	for( int i=0; i<w*h; i++ )  
		  
	{  
		  
		double lap_val = cvGet1D( lapl, i ).val[0];  
		  
		int v = (int)( (255.0*lap_val/ maxv) +0.5);  
		  
		cvSet1D( img, i, cvScalarAll( v) );  
		  
	}  
	  
	maxv = 0.0;  
	  
	cvMinMaxLoc( img, &minv, &maxv );  
	  
	for( int i=0; i<w*h; i++ )  
		  
	{  
		  
		double val = cvGet1D( img, i ).val[0];  
		  
		int v = (int)( (255.0*val/maxv) + 0.5);  
		  
		cvSet1D( img, i, cvScalarAll( v) );  
		  
	}  
	  
	cvReleaseImage( &gray2 );  
  
}  

you need to apply a convolution filter on the whole image.
i suggest you this link : http://www.roborealm.com/help/Convolution.php

for RGB image , you have to add all channel separtly, but in the same pass.

thanks for the reply.
the convolution filter looks very interesting but i have no clue on how to implement it.

i got the above code working for a RGB image, using cvSplit and cvMerge.

in void testApp::update i call doRGBSharpening(orgImg.getCvImage());

  
void testApp::doRGBSharpening(IplImage *img ) {  
//[http://permalink.gmane.org/gmane.comp.lib.opencv/35015](http://permalink.gmane.org/gmane.comp.lib.opencv/35015)  
	int w = img->width;  
	  
	int h = img->height;  
	  
	IplImage* imgRGB[3];  
	IplImage* grayRGB[3];  
  
	for(int i=0; i<3;i++){  
		imgRGB[i] = cvCreateImage( cvGetSize(img) , 8, 1 );  
		grayRGB[i] = cvCreateImage( cvGetSize(img) , 32, 1 );  
	}  
	  
	cvSplit(img, imgRGB[0], imgRGB[1], imgRGB[2], NULL);  
	  
	for(int n=0; n<3;n++){  
		  
		cvConvertScale( imgRGB[n], grayRGB[n], 1.0 );  
		  
		IplImage* lapl = cvCreateImage( cvGetSize(imgRGB[n]) , 32, 1 );  
		  
		CvMat* ker = cvCreateMat( 3, 3, CV_32FC1);  
		  
		cvSet( ker, cvScalarAll( -1.0 ) );  
		  
		//cvSet2D( ker, 1, 1, cvScalarAll( 15.0 ) );  
		cvSet2D( ker, 1, 1, cvScalarAll( sharpenVal ) );  
		cvFilter2D( grayRGB[n], lapl, ker );  
		  
		cvReleaseMat( &ker );  
		  
		double maxv = 0.0;  
		  
		double minv = DBL_MAX;  
		  
		cvMinMaxLoc( lapl, &minv, &maxv );  
		  
		for( int i=0; i<w*h; i++ ){  
			double lap_val = cvGet1D( lapl, i ).val[0];  
			  
			int v = (int)( (255.0*lap_val/ maxv) +0.5);  
			  
			cvSet1D( imgRGB[n], i, cvScalarAll( v) );  
		}  
		  
		maxv = 0.0;  
		  
		cvMinMaxLoc( imgRGB[n], &minv, &maxv );  
		  
		for( int i=0; i<w*h; i++ ){  
			double val = cvGet1D( imgRGB[n], i ).val[0];  
			  
			int v = (int)( (255.0*val/maxv) + 0.5);  
			  
			cvSet1D( imgRGB[n], i, cvScalarAll( v) );  
		}  
		  
		  
	}  
  
	cvMerge(imgRGB[0], imgRGB[1], imgRGB[2], NULL, img);  
  
	for(int i=0; i<3;i++){  
		cvReleaseImage( &grayRGB[i] );  
		cvReleaseImage( &imgRGB[i] );  
	}  
	  
}  
  

as for the GRB shapening, i suggest you try this-sharpen-method i have searched for you. you can have some filtering features. mode adjustment and some other fuctions in this image tool. it made a great effect on your blurry images.