0% found this document useful (0 votes)
34 views20 pages

Assignment 2dksjfhkjdshfadfjkhdksjafhkjsdh

hksdbfcmbsxmvbmnxvcbxcmbvkjsfdfsjfhvksdjbvk cskjzgvkhsdiv czxbv kcxz vjhvksd v vkzcgvmckvbkjsvhc kxjzhiufyuepqoifihbascbx iu c dsakjfhkajdf hfdajkhfkajdfhkjfda fkadhfkjhkajfdhfkjda

Uploaded by

Sandeep Angara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views20 pages

Assignment 2dksjfhkjdshfadfjkhdksjafhkjsdh

hksdbfcmbsxmvbmnxvcbxcmbvkjsfdfsjfhvksdjbvk cskjzgvkhsdiv czxbv kcxz vjhvksd v vkzcgvmckvbkjsvhc kxjzhiufyuepqoifihbascbx iu c dsakjfhkajdf hfdajkhfkajdfhkjfda fkadhfkjhkajfdhfkjda

Uploaded by

Sandeep Angara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Name: Naga Surya Sandeep Angara

Course: Computer Vision


Assignment-2
Date: 10/14/2014




































A) Edge detection using Roberts Mask:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>

using namespace std;

int main(){

// varible initialization
int i, j, M,N, Q,iLow,iHigh;
unsigned char *charImage,* OutImage;
char header [100], *ptr;
ifstream ifp;
ofstream ofp;
bool type;
long int hist[256]={0},out[256]={0};
int val=0,val1=0,val2=0,val3=0,val4=0,val5=0,val6=0,val7=0,val8=0,val9=0,valx,valy,valn;
long int filter1[3][3] = {{0,0,0},
{0,1,0},
{0,0,-1}};
long int filter2[3][3] = {{0,0,0},
{0,+1,0},
{-1,0,0}};

// opening an input image
ifp.open("LENA.pgm", ios::in | ios::binary);
if (!ifp){
cout << "Can't read image: " << endl;
exit(1);
}

// read header
ifp.getline(header,100,'\n');
if ( (header[0] != 80) && (header[1] != 53) ){
cout << "Image " << ifp << " is not PGM or PPM" << endl;
exit(1);
}

ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');

M=strtol(header,&ptr,0);
N=atoi(ptr);

ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);

// printing the details of image
cout<<"\nNumber of rows:"<<M<<"\nNumber of columns:"<<N<<"\nMaximum pixel
value:"<<Q<<endl;
cout<<"\nReading pixel values";

// creating dynamic memory for the input image and output image
charImage = (unsigned char *) new unsigned char [M*N];
OutImage = (unsigned char *) new unsigned char [M*N];

// changing the 2-d values into vector format
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ifp.fail())
{
cout << "Image " << ifp << " has wrong size" << endl;
exit(1);
}

for(i=1;i<M;i++)
{
for(j=1;j<N;j++)
{
//calculating the pixels of output image
val1=(long int) charImage [(i-1)*M+(j-1)]; //0,1
val2=(long int) charImage [i*M+(j-1)]; //1,0
val3=(long int) charImage [(i+1)*M+(j-1)]; //2,1
val4=(long int) charImage [(i-1)*M+j]; //1,2
val5=(long int) charImage [(i)*M+j]; //0,1
val6=(long int) charImage [(i+1)*M+j]; //1,0
val7=(long int) charImage [(i-1)*M+(j+1)]; //2,1
val8=(long int) charImage [i*M+(j+1)]; //1,2
val9=(long int) charImage [(i+1)*M+(j+1)];

valx=fabs(val1*filter1[0][0]+val2*filter1[1][0]+val3*filter1[2][0]+val4*filter1[0][1]+val5*filter1[1][1]+
val6*filter1[2][1]+val7*filter1[0][2]+val8*filter1[1][2]+val9*filter1[2][2]);

valy=fabs(val1*filter2[0][0]+val2*filter2[1][0]+val3*filter2[2][0]+val4*filter2[0][1]+val5*filter2[1][1]+
val6*filter2[2][1]+val7*filter2[0][2]+val8*filter2[1][2]+val9*filter2[2][2]);

if(valx>valy)
val=valx;
else
val=valy;
OutImage[i*M+j]=(char) val;
}
}

ofp.open("roberts.pgm", ios::out | ios::binary);

if (!ofp){
cout << "Can't open file: " << ofp << endl;
exit(1);
}

// writing the header information into output file
ofp << "P5"<<"\r\n" ;
ofp << M << " " << N << endl<<"\r\n";
ofp << Q << "\r\n";
ofp.write( reinterpret_cast<char *>(OutImage), (M*N)*sizeof(unsigned char));

if (ofp.fail()){
cout << "Can't write image " << ofp << endl;
exit(0);
}

