RSS
 

Archive for August, 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

 

Ubuntu: Install Opencv from source step-by-step

25 Aug

To install OpenCV library in Ubuntu from source, I tried these steps. The code of OpenCV is not the last but the one which is in the Ubuntu repository. If you want to install the last version just download it from the official SVN in sourceforge.
[ad#Amazon Opencv]
You can install opencv anywhere but its best you put in the propoer place

1) Sign as root

sudo su

2) Go to

cd /usr/local/src

3) prepare your environment

apt-get install build-essential

4) prepare your environment

apt-get install dpkg-dev

5) Download opencv packages

apt-get source opencv

6) Go to extracted opencv folder

cd opencv-1.0

7) List all dependency package informed in

more /debian/control 

8) Install all dependency package informed in /debian/control by invoking

apt-get install [package-1] package-2] [packga-3] ... [package-n]

9) Also install other necessary package

apt-get install fakeroot ffmpeg quilt

10) Build package by invoking

dpkg-buildpackage -us -uc -rfakeroot

11) Go to samples folder

cd /samples/c

12) Build samples

make -f  Makefile.debian

13) Plug in your webcam and run the samples

./facedetect

14) See you face detected in monitor

 
No Comments

Posted in Ubuntu