After significant trial and error on morphological operations the following results were achieved:
The Program:
#include
#include
#include
#include
#include
IplImage *img=0; //the pointer is assigned value zero;
//thus it points to nothing untill something is loaded
IplImage *img2=0;
int main(void){
img=cvLoadImage("sg (25).jpg");
if(img==0){
fprintf(stderr,"Cannot load file.");
getchar();
return(1);
}
int i,j,height,width, step, channels;
int temp;
height=img->height;
width=img->width;
channels=img->nChannels;
step=img->widthStep;
unsigned char *data=(unsigned char *)img->imageData; //Since it's an IPL_DEPTH_8U image
for(i=0;i
for(j=0;j
temp=2*data[i*step+j*channels+2]
-data[i*step+j*channels+1]
-data[i*step+j*channels+0]; //temp=2*red - sum of blue and green
//thus the current range of temp is -510 to 510
temp=temp/4; //this reduces the range to -127 to 127
temp=temp+127; //this shifts the range to 0 to 254
data[i*step+j*channels+0]=temp;
data[i*step+j*channels+1]=temp;
data[i*step+j*channels+2]=temp;
}
}
//once the image is rendered into gray scale we don't modify img
//hence img2 - the dummy - is used
img2=cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
unsigned char *data2=(unsigned char *)img2->imageData;
cvErode(img,img2,NULL,5);
cvDilate(img2,img2,NULL,5);
cvThreshold(img2,img2,100,255,CV_THRESH_BINARY);
cvNamedWindow("image",CV_WINDOW_AUTOSIZE);
cvShowImage("image",img2);
cvSaveImage("obj.jpg",img2);
cvWaitKey(0);
cvDestroyWindow("image");
cvReleaseImage(&img);
}