ofp.close();

// deleting the static memory created
delete [] charImage;
delete [] OutImage;

return (0);
ifp.close();
}

Console Output:


Number of rows:512
Number of columns:512
Maximum pixel value:255

Reading pixel values
--------------------------------
Process exited after 0.1429 seconds with return value 0
Press any key to continue . . .







Output:

Input Image Output Image



Output Image after thresholding with threshold value 15






B) Edge Detection using gradient Mask:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>

using namespace std;

int main(){

// varible initialization
int i, j, M,N, Q,iLow,iHigh;
unsigned char *charImage,* OutImage;
char header [100], *ptr;
ifstream ifp;
ofstream ofp;
bool type;
long int hist[256]={0},out[256]={0};
int val1=0,val2=0,val3=0,valx,valy,val;
int filter[3][3] = {0,-1, 0, -1, 4, -1, 0, -1, 0};

// opening an input image
ifp.open("LENA.pgm", ios::in | ios::binary);
if (!ifp){
cout << "Can't read image: " << endl;
exit(1);
}

// read header
ifp.getline(header,100,'\n');
if ( (header[0] != 80) && (header[1] != 53) ){
cout << "Image " << ifp << " is not PGM or PPM" << endl;
exit(1);
}

ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');

M=strtol(header,&ptr,0);
N=atoi(ptr);

ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);

// printing the details of image
cout<<"\nNumber of rows:"<<M<<"\nNumber of columns:"<<N<<"\nMaximum pixel
value:"<<Q<<endl;
cout<<"\nReading pixel values";

// creating dynamic memory for the input image and output image
charImage = (unsigned char *) new unsigned char [M*N];
OutImage = (unsigned char *) new unsigned char [M*N];

// changing the 2-d values into vector format
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ifp.fail())
{
cout << "Image " << ifp << " has wrong size" << endl;
exit(1);
}

for(i=1;i<M;i++)
{
for(j=1;j<N;j++)
{
//calculating the pixels of output image
val1=(long int) charImage [(i-1)*M+j]; //0,1
val2=(long int) charImage [i*M+(j-1)]; //1,0
val3=(long int) charImage [i*M+j]; //1,1
valx=fabs(val3-val2);
valy=fabs(val3-val1);
if (valx>valy)
val=valx;
else
val=valy;
OutImage[i*M+j]=(char) val;
}
}

ofp.open("gradient.pgm", ios::out | ios::binary);

if (!ofp){
cout << "Can't open file: " << ofp << endl;
exit(1);
}

// writing the header information into output file
ofp << "P5"<<"\r\n" ;
ofp << M << " " << N << endl<<"\r\n";
ofp << Q << "\r\n";
ofp.write( reinterpret_cast<char *>(OutImage), (M*N)*sizeof(unsigned char));

if (ofp.fail()){
cout << "Can't write image " << ofp << endl;
exit(0);
}

ofp.close();

// deleting the static memory created
delete [] charImage;
delete [] OutImage;

return (0);
ifp.close();
}

Console Output:

Number of rows:512
Number of columns:512
Maximum pixel value:255

Reading pixel values
--------------------------------
Process exited after 0.04082 seconds with return value 0
Press any key to continue . . .

Output:

Input Image Output Image


Output Image after thresholding with threshold value 15

C) Edge Detection using Laplacian mask
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>

using namespace std;

int main(){

// varible initialization
int i, j, M,N, Q,iLow,iHigh;
unsigned char *charImage,* OutImage;
char header [100], *ptr;
ifstream ifp;
ofstream ofp;
bool type;
long int hist[256]={0},out[256]={0};
int val1=0,val2=0,val3=0,val4=0,val5=0,val6=0;
int filter[3][3] = {0,-1, 0, -1, 4, -1, 0, -1, 0};

// opening an input image
ifp.open("LENA.pgm", ios::in | ios::binary);
if (!ifp){
cout << "Can't read image: " << endl;
exit(1);
}

// read header
ifp.getline(header,100,'\n');
if ( (header[0] != 80) && (header[1] != 53) ){
cout << "Image " << ifp << " is not PGM or PPM" << endl;
exit(1);
}

ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');

M=strtol(header,&ptr,0);
N=atoi(ptr);

ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);

// printing the details of image
cout<<"\nNumber of rows:"<<M<<"\nNumber of columns:"<<N<<"\nMaximum pixel
value:"<<Q<<endl;
cout<<"\nReading pixel values";

// creating dynamic memory for the input image and output image
charImage = (unsigned char *) new unsigned char [M*N];
OutImage = (unsigned char *) new unsigned char [M*N];

// changing the 2-d values into vector format
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ifp.fail())
{
cout << "Image " << ifp << " has wrong size" << endl;
exit(1);
}

