0% found this document useful (0 votes)
96 views31 pages

CGIP Lab-File

The document describes the implementation of various computer graphics and image processing algorithms in a laboratory course. It includes 7 labs covering topics like line drawing, circle drawing, line clipping, ellipse drawing, and image transformations. The labs provide code examples and outputs for algorithms like DDA, Bresenham, midpoint circle, Cohen-Sutherland and Cyrus-Beck line clipping, and midpoint ellipse drawing.

Uploaded by

Rahul Kumar
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)
96 views31 pages

CGIP Lab-File

The document describes the implementation of various computer graphics and image processing algorithms in a laboratory course. It includes 7 labs covering topics like line drawing, circle drawing, line clipping, ellipse drawing, and image transformations. The labs provide code examples and outputs for algorithms like DDA, Bresenham, midpoint circle, Cohen-Sutherland and Cyrus-Beck line clipping, and midpoint ellipse drawing.

Uploaded by

Rahul Kumar
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/ 31

COMPUTER GRAPHICS AND IMAGE

PROCESSING LABORATORY
CSPC-423
Dr. B.R. Ambedkar National Institute of Technology
Jalandhar

Bachelors of Technology
in
Computer Science & Engineering Department
Submitted by
Shishir Yadav
Roll No.:-19103128
Course Faculty

Dr. Shveta Mahajan


Department of Computer Science & Engineering

NATIONAL INSTITUTE OF TECHNOLOGY

Jalandhar, India

Academic year: 2022-23


Table of Contents

Sr.No Lab Content Page No.

1 Implementation of various line drawing 3


algorithms: DDA, Bresenham’s, Midpoint.

2 Implementation of various circle drawing 7


algorithms: Bresenham’s.

3 Implementation of various line clipping 10


algorithms: Cohen Sutherland, Cyrus beck line
clipping.

4 Implementation of Midpoint ellipse drawing 18


algorithm.

5 Implementation of different translation, rotation, 21


and scaling techniques in the 2D plane.

6 Implementation of various composite 25


transformation techniques on an object.

7 Simulation and Display of an Image, Negative of 30


an Image (Binary & Grey Scale).

Page 2
Lab1
Aim:-Implementation of various line drawing algorithms: DDA,
Bresenham.
DDA Line drawing Algorithm
Code:-

#include <graphics.h>

#include <iostream>

#include <math.h>

#include <dos.h>

using namespace std;

int main( )

float x,y,x1,y1,x2,y2,dx,dy,step;

int i,gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

cout<<"Enter the value of x1 and y1 : ";

cin>>x1>>y1;

cout<<"Enter the value of x2 and y2: ";

cin>>x2>>y2;

dx=abs(x2-x1);

dy=abs(y2-y1);

if(dx>=dy)

step=dx;

Page 3
else

step=dy;

dx=dx/step;

dy=dy/step;

x=x1;

y=y1;

i=1;

while(i<=step)

putpixel(x,y,5);

x=x+dx;

y=y+dy;

i=i+1;

delay(100);

delay(1000);

closegraph();

Output:-

Page 4
Bresehnam Line Drawing algorithm
Code:-
#include<iostream>
#include<graphics.h>
using namespace std;
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
delay(10000);
}

Page 5
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter co-ordinates of first point: ";
cin>>x0>>y0;
cout<<"Enter co-ordinates of second point: ";
cin>>x1>>y1;
drawline(x0, y0, x1, y1);
return 0;
}
Output

Page 6
Lab 2
Aim:-Implementation of various circle drawing algorithms:
Bresenham’s.
Bresehnam’s Circle Drawing Algorithm
Code:-
#include<iostream>
#include<graphics.h>
using namespace std;
void drawcircle(int x0, int y0, int radius)
{
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{

Page 7
x -= 1;
err -= 2*x + 1;
}
}
delay(10000);
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

cout<<"Enter radius of circle: ";


cin>>r;

cout<<"Enter co-ordinates of center(x and y): ";


cin>>x>>y;
drawcircle(x, y, r);

return 0;
}
Output:-

Page 8
Page 9
Lab3
Aim:-Implementation of various line clipping algorithms: Cohen
Sutherland, Cyrus beck line clipping.
Cohen Sutherland
Code:-
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
using namespace std;
typedef struct coordinate
{
int x,y;
char code[4];
}PT;

void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);

int main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
cout<<"\nEnter x1 and y1\n";
cin>>p1.x>>p1.y;
cout<<"\nEnter x2 and y2\n";
cin>>p2.x>>p2.y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);

Page 10
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}

void drawline(PT p1,PT p2)


{
line(p1.x,p1.y,p2.x,p2.y);
}

PT setcode(PT p) //for setting the 4 bit code


{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; //Top

Page 11
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}

int visibility(PT p1,PT p2)


{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);
return(2);
}

PT resetendpt(PT p1,PT p2)

Page 12
{
PT temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 && temp.y>=100)
return (temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}
Output:-

