You are on page 1of 13

Image Processing Lab Experiments in C

Submitted by,
Jijo Francis
M2 SP,
Ro !o "2
I) ImgAddOn.h
//Structure defned for storing Image Information
typedef struct {
int rows;int cols;int colors;int vectorSize;long fleSize;unsigned char* data;
}sImage;
//Function to get yte value of an image at a given o!set/position
long getImgInfo"FI#$* imgFile% long posn% int num&er'f(hars)
{
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
long value + -#;
int i;
//Set File pointer to the position mentined &y posn
fsee/"imgFile% posn% S$$01S$2);
for"i+3; i4+num&er'f(hars; i55)
{
fread"&u!er% sizeof"char)% 3% imgFile);
// calculate value &ased on adding &ytes
value + "long)"value 5 "*&u!er)*"pow"678% "i93))));
}
return"value);
}
//Function to get header info : rows%cols%fle size%colors and vector size
sImage get;eaderInfo"FI#$ *imgFile)
{
sImage imgInfo;
imgInfo<cols + "int)getImgInfo"imgFile% 3=% >);
imgInfo<rows + "int)getImgInfo"imgFile% 66% >);
imgInfo<fleSize + getImgInfo"imgFile% 6% >);
//imgInfo<colors + getImgInfo"imgFile% >8% >);
imgInfo<colors + 678;
imgInfo<vectorSize + imgInfo<fleSize 9 "3> 5 >- 5 >*imgInfo<colors);
return imgInfo;
}
//Function to copy header Information
void copy;eaderInfo"FI#$ *inputImgFile%FI#$ *outputImgFile)
{
int i;
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
fsee/"inputImgFile% -#% S$$01S$2);
fsee/"outputImgFile% -#% S$$01S$2);
for"i+-;i4+7?;i55)
{
fread"&u!er%sizeof"char)%3%inputImgFile);
fwrite"&u!er%sizeof"char)%3%outputImgFile);
}
}
//Function to copy (olour 2a&le
void copy(olor2a&le"FI#$ *inputImgFile%FI#$ *outputImgFile%int colors)
{
int i;
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
fsee/"inputImgFile% 7>#% S$$01S$2);
fsee/"outputImgFile% 7>#% S$$01S$2);
for"i+-;i4+>*colors;i55)
{
fread"&u!er%sizeof"char)%3%inputImgFile);
fwrite"&u!er%sizeof"char)%3%outputImgFile);
}
}
//Function to copy @aster Aata
void copy@asterAata"FI#$ *inputImgFile%FI#$ *outputImgFile%sImage inputImg)
{
int i%B;
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
fsee/"inputImgFile% 7>5>*inputImg<colors% S$$01S$2);
fsee/"outputImgFile% 7>5>*inputImg<colors% S$$01S$2);
for"i+-;i4inputImg<rows;i55)
{
for"B+-;B4inputImg<cols;B55)
{
fread"&u!er%sizeof"char)%3%inputImgFile);
fwrite"&u!er%sizeof"char)%3%outputImgFile);
}
}
}
//Function to convertCrayScale Image to lac/ and white &ased on a threshold
void gray6D"FI#$ *inputImgFile%FI#$ *outputImgFile%sImage inputImg%
int threshold#evel)
{
int i%B;
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
fsee/"inputImgFile% 7>5>*inputImg<colors% S$$01S$2);
fsee/"outputImgFile% 7>5>*inputImg<colors% S$$01S$2);
for"i+-;i4inputImg<rows;i55)
{
for"B+-;B4inputImg<cols;B55)
{
fread"&u!er%sizeof"char)%3%inputImgFile);
//Selecting an ar&itary level for thresholding
if"*&u!erE+threshold#evel)
*&u!er+677;
else
*&u!er+-;
fwrite"&u!er%sizeof"char)%3%outputImgFile);
}
}
}
I) ReadAndDisplayImg.c
Finclude4stdio<hE
Finclude GImgHdd'n<hG
void main")
{
FI#$ *inputImgFile;
FI#$ *outputImgFile;
sImage inputImg;
//'pen input and output image fle
inputImgFile+fopen"GInputImage<&mpG%Gr&G);
outputImgFile+fopen"G@eadAisplay'ut<&mpG%Gw&G);
//Cet ;eader Info from Input Image fle
inputImg+get;eaderInfo"inputImgFile);
printf"G@ows:IdJnG% inputImg<rows);
printf"G(ols:IdJnG% inputImg<cols);
printf"G(olors:IdJnG% inputImg<colors);
//(opy ;eader Info to 'utput Image
copy;eaderInfo"inputImgFile%outputImgFile);
printf"G;eader (opy (ompleted JnG);
//(opy (olor ta&le to 'utput Image
copy(olor2a&le"inputImgFile%outputImgFile%inputImg<colors);
printf"G(olor 2a&le (opy (ompleted JnG);
//(opy @aster Aata to 'utput Image
copy@asterAata"inputImgFile%outputImgFile%inputImg);
printf"G@aster Aata (opy (ompleted JnG);
printf"G$nd of Krocessing<< JnG);
//(losing input and output fles
fclose"inputImgFile);
fclose"outputImgFile);
}
Input Image:
'utput Image:
II) GrayScaleInverting.c
Finclude4stdio<hE
Finclude GImgHdd'n<hG
void main")
{
FI#$ *inputImgFile;
FI#$ *outputImgFile;
sImage inputImg;
int i%B;
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
//'pen input and output image fle
inputImgFile+fopen"GInputImage<&mpG%Gr&G);
outputImgFile+fopen"GCrayScaleInv'ut<&mpG%Gw&G);
//Cet ;eader Info from Input Image fle
inputImg+get;eaderInfo"inputImgFile);
printf"G@ows:IdJnG% inputImg<rows);
printf"G(ols:IdJnG% inputImg<cols);
//(opy ;eader Info to 'utput Image
copy;eaderInfo"inputImgFile%outputImgFile);
printf"G;eader (opy (ompleted JnG);
//(opy (olor ta&le to 'utput Image
copy(olor2a&le"inputImgFile%outputImgFile%inputImg<colors);
printf"G(olor 2a&le (opy (ompleted JnG);
fsee/"inputImgFile% 7>5>*inputImg<colors% S$$01S$2);
fsee/"outputImgFile% 7>5>*inputImg<colors% S$$01S$2);
for"i+-;i4inputImg<rows;i55)
{
for"B+-;B4inputImg<cols;B55)
{
fread"&u!er%sizeof"char)%3%inputImgFile);
*&u!er+6779*&u!er;
fwrite"&u!er%sizeof"char)%3%outputImgFile);
}
}
printf"G@aster data generation (ompleted JnG);
printf"GKrocessing (ompleted JnG);
}
Input Image:
'utput Image:
III) ThresholdingImg.c
Finclude4stdio<hE
Finclude GImgHdd'n<hG
void main")
{
FI#$ *inputImgFile;
FI#$ *outputImgFile;
sImage inputImg;
int i%B;
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
//'pen input and output image fle
inputImgFile+fopen"GInputImage<&mpG%Gr&G);
outputImgFile+fopen"G2hresholdImg'ut<&mpG%Gw&G);
//Cet ;eader Info from Input Image fle
inputImg+get;eaderInfo"inputImgFile);
printf"G@ows:IdJnG% inputImg<rows);
printf"G(ols:IdJnG% inputImg<cols);
//(opy ;eader Info to 'utput Image
copy;eaderInfo"inputImgFile%outputImgFile);
printf"G;eader (opy (ompleted JnG);
//(opy (olor ta&le to 'utput Image
copy(olor2a&le"inputImgFile%outputImgFile%inputImg<colors);
printf"G(olor 2a&le (opy (ompleted JnG);
fsee/"inputImgFile% 7>5>*inputImg<colors% S$$01S$2);
fsee/"outputImgFile% 7>5>*inputImg<colors% S$$01S$2);
for"i+-;i4inputImg<rows;i55)
{
for"B+-;B4inputImg<cols;B55)
{
fread"&u!er%sizeof"char)%3%inputImgFile);
if"*&u!erE+8- .. *&u!er4+3--)
*&u!er+*&u!er53-;
fwrite"&u!er%sizeof"char)%3%outputImgFile);
}
}
printf"G@aster data generation (ompleted JnG);
printf"GKrocessing (ompleted JnG);
}
Input Image:
'utput Image:
IV) GrayToBW.c
Finclude4stdio<hE
Finclude GImgHdd'n<hG
void main")
{
FI#$ *inputImgFile;
FI#$ *outputImgFile;
sImage inputImg;
int i%B;
unsigned char *&u!er;
unsigned char dummy*al;
dummy*al+,-,;
&u!er+.dummy*al;
//'pen input and output image fle
inputImgFile+fopen"GInputImage<&mpG%Gr&G);
outputImgFile+fopen"GCray2oD'ut<&mpG%Gw&G);
//Cet ;eader Info from Input Image fle
inputImg+get;eaderInfo"inputImgFile);
printf"G@ows:IdJnG% inputImg<rows);
printf"G(ols:IdJnG% inputImg<cols);
//(opy ;eader Info to 'utput Image
copy;eaderInfo"inputImgFile%outputImgFile);
printf"G;eader (opy (ompleted JnG);
//(opy (olor ta&le to 'utput Image
copy(olor2a&le"inputImgFile%outputImgFile%inputImg<colors);
printf"G(olor 2a&le (opy (ompleted JnG);
//convertCrayScale Image to lac/ and white &ased on a threshold
gray6D"inputImgFile%outputImgFile%inputImg%3--);
printf"G@aster data generation (ompleted JnG);
printf"GKrocessing (ompleted JnG);
}
Input Image:
'utput Image:

You might also like