for(i=1;i<M;i++)
{
for(j=1;j<N;j++)
{
//calculating the pixels of output image
val1=(long int) charImage [(i-1)*M+j]; //0,1
val2=(long int) charImage [i*M+(j-1)]; //1,0
val3=(long int) charImage [(i+1)*M+j]; //2,1
val4=(long int) charImage [i*M+(j+1)]; //1,2
val5=(long int) charImage [i*M+j];
val6=fabs((val1+val2+val3+val4)-(4*val5));
OutImage[i*M+j]=(char) val6;
}
}

ofp.open("laplacian.pgm", ios::out | ios::binary);

if (!ofp){
cout << "Can't open file: " << ofp << endl;
exit(1);
}

// writing the header information into output file
ofp << "P5"<<"\r\n" ;
ofp << M << " " << N << endl<<"\r\n";
ofp << Q << "\r\n";
ofp.write( reinterpret_cast<char *>(OutImage), (M*N)*sizeof(unsigned char));

if (ofp.fail()){
cout << "Can't write image " << ofp << endl;
exit(0);
}

ofp.close();

// deleting the static memory created
delete [] charImage;
delete [] OutImage;

return (0);
ifp.close();
}

Console Output:

Number of rows:512
Number of columns:512
Maximum pixel value:255

Reading pixel values
--------------------------------
Process exited after 0.03423 seconds with return value 0
Press any key to continue . . .





Output:

Input Image Output Image




Output Image with threshold value 15






D) Edge Detection using Prewitt Mask:
Source Code:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>

using namespace std;

int main(){

// varible initialization
int i, j, M,N, Q,iLow,iHigh;
unsigned char *charImage,* OutImage;
char header [100], *ptr;
ifstream ifp;
ofstream ofp;
bool type;
long int hist[256]={0},out[256]={0};
int val=0,val1=0,val2=0,val3=0,val4=0,val5=0,val6=0,val7=0,val8=0,val9=0,valx,valy,valn;
long int filter1[3][3] = {{-1,0,1},
{-1,0,1},
{-1,0,1}};
long int filter2[3][3] = {{-1,-1,-1},
{0,0,0},
{1,1,1}};

// opening an input image
ifp.open("LENA.pgm", ios::in | ios::binary);
if (!ifp){
cout << "Can't read image: " << endl;
exit(1);
}

// read header
ifp.getline(header,100,'\n');
if ( (header[0] != 80) && (header[1] != 53) ){
cout << "Image " << ifp << " is not PGM or PPM" << endl;
exit(1);
}

ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');

M=strtol(header,&ptr,0);
N=atoi(ptr);

ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);

// printing the details of image
cout<<"\nNumber of rows:"<<M<<"\nNumber of columns:"<<N<<"\nMaximum pixel
value:"<<Q<<endl;
cout<<"\nReading pixel values";

// creating dynamic memory for the input image and output image
charImage = (unsigned char *) new unsigned char [M*N];
OutImage = (unsigned char *) new unsigned char [M*N];

// changing the 2-d values into vector format
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ifp.fail())
{
cout << "Image " << ifp << " has wrong size" << endl;
exit(1);
}

for(i=1;i<M;i++)
{
for(j=1;j<N;j++)
{
//calculating the pixels of output image
val1=(long int) charImage [(i-1)*M+(j-1)]; //0,1
val2=(long int) charImage [i*M+(j-1)]; //1,0
val3=(long int) charImage [(i+1)*M+(j-1)]; //2,1
val4=(long int) charImage [(i-1)*M+j]; //1,2
val5=(long int) charImage [(i)*M+j]; //0,1
val6=(long int) charImage [(i+1)*M+j]; //1,0
val7=(long int) charImage [(i-1)*M+(j+1)]; //2,1
val8=(long int) charImage [i*M+(j+1)]; //1,2
val9=(long int) charImage [(i+1)*M+(j+1)];

valx=fabs(val1*filter1[0][0]+val2*filter1[1][0]+val3*filter1[2][0]+val4*filter1[0][1]+val5*filter1[1][1]+
val6*filter1[2][1]+val7*filter1[0][2]+val8*filter1[1][2]+val9*filter1[2][2]);

valy=fabs(val1*filter2[0][0]+val2*filter2[1][0]+val3*filter2[2][0]+val4*filter2[0][1]+val5*filter2[1][1]+
val6*filter2[2][1]+val7*filter2[0][2]+val8*filter2[1][2]+val9*filter2[2][2]);
//val=sqrt(valx*valx+valy*valy);
if(valx>valy)
val=valx;
else
val=valy;

OutImage[i*M+j]=(char) val;
}
}

ofp.open("prewitts.pgm", ios::out | ios::binary);

if (!ofp){
cout << "Can't open file: " << ofp << endl;
exit(1);
}