Page 13
Cyrus Beck
code:-
#include<iostream>
#include<graphics.h>
#include<math.h>
#include<dos.h>
using namespace std;
int main()
{
int i,gd=DETECT,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;
x1=100;
y1=120;
x2=400;
y2=300;

xmin=100;
ymin=100;
xmax=250;
ymax=250;

Page 14
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0)
{
cout<<"line is parallel to one of the clipping boundary";
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}
if(y2>ymax)
{
y2=ymax;
}
line(x1,y1,x2,y2);
}
if(i>1)
{
if(x1<xmin)
{

Page 15
x1=xmin;
}
if(x2>xmax)
{
x2=xmax;
}
line(x1,y1,x2,y2);
}
}
}
}
t1=0;
t2=1;
for(i=0;i<4;i++)
{
temp=q[i]/p[i];
if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}
if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}

Page 16
delay(5000);
closegraph();
}
Output:-

Page 17
Lab 4
Aim:-Implementation of Midpoint ellipse drawing algorithm.
Code:-
#include<iostream>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
using namespace std;
void display (int xs1, int ys1, int x, int y);
void ellips1(int xs1,int ys1,int rx, int ry)
{
int x,y;
float d1,d2,dx,dy;
x = 0; // take start position as (0,ry)
y = ry; // finding decision parameter d1
d1 = pow(ry,2) - (pow(rx,2) * ry) + (0.25 * pow(rx,2));
dx = 2 * pow(ry,2) * x;
dy = 2 * pow(rx,2) * y;
do // region one
{
display(xs1,ys1,x,y);
if(d1<0)
{
x++;
dx = dx + (2 * (pow(ry,2)));
d1 = d1 + dx +(pow(ry,2));
}
else
{
x++;
y--;
dx = dx + (2 * (pow(ry,2)));
dy = dy - (2 * (pow(rx,2)));
d1 = d1 + dx - dy + (pow(ry,2));
}
}while(dx<dy); // change over condition for region-2
do // region two
{
display(xs1,ys1,x,y);

Page 18
if(d2>0)
{
x = x;
y--;
dy = dy - (2 * (pow(rx,2)));
d2 = d2 - dy + pow(rx,2);
}
else
{
x++;
y--;
dy = dy - (2 * (pow(rx,2)));
dx = dx + (2 * (pow(ry,2)));
d2 = d2 +dx - dy + pow(rx,2);

}
}while(y>0);
}
void display(int xs,int ys,int x,int y)
{
putpixel(xs+x,ys+y,WHITE); // plot points by using 4 point symmetry
putpixel(xs-x,ys-y,WHITE);
putpixel(xs+x,ys-y,WHITE);
putpixel(xs-x,ys+y,WHITE);
}
int main(void)
{
int xs1,ys1;
float rx1,ry1;
int gd = DETECT,gm; // Initialise the graphics system
initgraph(&gd,&gm,(char*)"");
cout<<"\t\tMidpoint Ellipe Drawing Algorithm\n";
cout<<"Enter the Center Co-ordinates\n";
cout<<"xc = \t";
cin>>xs1;
cout<<"yc = \t";
cin>>ys1;
cout<<"Enter the X Radius\t";
cin>>rx1;
cout<<"Enter the Y Radius\t";

Page 19
cin>>ry1;
ellips1(xs1,ys1,rx1,ry1);
getch();
closegraph();
}
Output

Page 20
Lab 5
Aim:-Implementation of different translation, rotation, and scaling
techniques in the 2D plane.
Code:-
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;

#define SIN(x) sin(x * 3.141592653589 / 180)


#define COS(x) cos(x * 3.141592653589 / 180)

void translateRectangle ( int P[][2], int T[])


{
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);

P[0][0] = P[0][0] + T[0];


P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];

getch();
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);
}

void rotate(float a[][2], int n, int x_pivot, int y_pivot,int angle)


