Tuesday, June 14, 2011

Code snippets

Before you use the snippets, I hope you installed openCV, compiled and understood the helloworld program given in the following website.

http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html

If not go back to my previous posts

All the code in the snippets will be similar to that of hello world program as shown below

//Program to create the negative of a given image

//Links used:http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/



#include<stdio.h>
#include<cv.h>
#include<highgui.h>
int main(int argc,char *argv[])
{int i,j,k;
IplImage *img=0;
int height,width,step,components;
uchar *data;
if(argc==0)
{printf("Could not load image");
return 0;
}
//Loading image
img=cvLoadImage(argv[1]);
if(img==0)
{printf("Image loading error");
return 0;
}
//Assigning data
height=img->height;
width=img->width;
step=img->widthStep;
components=img->nChannels;
data=(uchar *)(img->imageData);
printf("The picture's size is %d X %dnThe image has %d componentsnEach step length is %dn",height,width,components,step);
//To get the negative of image
for(i=0;i<height;i++)
for(j=0;j<width;j++)
for(k=0;k<components;k++)
data[i*step+j*components+k]=255-data[i*step+j*components+k];
cvNamedWindow("Lena",CV_WINDOW_AUTOSIZE);
cvMoveWindow("Lena",50,50);
//Showing the image
cvShowImage("Lena",img);
cvSaveImage("Lenanegative.jpg",img);
//Waiting for a key press
cvWaitKey(0);
//Releasing the image
cvReleaseImage(&img);
return 0;
}

The code snippets contain three additional problems and their folder names are as follows :


  1. MnMdVr - This folder contains the finding of mean, median and standard deviation of image, using openCV inbuilt functions and some hacking of the helloworld.cpp code, which you wrote previously
  2. Block-Processing - This is  same as above, except, the finding of mean, median and variance are found block wise, like for every 20x20 pixels or so
  3. Sobel - This one contains the famous sobel edge detection technique, very simple one


This is the link for code snippets

Code Snippets

If the link goes down, tell me in the comments section, I will correct it.Also if you have any doubts understanding the code snippets, do comment them.

So keep trying to understand the code.If you already did the home work yourself, then very good.

If you have come this far, it is just a few steps away.On thursday, we will discuss about face detection and then saturday and sunday, we will drill down the most important topic -- "Haar Training"

And one more thing, from next post on, I will be personally posting and analyze the code snippets for you, step by step.

No comments:

Post a Comment