// writing the header information into output file
ofp << "P5"<<"\r\n" ;
ofp << M << " " << N << endl<<"\r\n";
ofp << Q << "\r\n";
ofp.write( reinterpret_cast<char *>(OutImage), (M*N)*sizeof(unsigned char));

if (ofp.fail()){
cout << "Can't write image " << ofp << endl;
exit(0);
}

ofp.close();

// deleting the static memory created
delete [] charImage;
delete [] OutImage;

return (0);
ifp.close();
}

Console Output:

Number of rows:512
Number of columns:512
Maximum pixel value:255

Reading pixel values
--------------------------------
Process exited after 0.03878 seconds with return value 0
Press any key to continue . . .






Outputs:

Input Image Output Image

Output Image with threshold value 15








E) Edge detection using Sobel Masks
Source Code:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>

using namespace std;

int main(){

// varible initialization
int i, j, M,N, Q,iLow,iHigh;
unsigned char *charImage,* OutImage;
char header [100], *ptr;
ifstream ifp;
ofstream ofp;
bool type;
long int hist[256]={0},out[256]={0};
int val=0,val1=0,val2=0,val3=0,val4=0,val5=0,val6=0,val7=0,val8=0,val9=0,valx,valy,valn;
long int filter1[3][3] = {{-1,0,1},
{-2,0,2},
{-1,0,1}};
long int filter2[3][3] = {{-1,-2,-1},
{0,0,0},
{1,2,1}};

// opening an input image
ifp.open("LENA.pgm", ios::in | ios::binary);
if (!ifp){
cout << "Can't read image: " << endl;
exit(1);
}

// read header
ifp.getline(header,100,'\n');
if ( (header[0] != 80) && (header[1] != 53) ){
cout << "Image " << ifp << " is not PGM or PPM" << endl;
exit(1);
}

ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');

M=strtol(header,&ptr,0);
N=atoi(ptr);

ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);

// printing the details of image
cout<<"\nNumber of rows:"<<M<<"\nNumber of columns:"<<N<<"\nMaximum pixel value:"<<Q<<endl;
cout<<"\nReading pixel values";

// creating dynamic memory for the input image and output image
charImage = (unsigned char *) new unsigned char [M*N];
OutImage = (unsigned char *) new unsigned char [M*N];

// changing the 2-d values into vector format
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ifp.fail())
{
cout << "Image " << ifp << " has wrong size" << endl;
exit(1);
}

for(i=1;i<M;i++)
{
for(j=1;j<N;j++)
{
//calculating the pixels of output image
val1=(long int) charImage [(i-1)*M+(j-1)]; //0,1
val2=(long int) charImage [i*M+(j-1)]; //1,0
val3=(long int) charImage [(i+1)*M+(j-1)]; //2,1
val4=(long int) charImage [(i-1)*M+j]; //1,2
val5=(long int) charImage [(i)*M+j]; //0,1
val6=(long int) charImage [(i+1)*M+j]; //1,0
val7=(long int) charImage [(i-1)*M+(j+1)]; //2,1
val8=(long int) charImage [i*M+(j+1)]; //1,2
val9=(long int) charImage [(i+1)*M+(j+1)];

valx=fabs(val1*filter1[0][0]+val2*filter1[1][0]+val3*filter1[2][0]+val4*filter1[0][1]+val5*filter1[1][1]+val6
*filter1[2][1]+val7*filter1[0][2]+val8*filter1[1][2]+val9*filter1[2][2]);

valy=fabs(val1*filter2[0][0]+val2*filter2[1][0]+val3*filter2[2][0]+val4*filter2[0][1]+val5*filter2[1][1]+val
6*filter2[2][1]+val7*filter2[0][2]+val8*filter2[1][2]+val9*filter2[2][2]);
//val=sqrt(valx*valx+valy*valy);
if(valx>valy)
val=valx;
else
val=valy;

OutImage[i*M+j]=(char) val;
}
}

ofp.open("sobel.pgm", ios::out | ios::binary);

if (!ofp){
cout << "Can't open file: " << ofp << endl;
exit(1);
}

// writing the header information into output file
ofp << "P5"<<"\r\n" ;
ofp << M << " " << N << endl<<"\r\n";
ofp << Q << "\r\n";
ofp.write( reinterpret_cast<char *>(OutImage), (M*N)*sizeof(unsigned char));

if (ofp.fail()){
cout << "Can't write image " << ofp << endl;
exit(0);
}

ofp.close();

// deleting the static memory created
delete [] charImage;
delete [] OutImage;

return (0);
ifp.close();
}

Console Output:


Number of rows:512
Number of columns:512
Maximum pixel value:255

Reading pixel values
--------------------------------
Process exited after 0.03873 seconds with return value 0
Press any key to continue . . .






Outputs:

Input Image Output Image




Output Image with threshold value 45

You might also like