RSS
 

Archive for August 29th, 2009

OpenCV: Difference between cvCloneImage and cvClone

29 Aug

As of my experience,
- cvClone makes a copy of the image with respect to the ROI attached.
- cvCloneImage makes a full clone of the image (e.g., a duplicate full size image with its own matching ROI attached) rather than just copying the sub-image identified by the source ROI.
[ad#AdBrite inline]

 
No Comments

Posted in OpenCV

 

OpenCV: Access RGB value of an image

29 Aug

[ad#AdBrite inline]

CvScalar p = cvGet2D(img, y, x);
unsigned char r = p.val[0];
unsigned char g = p.val[1];
unsigned char b = p.val[2];

[ad#AdBrite inline]

 
No Comments

Posted in OpenCV

 

OpenCV: Adaptive threshold

29 Aug

[ad#AdBrite inline]

//src is IPL_DEPTH-8U, 3 channels
IplImage* gray = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U , 1);
cvCvtColor(src ,gray, CV_RGB2GRAY);

double parameters[2];
double minDisp = 40;

double mean, stdDev;

cvMean_StdDev(gray, &mean, &stdDev);
parameters[0]=(double)2;
parameters[1]=(double) stdDev * 0.9;

cvAdaptiveThreshold(gray,gray,
double(255),
   	CV_STDDEV_ADAPTIVE_THRESH,
CV_THRESH_BINARY,

parameters);

[ad#AdBrite inline]

 
No Comments

Posted in OpenCV

 

OpenCV: Change contrast and brightness of an image

29 Aug

[ad#AdBrite inline]

// CHANGES BRIGHTNESS AND/OR CONTRAST
// INPUT: 	IplImage * (nChannels=1 OR nChannels=3; IPL_DEPTH_8U)
// RETURNS: 	IplImage * SAME DEPTH AND CHANNELS AS INPUT

IplImage * ContrastBrightness(IplImage *src, int Contrast, int Brightness)

{

	if(Contrast > 100) Contrast = 100;
	if(Contrast < -100) Contrast = -100;
	if(Brightness > 100) Brightness = 100;
	if(Brightness < -100) Brightness = -100;

	uchar lut[256];

	CvMat* lut_mat;
	int hist_size = 256;
	float range_0[]={0,256};
	float* ranges[] = { range_0 };

	int i;

	IplImage * dest = cvCloneImage(src);

	IplImage * GRAY = cvCreateImage(cvGetSize(src),src->depth,1);

	if (src->nChannels ==3)
	{
		cvCvtColor(src,GRAY,CV_RGB2GRAY);
	}
	else
	{
		cvCopyImage(src,GRAY);
	}

    lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
    cvSetData( lut_mat, lut, 0 );

	 /*
     * The algorithm is by Werner D. Streidt
     * (http://visca.com/ffactory/archives/5-99/msg00021.html)
     */

	if( Contrast > 0 )

    {
        double delta = 127.* Contrast/100;
        double a = 255./(255. - delta*2);
        double b = a*(Brightness - delta);
        for( i = 0; i < 256; i++ )
        {
            int v = cvRound(a*i + b);

            if( v < 0 )
                v = 0;
            if( v > 255 )
                v = 255;
            lut[i] = v;
        }
    }
    else
    {
        double delta = -128.* Contrast/100;

        double a = (256.-delta*2)/255.;
        double b = a* Brightness + delta;
        for( i = 0; i < 256; i++ )
        {
            int v = cvRound(a*i + b);
            if( v < 0 )
                v = 0;

            if( v > 255 )
                v = 255;
            lut[i] = v;
        }
    }

	if (src->nChannels ==3)
	{

		IplImage * R = cvCreateImage(cvGetSize(src),src->depth,1);

		IplImage * G = cvCreateImage(cvGetSize(src),src->depth,1);
		IplImage * B = cvCreateImage(cvGetSize(src),src->depth,1);

		cvCvtPixToPlane(src,R,G,B,NULL);

		// PERFORM IT ON EVERY CHANNEL
		cvLUT( R, R, lut_mat );

		cvLUT( G, G, lut_mat );
		cvLUT( B, B, lut_mat );

		cvCvtPlaneToPix(R,G,B,NULL,dest);

		cvReleaseImage(&R);
		cvReleaseImage(&G);
		cvReleaseImage(&B);

	}
	else
	{

		//PERFORM IT ON THE CHANNEL
		cvLUT( GRAY, dest, lut_mat );
	}

	cvReleaseImage(&GRAY);
	cvReleaseMat( &lut_mat);

	return dest;

}

[ad#AdBrite inline]

 
1 Comment

Posted in OpenCV