SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
Assignment 5
31.Write a program to generate Fractal Generation : Koch Curve.
Code:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#define pi 3.14
int ang,tol;
void koch(int,int,int,int);
void main()
{
int gd=DETECT,gm,x1=25,y1=310,x2,y2,side;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
cout<<"Enter the cord length: ";
cin>>side;
cout<<"enter the tolerance(base criteria) and angle: ";
cin>>tol>>ang;
ang=180/ang;
x2=x1+side;
y2=y1;
koch(x1,y1,x2,y2);
getch();
closegraph();
}
void koch(int x1,int y1,int x2,int y2)
{
int x3,y3,x,y,xg,yg,xp,yp;
delay(5);
xg=x1+(x2-x1)/3;
yg=y1+(y2-y1)/3;
xp=xg+(x2-x1)/3;
yp=yg+(y2-y1)/3;
x=xp-xg;
y=yp-yg;
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
x3=xg+(x*cos(pi/ang)+y*sin(pi/ang));
y3=yg+(-x*sin(pi/ang)+y*cos(pi/ang));
if(sqrt(pow(xg-x1,2)+pow(yg-y1,2))>tol)
{
koch(x1,y1,xg,yg);
koch(xg,yg,x3,y3);
koch(x3,y3,xp,yp);
koch(xp,yp,x2,y2);
}
else
{
line(x1,y1,xg,yg);
line(xg,yg,x3,y3);
line(x3,y3,xp,yp);
line(xp,yp,x2,y2);
}
}
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
32. Write a C++ Program to implement Image Negation.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main(){
clrscr();
FILE *iptr,*optr;
unsigned char b[2];
if((iptr=fopen("c:\\turboc3\\bin\\kung.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("c:\\turboc3\\bin\\pan.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
b[0]=255-b[0];
fwrite(b,1,1,optr);
}
fclose(iptr);
fclose(optr);
getch();
}
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
33.Write a C++ Program to implement Image Addition.
Code:
#include<string.h>
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
ifstream in1,in2;
ofstream out;
char b[54];
char c[54];
char d[54];
void main()
{
clrscr();
char infile1[]="c:\\turboc3\\bin\\N.bmp";
char infile2[]="c:\\turboc3\\bin\\J.bmp";
char outfile[]="c:\\turboc3\\bin\\NJ.bmp";
in1.open(infile1,ios::in|ios::binary);
in1.read((char*)(&b),sizeof(b));
in2.open(infile2,ios::in|ios::binary);
in2.read((char*)(&c),sizeof(c));
out.open(outfile,ios::out|ios::binary);
out.write((char*)(&b),sizeof(b));
while(!in1.eof())
{
in1.read((char *)(&b),sizeof(b));
in2.read((char *)(&c),sizeof(c));
d[0]=(b[0]+c[0])%256;
out.write((char*)(&d),sizeof(d));
}
in1.close();
in2.close();
out.close();
cout<<"done";
getch();
}
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
34.Write a C++ Program to implement Image Subtraction.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main(){
clrscr();
FILE *iptr,*iptr2,*optr;
unsigned char b[2],c[2],d[2];
if((iptr=fopen("C:\\turboc3\\bin\\kung.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((iptr2=fopen("C:\\turboc3\\bin\\fu.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("C:\\turboc3\\bin\\po.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fread(c,1,1,iptr2);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
if(fread(c,1,1,iptr2)<=0)
break;
d[0]=b[0]-c[0];
fwrite(d,1,1,optr);
}
fclose(iptr);
fclose(optr);
cout<<"done";
getch();
}
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
35.Write a C++ Program to implement Thresholding. Input appropriate
threshold values.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main(){
clrscr();
FILE *iptr,*optr;
unsigned char b[2];
int th;
if((iptr=fopen("C:\\turboc3\\bin\\kung.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("C:\\turboc3\\bin\\thres.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
cout<<"Enter threshold value(0-255):";
cin>>th;
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
if(b[0]<th)
b[0]=0;
else
b[0]=255;
fwrite(b,1,1,optr);
}
fclose(iptr);
fclose(optr);
cout<<"\ndone";
getch();
}
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
36.Write a C++ Program to implement Contrast Stretching.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main(){
clrscr();
int max=0,min=255,c=0;
FILE *iptr,*optr;
unsigned char b[2];
if((iptr=fopen("C:\\turboc3\\bin\\con1.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("C:\\turboc3\\bin\\xray_con.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
if(b[0]>max)
max=b[0];
if(b[0]<min)
min=b[0];
c++;
}
fseek(iptr,56,SEEK_SET);
fseek(optr,56,SEEK_SET);
c=0;
while(1){
if(fread(b,1,1,iptr)<=0)
break;
b[0]=(b[0]-min)*255/(max-min);
fwrite(b,1,1,optr);
c++;
}
fclose(iptr);
fclose(optr);
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
cout<<"\ndone";
getch();
}
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
37.Write a C++ Program to implement Log and Inverse Log
transformation.
Code:
Log:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void main(){
clrscr();
double c;
FILE *iptr,*optr;
unsigned char b[2];
if((iptr=fopen("C:\\turboc3\\bin\\kung.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("C:\\turboc3\\bin\\bu.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
cout<<"Enter the constant value: ";
cin>>c;
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
b[0]=c*log(1+(double)b[0]);
fwrite(b,1,1,optr);
}
fclose(iptr);
fclose(optr);
getch();
}
Inverse log:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void main(){
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
clrscr();
double c;
FILE *iptr,*optr;
unsigned char b[2];
if((iptr=fopen("C:\\turboc3\\bin\\ kung.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("C:\\turboc3\\bin\\nu.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
cout<<"Enter the constant value: ";
cin>>c;
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
b[0]=pow(exp((double)b[0]),1/c)-1;
fwrite(b,1,1,optr);
}
fclose(iptr);
fclose(optr);
getch();
}
Output:
Log
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
Inverse log:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
38.Write a C++ Program to implement Power-Law transformations.
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void main(){
clrscr();
double g;
FILE *iptr,*optr;
unsigned char b[2];
if((iptr=fopen("C:\\turboc3\\bin\\scn.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("C:\\turboc3\\bin\\kin.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
cout<<"Enter the gamma value: ";
cin>>g;
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
b[0]=255*pow(((double)b[0]/255),g);
fwrite(b,1,1,optr);
}
fclose(iptr);
fclose(optr);
getch();
}
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
39.Write a C++ Program to implement Grey-level Slicing.
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void main(){
clrscr();
int c,d,choice;
FILE *iptr,*optr;
unsigned char b[2];
if((iptr=fopen("C:\\TURBOC3\\BIN\\kung.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
if((optr=fopen("C:\\TURBOC3\\BIN\\ni.bmp","wb+"))==NULL)
fprintf(stderr,"Can't open output file.\t");
cout<<"Enter the gray level 'a' corresponding to the lower threshold point:\n";
cin>>c;
cout<<"Enter the gray level 'b' corresponding to the lower threshold point:\n";
cin>>d;
cout<<"\n do you want to preserve the backgrounf?\n 1.Yes \n 2.No \n";
cin>>choice;
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
fwrite(b,1,1,optr);
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
if(b[0]>=c && b[0]<=d)
{
if(choice==2)
b[0]=0;
}
else
b[0]=255;
fwrite(b,1,1,optr);
}
fclose(iptr);
fclose(optr);
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
cout<<"Done";
getch();
}
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
40.Write a C++ Program to plot a Histogram for a given image.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main(){
clrscr();
FILE *iptr,*optr;
int freq[256];
int gd=DETECT,gm,max=0;
unsigned char b[2];
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
if((iptr=fopen("C:\\turboc3\\bin\\scn.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
}
for(i=0; i<256; i++)
freq[i]=0;
while(1){
if(fread(b,1,1,iptr)<=0)
break;
freq[b[0]]++;
}
for(i=0; i<256; i++)
if(freq[i]>max)
max=freq[i];
for(i=0; i<256; i++)
freq[i]=((float)freq[i]/max)*479;
for(i=0; i<256; i++)
line(i*2, 479, i*2, 479-freq[i]);
getch();
}
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
41.Write a C++ Program to equalise the histogram of a given image and
plot the original and equalised histogram.
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
int round(float val){
int incr=1,sc=1;
if(val<0){
incr=-1;sc=-1;
}
if((val*sc)-((int)(val*sc))>=0.5)
return ((int)val+incr);
else return ((int)val);
}
void main(){
clrscr();
FILE *iptr,*optr;
double freq[256], equi[256];
float cumlative=0;
double total=0,max=0,max2=0,sum=0;
int gd=DETECT,gm;
unsigned char b[2];
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
if((iptr=fopen("C:\\turboc3\\bin\\scn.bmp","rb"))==NULL)
fprintf(stderr,"Can't open input file \n");
for(int i=0;i<=55;i++){
fread(b,1,1,iptr);
}
for(i=0; i<256; i++){
freq[i]=0;
equi[i]=0;
}
while(1){
if(fread(b,1,1,iptr)<=0)
break;
freq[b[0]]++;
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
sum++;
}
for(i=0; i<256; i++){
cumlative+=(freq[i]/sum);
equi[round(256*cumlative)]+=freq[i];
if(freq[i]>max)
max=freq[i];
}
for(i=0; i<256; i++)
if(freq[i]>max2)
max2=equi[i];
for(i=0; i<256; i++)
freq[i]=((float)freq[i]/max)*479;
cout<<"Histogram:";
for(i=0; i<256; i++)
line(i*2, 479, i*2, 479-freq[i]);
getch();
cleardevice();
cout<<"Equilized:";
for(i=0; i<256; i++)
equi[i]=((float)equi[i]/max2)*479;
for(i=0; i<256; i++)
line(i*2, 479, i*2, 479-equi[i]);
getch();
}
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
42.Write a C++ Program to apply a Low pass filter to an image. Use
appropriate masks.
Code:
#include <string.h>
#include <stdio.h>
#include<iostream.h>
#include <conio.h>
FILE *stream,*cptr;
int width,height;
unsigned char b[2];
int value(int row,int col)
{
// 54th byte onwards
int s;
double r=(double)row*((width)*3)+54+(col*3);
fseek(stream,0,SEEK_SET);
while(r>=32760)
{
fseek(stream,32760,SEEK_CUR);
r=r-32760;
}
fseek(stream,(int)r,SEEK_CUR);
fread(b,1, 1, stream);
fread(b,1, 1, stream);
fread(b,1, 1, stream);
return b[0];
}
int main(void)
{
int dimension;
int i,j,k,l,prod;
int dim;
float temp;
clrscr();
if ((stream = fopen("C:\\turboc3\\bin\\lalu.bmp", "rb+"))== NULL)
{
fprintf(stderr, "Cannot open input file.\n");
return 1;
}
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
if ((cptr = fopen("C:\\turboc3\\bin\\min.bmp", "wb+"))== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
//copy the header to the output file as it is.
for(i=0;i<=53;i++)
{
fread(b,1, 1, stream);
fwrite(b,1,1,cptr);
}
//to get the width of the image
fseek(stream,18,SEEK_SET);
width=0;
for(j=0,prod=1;j<3;j++)
{
fread(b,1, 1, stream);
width=width+b[0]*prod;
prod=prod*255;
}
//to get the height of the image
fseek(stream,22,SEEK_SET);
height=0;
for(j=0,prod=1;j<3;j++)
{
fread(b,1, 1, stream);
height=height+b[0]*prod;
prod=prod*255;
}
cout<<"Enter the dimension of the low pass filter mask to be applied"<<endl;
cout<<"Note the dimension should be an odd number ";
cin>>dimension;
dim=dimension;
dimension/=2;
//height contains the height of the image
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
temp=0;
for(k=i-dimension;k<=i+dimension;k++)
{
for(l=j-dimension;l<=j+dimension;l++)
{
if(k>=0&&l>=0&&k<height&&l<width)
temp+=value(k,l);
}
}
b[0]=temp/(dim*dim);
fwrite(b,1,1,cptr);
fwrite(b,1,1,cptr);
fwrite(b,1,1,cptr);
}
}
cout<<"\n done";
fclose(stream);
fclose(cptr);
return 0;
}
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
43.Write a C++ Program to apply a High pass filter to an image. Use
appropriate masks.
Code:
#include<iostream.h>
#include <string.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
FILE *stream,*cptr;
int width,height;
unsigned char b[2];
int value(int row,int col)
{
// 54th byte onwards
int s;
double r=(double)row*((width)*3)+54+(col*3);
fseek(stream,0,SEEK_SET);
while(r>=32760)
{
fseek(stream,32760,SEEK_CUR);
r=r-32760;
}
fseek(stream,(int)r,SEEK_CUR);
fread(b,1, 1, stream);
fread(b,1, 1, stream);
fread(b,1, 1, stream);
return b[0];
}
int main(void)
{
int dimension;
int i,j,k,l,prod,temp1;
int dim;
float temp;
clrscr();
if ((stream = fopen("C:\\turboc3\\bin\\lalu.bmp", "rb+"))== NULL)
{
fprintf(stderr, "Cannot open input file.\n");
return 1;
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
}
if ((cptr = fopen("C:\\turboc3\\bin\\img4.bmp", "wb+"))== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
//copy the header to the output file as it is.
for(i=0;i<=53;i++)
{
fread(b,1, 1, stream);
fwrite(b,1,1,cptr);
}
//to get the width of the image
fseek(stream,18,SEEK_SET);
width=0;
for(j=0,prod=1;j<3;j++)
{
fread(b,1, 1, stream);
width=width+b[0]*prod;
prod=prod*255;
}
//to get the height of the image
fseek(stream,22,SEEK_SET);
height=0;
for(j=0,prod=1;j<3;j++)
{
fread(b,1, 1, stream);
height=height+b[0]*prod;
prod=prod*255;
}
cout<<"Enter the dimension of the high pass filter mask to be applied"<<endl;
cout<<"Note the dimension should be an odd number"<<endl;
cin>>dimension;
dim=dimension;
dimension/=2;
//height contains the height of the image
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23
{
temp=0;
temp1=value(i,j);
for(k=i-dimension;k<=i+dimension;k++)
{
for(l=j-dimension;l<=j+dimension;l++)
{
if(k>=0&&l>=0&&k<height&&l<width)
temp+=value(k,l);
}
}
b[0]=temp/(dim*dim);
b[0]=abs(temp1-b[0]);
fwrite(b,1,1,cptr);
fwrite(b,1,1,cptr);
fwrite(b,1,1,cptr);
}
}
cout<<"\n done";
fclose(stream);
fclose(cptr);
return 0;
}
Output:
SIES College of Management Studies SYMCA, Sem IV, Roll No : 23