0% found this document useful (0 votes)
31 views10 pages

Practical 13 (A) : Piecewise Linear Transformation Function Contrast Stretching Program Code

1) The document describes several C++ programs that perform piecewise linear transformations on images, including contrast stretching, thresholding, grey level slicing with and without background, and bit plane slicing. 2) The programs take an input image, apply the transformation, and output the result to a new image file while writing debugging information to a text file. 3) They prompt the user to enter threshold values, coordinate points, or other parameters needed for the specific transformation being tested.

Uploaded by

кʜaɴ S aʟaм
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Practical 13 (A) : Piecewise Linear Transformation Function Contrast Stretching Program Code

1) The document describes several C++ programs that perform piecewise linear transformations on images, including contrast stretching, thresholding, grey level slicing with and without background, and bit plane slicing. 2) The programs take an input image, apply the transformation, and output the result to a new image file while writing debugging information to a text file. 3) They prompt the user to enter threshold values, coordinate points, or other parameters needed for the specific transformation being tested.

Uploaded by

кʜaɴ S aʟaм
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Div.:-B Roll No.

:-31

Practical 13(A)
Aim: Piecewise Linear Transformation Function – Contrast Stretching.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<string.h>
#include<math.h>
struct pix
{unsigned char b,g,r;
}pixel;
int L=255;
char Header[54]; ifstream in; ofstream out,out1;
void main()
{int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");
char infile[]="c:\\tc\\bin\\image\\butter.bmp";
char outfile[]="c:\\tc\\bin\\image\\con_butter.bmp"; char
imdata[]="c:\\tc\\bin\\image\\imdata.bmp"; in.open(infile,ios::in|ios::binary);
in.read((char*)(&Header),sizeof(Header)); out.open(outfile,ios::out|ios::binary);
out.write((char*)(&Header),sizeof(Header)); out1.write(imdata,ios::out);
int r1,r2,s1,s2;
cout<<"\n Enter coordinates of (r1 s1)"; cin>>r1>>s1;
cout<<"\n enter coordinates of (r2 s2)"; cin>>r2>>s2;
int v,w,a,b,l,m,n; v=s1;
w=s2;
a=r1; b=r2;
l=(float)(s1/r1); m=(float)(s2-s1)/(r2-r1);
n=(float)((255-s2)/(255-r2)); while(!in.eof())
{
Div.:-B Roll No.:-31

in.read((char*)(&pixel),sizeof(pixel)); out1<<"original"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pi
xel.b<<endl;
if(pixel.r<a && pixel.g<a && pixel.b<a)
{
pixel.r=1*pixel.r; pixel.g=1*pixel.g; pixel.b=1*pixel.b;
}
if(pixel.r<b && pixel.g<b && pixel.b<b)
{
pixel.r=m*(pixel.r-a)+v; pixel.g=m*(pixel.g-a)+v; pixel.b=m*(pixel.b-a)+v;
}
else
{
pixel.r=n*(pixel.r-b)+w; pixel.g=n*(pixel.g-b)+w; pixel.b=n*(pixel.b-b)+w;
}

out.write((char*)(&pixel),sizeof(pixel)); out1<<"mod"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pixel
.b<<endl;
}
getch();
}
Output:
Div.:-B Roll No.:-31

Practical 13(B)
Aim: Piecewise Linear Transformation Function – Threshold function.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<string.h>
#include<math.h>
struct pix
{
unsigned char b,g,r;
}pixel;
int L=255;
char Header[54]; ifstream in; ofstream out,out1; void main()
{
int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");
char infile[]="c:\\tc\\bin\\image\\butter.bmp";
char outfile[]="c:\\tc\\bin\\image\\thersh_butter.bmp"; char
imdata[]="c:\\tc\\bin\\image\\imdata.bmp"; in.open(infile,ios::in|ios::binary);
in.read((char*)(&Header),sizeof(Header)); out.open(outfile,ios::out|ios::binary);
out.write((char*)(&Header),sizeof(Header)); out1.write(imdata,ios::out);
int thresh;
cout<<"\n Enter the threshold value"; cin>>thresh;
while(!in.eof())
{
in.read((char*)(&pixel),sizeof(pixel)); out1<<"original"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pi
xel.b<<endl;
if((pixel.r<thresh&& pixel.g<thresh&& pixel.b<thresh)) pixel.r=pixel.g=pixel.b=0; //black

else
Div.:-B Roll No.:-31

pixel.r=pixel.g=pixel.b=255; //white out.write((char*)(&pixel),sizeof(pixel));


out1<<"mod"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pixel
.b<<endl;
}
in.close();
out.close();
getch();
}
Output:
Div.:-B Roll No.:-31