{
line(a[0][0],a[0][1],a[1][0],a[1][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
line(a[2][0],a[2][1],a[3][0],a[3][1]);
line(a[3][0],a[3][1],a[0][0],a[0][1]);
int i = 0;
while (i < n) {

int x_shifted = a[i][0] - x_pivot;


int y_shifted = a[i][1] - y_pivot;

a[i][0] = x_pivot
+ (x_shifted * COS(angle)
- y_shifted * SIN(angle));

Page 21
a[i][1] = y_pivot
+ (x_shifted * SIN(angle)
+ y_shifted * COS(angle));
cout << "(" << a[i][0] << ", " << a[i][1] << ") ";
i++;
}
getch();
line(a[0][0],a[0][1],a[1][0],a[1][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
line(a[2][0],a[2][1],a[3][0],a[3][1]);
line(a[3][0],a[3][1],a[0][0],a[0][1]);
}

void findNewCoordinate(int s[][2], int p[][1])


{
int temp[2][1] = { 0 };

for (int i = 0; i < 2; i++)


for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);

p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}

void scale(int x[], int y[], int sx, int sy)


{
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);

int s[2][2] = { sx, 0, 0, sy };


int p[2][1];

for (int i = 0; i < 3; i++)


{
p[0][0] = x[i];
p[1][0] = y[i];

Page 22
findNewCoordinate(s, p);

x[i] = p[0][0];
y[i] = p[1][0];
}

getch();
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}

int main()
{
int P[2][2] = {5, 8, 120, 180};
int T[] = {200, 100};
initwindow(1600,800);
translateRectangle (P, T);
getch();

int size1 = 4;

float points_list1[][2] = { { 100, 100 },


{ 150, 200 },
{ 200, 200 },
{ 200, 150 } };
rotate(points_list1, size1, 100, 100, 90);
getch();

int x[] = { 100, 200, 300 };


int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;
scale(x, y, sx,sy);
getch();
closegraph();
return 0;
}
Output

Page 23
Page 24
Lab6
Aim:-Implementation of different translation, rotation, and scaling
techniques in the 2D plane.
Translation
Code:-
#include <bits/stdc++.h>
#include <graphics.h>

using namespace std;


void DrawLine(int x1, int y1, int x2, int y2, int T[])
{
setbkcolor(WHITE);
// setcolor(1);
line(150, 100, 450, 100);
line(450, 100, 450, 350);
line(450, 350, 150, 350);
line(150, 350, 150, 100);
setcolor(RED);
line(150 + T[0], 100 + T[1], 450 + T[0], 100 + T[1]);
line(450 + T[0], 100 + T[1], 450 + T[0], 350 + T[1]);
line(450 + T[0], 350 + T[1], 150 + T[0], 350 + T[1]);
line(150 + T[0], 350 + T[1], 150 + T[0], 100 + T[1]);
}

void Draw(int T[])


{
setcolor(5);
line(50, 100, 350, 100);
line(350, 100, 350, 350);
line(350, 350, 50, 350);
line(50, 350, 50, 100);

delay(1000);
setcolor(7);
line(50 + T[0], 100 + T[1], 350 + T[0], 100 + T[1]);
line(350 + T[0], 100 + T[1], 350 + T[0], 350 + T[1]);
line(350 + T[0], 350 + T[1], 50 + T[0], 350 + T[1]);
line(50 + T[0], 350 + T[1], 50 + T[0], 100 + T[1]);
}

Page 25
void translatePoint(int P[], int T[])
{
int gd = DETECT, gm, errorcode;
initgraph(&gd, &gm, "c:\\tc\\bgi");

putpixel(P[0], P[1], 1);


// DrawLine(50, 100, 100, 200, T);
// DrawLine(100, 200, 5, 250, T);
// DrawLine(10, 200, 70, 200, T);
Draw(T);

getch();
closegraph();
}

int main()
{
int P[2] = {5, 8};
int T[2] = {200, 100};
translatePoint(P, T);
return 0;
}
Output:-

Rotation

Page 26
Code:-
#include<graphics.h>
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
int gd=DETECT,gm;
int pivot_x,pivot_y,x,y;
double degree,radian;
int rotated_point_x,rotated_point_y;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
cleardevice();
cout<<"\n Enter an initial coordinates of the line = ";
cin>>pivot_x>>pivot_y;
cout<<"\n Enter a final coordinates of the line = ";
cin>>x>>y;
line(pivot_x,pivot_y,x,y);
cout<<"\n Enter a degree = ";
cin>>degree;
radian=degree*3.14/180;
rotated_point_x=(int)(pivot_x +((x-pivot_x)*cos(radian)-(y-pivot_y)*sin(radian)));
rotated_point_y=(int)(pivot_y +((x-pivot_x)*sin(radian)+(y-pivot_y)*cos(radian)));
setcolor(RED);
line(pivot_x,pivot_y,rotated_point_x,rotated_point_y);
getch();
closegraph();
}

Output:-

Page 27
Scaling
Code:-
#include <iostream>
#include <conio.h>
#include <graphics.h>
using namespace std;
int main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
cout<<"SCALING OF A LINE\n";
cout<<"Enter the first coordinate of a line:";
cin>>x1>>y1;
cout<<"Enter the second coordinate of a line:";
cin>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"Enter the scaling factor:";
cin>>sx>>sy;
setcolor(RED);
x1=x1*sx;
y1=y1*sy;
x2=x2*sx;
y2=y2*sy;
line(x1,y1,x2,y2);
getch();
closegraph();

Page 28
}
Output:-

Page 29
Lab 7
Aim:-Simulation and Display of an Image, Negative of an Image
(Binary & Grey Scale).
Code:-
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('/content/grey_image.jpg', 1)
plt.imshow(img)
plt.show()
img_inverse=255-img
plt.imshow(img_inverse)
plt.show()
Output:-

Page 30
Page 31

You might also like