Tuesday, October 7, 2008

Source code for bwlabel() & regionprops()

Hi..
This is the source code for finding out the connected components as well as the area and centroid of that perticular component... use n njoy.. This source code is equivalent to the bwlabel() & regionprops()commands that we basically use in matlab..

NOTE: For this you require the OpenCV Library installed on your PC & also VC++2008 Express Edition




#include "stdafx.h"
#include "cvaux.h"
#include "highgui.h"




struct region_props
{
int value;
int area;
float x;
float y;
} rp[50];

int no_of_labels[50];
int eqMat[50][50];
int cnt=0;

int check_label(int value)
{
int i;
for(i=0;i {
if(no_of_labels[i]==value)
return 0;
}
if(i==cnt)
return 1;
}

void add_to_labels(int value)
{
int success_flag;
success_flag= check_label(value);
if(success_flag==1)
{
no_of_labels[cnt]=value;
cnt++;
}
}

void equivalance_matrix(int row_no, int col_no)
{
eqMat[row_no][col_no]=1;
eqMat[col_no][row_no]=1;
}
void regionprops(IplImage* img)
{

int i,j;
for(i=0;i<50;i++)
{
for(j=0;j<50;j++)
{
eqMat[i][j]=0;
}
}

//IplImage *img = cvLoadImage("I:/test.bmp",0);
int h1=img->height,w1=img->width;
unsigned long ul;
float varf;

float var,var1,var2,var3,var4;
//unsigned long ul;
int x;
int label=0;
///////////////////////////////////////////////////////////////////////
//fill the first row, first column, last row, last column by 0
for(i=0;i {
cvSetReal2D(img,0,i,0);
cvSetReal2D(img,(h1-1),i,0);
}

for(i=0;i {
cvSetReal2D(img,i,0,0);
cvSetReal2D(img,i,(w1-1),0);
}
///////////////////////////////////////////////////////////////////////
for(i=0;i{
for(j=0;j {
varf=(float)cvGetReal2D(img,i,j);
ul=(unsigned long)varf;
printf(" %d", ul);
}
printf("\n");
}

for(i=1;i {
for(j=1;j {
var=(float)cvGetReal2D(img,i,j);
ul=(unsigned long)var;
x=ul;
if(i==2)
printf("\n i=%d j=%d",i,j);
if(x!=0)
{
var1=(float)cvGetReal2D(img,i,j-1);
var2=(float)cvGetReal2D(img,i-1,j-1);
var3=(float)cvGetReal2D(img,i-1,j);
var4=(float)cvGetReal2D(img,i-1,j+1);
int cnt=0;
if(var1!=0)
{
cnt++;
//cvSetReal2D(img,i,j,var1);
}
if(var2!=0)
{
cnt++;
//cvSetReal2D(img,i,j,var2);
}
if(var3!=0)
{
cnt++;
//cvSetReal2D(img,i,j,var3);
}
if(var4!=0)
{
cnt++;
//cvSetReal2D(img,i,j,var4);
}
if(cnt==0)
{
//central pixel non zero & neighbours zero
label++;
cvSetReal2D(img,i,j,label);
}
if(cnt==1)
{
// one neighbour is non zero
if(var1!=0)
{
if(var1!=255)
cvSetReal2D(img,i,j,var1);
else
{
//
}
}
else
{
if(var2!=0)
{
cvSetReal2D(img,i,j,var2);
}
else
{
if(var3!=0)
{
cvSetReal2D(img,i,j,var3);
}
else
{
if(var4!=0)
{
cvSetReal2D(img,i,j,var4);
}
}
}
}
}
if(cnt>=2)
{
// more than two elements are non zero fill the equivalance matrix.
unsigned long middle_pixel;
middle_pixel=-1;
if(var1!=0)
{
cvSetReal2D(img,i,j,var1);
middle_pixel=(unsigned long)var1;
}
else
{
if(var2!=0)
{
cvSetReal2D(img,i,j,var2);
middle_pixel=var2;
}
else
{
if(var3!=0)
{
cvSetReal2D(img,i,j,var3);
middle_pixel=var3;
}
else
{
if(var4!=0)
{
cvSetReal2D(img,i,j,var4);
middle_pixel=var4;
}
}
}
}

// do entry in equivalance matrix.
if(middle_pixel!=var1 && var1!=0 && var1!=255)
{
equivalance_matrix(middle_pixel,(unsigned long)var1);
}
if(middle_pixel!=var2 && var2!=0 && var2!=255)
{
equivalance_matrix(middle_pixel,(unsigned long)var2);
}
if(middle_pixel!=var3 && var3!=0 && var3!=255)
{
equivalance_matrix(middle_pixel,(unsigned long)var3);
}
if(middle_pixel!=var4 && var4!=0 && var4!=255)
{
equivalance_matrix(middle_pixel,(unsigned long)var4);
}
}


}
}
}

printf("\n\n\n\n\n");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
printf(" %d", eqMat[i][j] );
}
printf("\n");
}



int k;

for(j=0;j<50;j++)
{
for(i=0;i<50;i++)
{
if(eqMat[i][j]==1)
{
for(k=1;k<50;k++)
{
eqMat[i][k]=eqMat[i][k];
eqMat[i][k]=eqMat[j][k];
}
}
if(i==j) // set digonal to 1
eqMat[i][j]=1;
}
}







printf("\n\n\n\n\n");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
printf(" %d", eqMat[i][j] );
}
printf("\n");
}

printf("\nB4 replacement \n\n\n\n");
for(i=0;i{
for(j=0;j {
varf=(float)cvGetReal2D(img,i,j);
ul=(unsigned long)varf;
printf(" %d", ul);
}
printf("\n");
}

int rw, col;

for(i=5;i>=0;i--)
{
for(j=5;j>=0;j--)
{
if(eqMat[i][j]==1)
{
if(i {
for(rw=0;rw {
for(col=0;col {
//find j in image & replace it with i
var=(float) cvGetReal2D(img,rw,col);
ul=(unsigned long) var;
if(var==j)
{
cvSetReal2D(img,rw,col,i);
}

}
}
}
else
{
if(i>j)
{
for(rw=0;rw {
for(col=0;col {
//find i in image & replace it with j
var=(float) cvGetReal2D(img,rw,col);
ul=(unsigned long) var;
if(var==i)
{
cvSetReal2D(img,rw,col,j);
}

}
}
}
}
}
}
}

printf("\n\n\n\n\n");
for(i=0;i{
for(j=0;j {
varf=(float)cvGetReal2D(img,i,j);
ul=(unsigned long)varf;
printf(" %d", ul);
}
printf("\n");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// region props
//////////////////////////////////////////////////////////////////////////////////////////////////////


for(i=0;i{
for(j=0;j {
varf=(float)cvGetReal2D(img,i,j);
ul=(unsigned long)varf;
if(ul!=0)
{
add_to_labels(ul);
}
}
}

printf("\n\n\n\n\n");

printf("\n No of labels are =%d", cnt);

for(i=0;i{
printf(" %d",no_of_labels[i]);
}

// find out the count for each of the label...
int struct_index=0,l,m;

for(i=0;i{
for(l=1;l {
for(m=1;m {
varf=(float)cvGetReal2D(img,l,m);
ul=(unsigned long)varf;
if(ul==no_of_labels[i])
{
rp[struct_index].area ++;
rp[struct_index].value=ul;
rp[struct_index].x+= l;
rp[struct_index].y+= m;
}
}
}
struct_index ++;
}


printf("\n\n\n\n\n");

for(i=0;i{
printf("\n label is=%d, Area is=%d Centroid is (x=%f,y=%f) ",rp[i].value,rp[i].area,(rp[i].x/rp[i].area),(rp[i].y/rp[i].area));
}


}

OpenCV - Creation of AVI file

1.create a VideoWriter
CvVideoWriter*video=cvCreateAVIWriter("I:/one.avi",0,15,size,1);

2. write ur image file into that video

cvWriteToAVI(video,image);

3. Use for loop for writing multiple images.

4. Finally release the VideoWriter


cvReleaseVideoWriter( &video );

Monday, October 6, 2008

Listing the file names from a directory

Hi all..
This is the source code for listing the file names from a perticuler (specified directory..)..

#include
#include
#include
#include
#include
int _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
TCHAR szDir[MAX_PATH];
size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
// SPECIFY THE NAME OF THE DIRECTORY FROM WHERE U WANT TO TAKE THE FILES AS INPUT

szDir[0]='I';szDir[1]=':';szDir[2]='\\';szDir[3]='a';szDir[4]='m';szDir[5]='o';szDir[6]='z'; szDir[7]='\\';szDir[8]='*'; szDir[9]='\0';
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
return dwError;
}
// List all the files in the directory with some info about them.
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{

}
else
{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
if(filesize.QuadPart!=0) ////////////////////
{
_tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
dwError = GetLastError();
FindClose(hFind);
getch();
return dwError;
}

Convert a Color image to Gray using OpenCV

Hello All
This is the source code for converting a Colour Image to gray image. I had make use of OpenCV library for this purpose.

#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

IplImage* convert_grayscale(IplImage *img)
{
IplImage *gray_image,*gray_img0;
gray_image=cvCloneImage(img);
//check image is it gray or not if nor convert it to the gray
if(img->nChannels!=1)
{
//convert original image to gray_scale image
gray_img0 = cvCreateImage(cvSize( gray_image->width,gray_image->height), 8, 1 );
cvCvtColor(gray_image, gray_img0, CV_RGB2GRAY);
gray_image = cvCloneImage( gray_img0 );
}
return gray_image;
}

int _tmain(int argc, _TCHAR* argv[])
{
IplImage *Img =cvLoadImage("c:/program files/opencv/samples/c/fruits.jpg"),*gray_image;
cvNamedWindow("Image",1);
gray_image=convert_grayscale(Img);
cvShowImage("Image",gray_image);
cvWaitKey();
cvReleaseImage(&Img);
cvDestroyWindow("Image");
return 0;
}