Practical 13(C)
Aim: Piecewise Linear Transformation Function – Grey level slicing with background.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<string.h>
#include<math.h>
struct pix
{unsigned char b,g,r;
}pixel;
int L=255;
char Header[54]; ifstream in; ofstream out,out1;
void main()
{int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");
char infile[]="c:\\tc\\bin\\image\\butter.bmp";
char outfile[]="c:\\tc\\bin\\image\\greyssb_butter.bmp"; char
imdata[]="c:\\tc\\bin\\image\\imdata.bmp"; in.open(infile,ios::in|ios::binary);
in.read((char*)(&Header),sizeof(Header)); out.open(outfile,ios::out|ios::binary);
out.write((char*)(&Header),sizeof(Header)); out1.write(imdata,ios::out);
int a,b;
cout<<"Enter the values of a and b"; cin>>a>>b;
while(!in.eof())
{
in.read((char*)(&pixel),sizeof(pixel)); out1<<"original"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pi
xel.b<<endl;
if((pixel.r<a&& pixel.g<a&& pixel.b<a)&&(pixel.r<b&& pixel.g<b&& pixel.b<a))

pixel.r=pixel.g=pixel.b=255; else
{
Div.:-B Roll No.:-31

pixel.r=pixel.r; pixel.b=pixel.b; pixel.g=pixel.g;


}
out.write((char*)(&pixel),sizeof(pixel)); out1<<"mod"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pixel
.b<<endl;
}
in.close();
out.close();
getch();
}

Output:
Div.:-B Roll No.:-31

Practical 13(D)
Aim: Piecewise Linear Transformation Function – Grey level slicing without background.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<string.h>
#include<math.h>
struct pix
{unsigned char b,g,r;
}pixel;
int L=255;
char Header[54]; ifstream in; ofstream out,out1;
void main()
{int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");
char infile[]="c:\\tc\\bin\\image\\butter.bmp";
char outfile[]="c:\\tc\\bin\\image\\greyss_butter.bmp"; char
imdata[]="c:\\tc\\bin\\image\\imdata.bmp"; in.open(infile,ios::in|ios::binary);
in.read((char*)(&Header),sizeof(Header)); out.open(outfile,ios::out|ios::binary);
out.write((char*)(&Header),sizeof(Header)); out1.write(imdata,ios::out);
int a,b;
cout<<"Enter the values of a and b"; cin>>a>>b;
while(!in.eof())
{
in.read((char*)(&pixel),sizeof(pixel)); out1<<"original"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pi
xel.b<<endl;
if((pixel.r<a&& pixel.g<a&& pixel.b<a)&&(pixel.r<b&& pixel.g<b&& pixel.b<a))

pixel.r=pixel.g=pixel.b=255; else pixel.r=pixel.g=pixel.b=0;


out.write((char*)(&pixel),sizeof(pixel)); out1<<"mod"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pixel
Div.:-B Roll No.:-31

.b<<endl;
}
in.close();
out.close();
getch();
}

Output:
Div.:-B Roll No.:-31

Practical 13(E)
Aim: Piecewise Linear Transformation Function – Bit plane slicing.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<string.h>
#include<math.h>
struct pix
{unsigned char b,g,r;
}pixel;
int L=255;
char Header[54]; ifstream in; ofstream out,out1;
void main()
{
int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");
char infile[]="c:\\tc\\bin\\image\\butter.bmp";
char outfile[]="c:\\tc\\bin\\image\\bitplane_butter.bmp"; char
imdata[]="c:\\tc\\bin\\image\\imdata.bmp"; in.open(infile,ios::in|ios::binary);
in.read((char*)(&Header),sizeof(Header)); out.open(outfile,ios::out|ios::binary);
out.write((char*)(&Header),sizeof(Header)); out1.write(imdata,ios::out);
int bi; do{
cout<<"which bit image you want to extract 0=LSB and 7=MSB"; cin>>bi;
}while(bi<0||bi>7); while(!in.eof()){
in.read((char*)(&pixel),sizeof(pixel)); out1<<"original"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pi
xel.b<<endl;
pixel.r=pixel.r & (int)pow(2.0,bi);

pixel.b=pixel.b & (int)pow(2.0,bi); pixel.g=pixel.g & (int)pow(2.0,bi);


out.write((char*)(&pixel),sizeof(pixel));
Div.:-B Roll No.:-31

out1<<"mod"<<(int)pixel.r<<","<<(int)pixel.g<<","<<(int)pixel
.b<<endl;} in.close();
out.close();
getch();
}

Output:

You might also like