CGIP Lab-File
CGIP Lab-File
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
Jalandhar, India
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>
int main( )
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cin>>x1>>y1;
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");
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);
}
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);
}
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;
getch();
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);
}
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]);
}
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}
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;
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>
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");
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