Moving on…

February 12, 2011 § Leave a comment

I started this blog primarily for jotting down any technical stuff that I come across in my college life. But I think, I don’t need a full fledged tech blog as I don’t have much to write down and share with guys over net. I believe I can continue my work in my personal blog itself and that would make my personal blog more complete in yet another sense.

I have already moved all my posts in this blog to my personal blog vaamarnath.wordpress.com. More ideas and notes would be put up in my blog mentioned above.

Thanks guys…

OpenCV – Image Resize (Pixel Replication)

October 6, 2010 § 3 Comments

Image resizing is an important function for image processing applications. Its really important that the information content is not lost during zooming or shrinking of an image. In OpenCV, we have a library function cvResize that can be used for image resizing. Here, I am implementing pixel replication algorithm for zooming of images.

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

int main(int argc, char **argv)

{	const char* imagename = argc > 1 ? argv[1] : "lena128.png";
	IplImage* img = 0;
	int h, w, step, zstep, channels, i, j, k, px, py;
	uchar *data, *newdata;
	//reading image, by default reads lena128.png
	img = cvLoadImage(imagename, CV_LOAD_IMAGE_UNCHANGED);
	if(!img)
		printf("Could not load image file: %s\n", imagename);
	h = img->height;
	w = img->width;
	step = img->widthStep;
	channels = img->nChannels;
	data = (uchar *)img->imageData;

	int factor = 2;	//zoom by 2x

	//create new image of required size
	IplImage* zoomed = cvCreateImage(cvSize(factor*w, factor*h), 8, channels);
	h = zoomed->height;
	w = zoomed->width;
	zstep = zoomed->widthStep;
	channels = zoomed->nChannels;
	newdata = (uchar *)zoomed->imageData;

	//pixel replication algorithm
	for(i=0; i<h; i++)
		if(i%factor==0)
			for(j=0; j<w; j++)
				if(j%factor==0)
					for(k=0; k<channels; k++)
						for(px=0; px<factor; px++)
							for(py=0; py<factor; py++)
								newdata[(i+px)*zstep+(j+py)*channels+k] = data[(i*step)/factor+(j*channels)/factor+k];

	//display the zoomed image
	cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE);
	cvShowImage("OpenCV", zoomed);
	cvWaitKey(0);
	cvReleaseImage(&zoomed);
	cvReleaseImage(&img);
	return 0;
}

Now lets see how the code works on a 64×64 image. The image on left shows the input image and the image on the right shows the image zoomed by 2x.

Switch over to LibreOffice!

October 5, 2010 § Leave a comment

Many of you must be aware of the formation of ‘The Document Foundation’ to be free from Oracle once it acquired Sun. OOo is now re-branded as ‘LibreOffice’. This is really good news for open source community.

Lets see how LibreOffice can be installed in Ubuntu and Debian based systems. Currently only beta versions of EN_US builds are available.

« Read the rest of this entry »

OpenCV – Extracting RGB from a color image

September 8, 2010 § 4 Comments

In this post I would like to go into details of a color image. In opencv, an image is represented as a 1 dimensional entity. From the top left hand corner of the image, each pixel is represented as BGR in an array form. That is. each row of pixels is represented one after another where each pixel information is given completely. An RGB image is a 3 channel image where each pixel is represented by three 8 bit values corresponding to blue, green and red components. Here, I would give an example how to extract only the blue components of an input image.

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

int main(int argc, char **argv)

{
 const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
 IplImage* img = 0;
 int h, w, step, channels, i, j, k;
 uchar *data, *trans;
 img = cvLoadImage(imagename, CV_LOAD_IMAGE_UNCHANGED);
 if(!img)
 printf("Could not load image file: %s\n", imagename);
 h = img->height;
 w = img->width;
 step = img->widthStep;
 channels = img->nChannels;
 data = (uchar *)img->imageData;

 IplImage* RGB = cvCreateImage(cvGetSize(img), 8, 1);
 trans = (uchar *)RGB->imageData;
 int trans_step;
 trans_step = RGB->widthStep;

//extracting necessary components
 int col = 0;
 for(i=0; i<h; i++)
 for(j=0; j<w; j++)
 trans[i*trans_step+j] = data[i*step+j*channels+col];

 cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE);
 cvShowImage("OpenCV", RGB);
 cvWaitKey(0);
 cvReleaseImage(&img);
 cvReleaseImage(&RGB);

 return 0;
}

In this example code, we read an image into img and extract required component into a single channel image RGB. Here, in the code section mentioning extraction features takes the blue components into the image data array trans as col is set as 0. That is we are copying every one first value representing an image, i.e. the blue component into our new image. If we set col = 1 we can obtain the green sections of the image and similarly col = 2 gives red components of the image.

Basic OpenCV Programming – Negative of an Image

September 5, 2010 § 2 Comments

For starting with image processing using OpenCV, I would suggest to start working on problems and try to write the code for its solution. This can give you real experience in coding and learning syntax can be done as and when its required.

Lets see the code to obtain the negative of an image.

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

int main(int argc, char **argv)
{
//reading the input image
const char* imagename = argc &gt; 1 ? argv[1] : "lena.jpg";
IplImage* img = 0;
int h, w, step, channels, i, j, k;
uchar *data;
img = cvLoadImage(imagename, CV_LOAD_IMAGE_UNCHANGED);
if(!img)
printf("Could not load image file: %s\n", imagename);
//obtaining the image parameters
h = img-&gt;height;
w = img-&gt;width;
step = img-&gt;widthStep;
channels = img-&gt;nChannels;
data = (uchar *)img-&gt;imageData;
//transforming to negative
for(i=0; i&lt;h; i++)
for(j=0; j&lt;w; j++)
for(k=0; k&lt;channels; k++)
data[i*step+j*channels+k] = 255 - data[i*step+j*channels+k];
//displaying the negative
cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE);
cvShowImage("OpenCV", img);
cvWaitKey(0);
cvReleaseImage(&amp;img);
return 0;
}

In this code we have read an image and converted it to its negative. The details of the functions used are given below:

cvLoadImage() Loads the required image.

cvNamedWindow() Creates a window with the name and window size as specified.

cvShowImage() Outputs the image to the window created earlier.

cvWaitKey() Waits till the user interrupts.

For more detailed understanding of these functions, I would recommend to read the OpenCV documentation.

Starting off Image Processing with OpenCV

September 5, 2010 § 3 Comments

Image processing is an important field where a lot of signal processing concepts learned is applied. Image processing maybe done with any programming language. I think Matlab and C are the most commonly used and the latter being more popular due to the user friendly interface. But, C gives you more power and flexibility to solve image processing problems. OpenCV is a image – video processing library developed by Intel and released under BSD license. It can serve as a great tool for image manipulation and transformations in C programming.

I am using GCC to compile my C programs in my Ubuntu machine. You may use any other C compilers and please note to include the CV libraries in your include path.

gcc program.c -o program -I /usr/include/opencv/ -L /usr/local/lib/ -lcv -lhighgui -lcvaux -lm

The above command is used for compiling the programs. Please see that you have installed libcv-dev, libcvaux and libhighgui-dev packages installed from the repository in case of linux distributions. Other users please check http://opencv.willowgarage.com/.

To run the program after compilation,

./program argumentsifany

In the following posts I would include sample programs and explanations for some of the library functions in OpenCV.