Computer Graphics Lab Manual For VTU
Computer Graphics Lab Manual For VTU
1 Program 01 2
1.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Program 02 4
2.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Program 03 6
3.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4 Program 04 9
4.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Program 05 12
5.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 Program 06 15
6.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
7 Program 07 17
7.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
8 Program 08 18
8.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
9 Program 09 20
9.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
10 Program 10 21
10.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
11 Program 11 22
11.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
12 Program 12 23
12.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1
1 Program 01
Develop a program to draw a line using Bresenham’s line drawing technique.
1.1 Code
import turtle
def bresenham_line ( x1 , y1 , x2 , y2 ) :
# Calculate the deltas
dx = abs ( x2 - x1 )
dy = abs ( y2 - y1 )
return line_points
# Example usage
turtle . setup (500 , 500)
turtle . speed (0) # Fastest drawing speed
2
x1 , y1 = 100 , 100
x2 , y2 = 400 , 300
line_points = bresenham_line ( x1 , y1 , x2 , y2 )
turtle . exitonclick ()
Listing 1: Bresenham’s Line Drawing Algorithm
3
2 Program 02
Develop a program to demonstrate basic geometric operations on the 2D object.
2.1 Code
import turtle
import math
4
# Translate the square
t r an s l at e d _v e r ti c e s = translate ( square_vertices , 200 ,
100)
draw_square (t , translated_vertices , " blue " )
5
3 Program 03
Develop a program to demonstrate basic geometric operations on the 3D object.
3.1 Code
import numpy as np
import matplotlib . pyplot as plt
from mpl_toolkits . mplot3d . art3d import
Poly3DCollection
# Translation function
def translate ( vertices , tx , ty , tz ) :
tr an sl at io n_ ma tr ix = np . array ([ tx , ty , tz ])
return vertices + tra ns la ti on _m at ri x
6
rotation_matrix = np . array ([
[ np . cos ( theta ) , - np . sin ( theta ) , 0] ,
[ np . sin ( theta ) , np . cos ( theta ) , 0] ,
[0 , 0 , 1]
])
return np . dot ( vertices , rotation_matrix )
# Scaling function
def scale ( vertices , sx , sy , sz ) :
scaling_matrix = np . array ([
[ sx , 0 , 0] ,
[0 , sy , 0] ,
[0 , 0 , sz ]
])
return np . dot ( vertices , scaling_matrix )
# Plotting function
def plot_cube ( vertices , title ) :
fig = plt . figure ()
ax = fig . add_subplot (111 , projection = ’3 d ’)
faces = create_faces ( vertices )
face_collection = Poly3DCollection ( faces , alpha
=0.25 , linewidths =1 , edgecolors = ’r ’)
face_collection . set_facecolor ( ’ cyan ’)
ax . add_collection3d ( face_collection )
# Set labels
ax . set_xlabel ( ’X ’)
ax . set_ylabel ( ’Y ’)
ax . set_zlabel ( ’Z ’)
plt . show ()
7
# Plot the original cube
plot_cube ( original_cube , " Original ␣ Cube " )
8
4 Program 04
Develop a program to demonstrate 2D transformation on basic objects.
4.1 Code
9
scaled_vertices = v e r t i c e s _ h o m o g e n e o u s . dot (
scaling_matrix . T )
return scaled_vertices [: , :2]
def main () :
# Define the original square vertices
square_vertices = np . array ([
[1 , 1] ,
[3 , 1] ,
[3 , 3] ,
[1 , 3]
])
plt . figure ()
plt . axis ( ’ equal ’)
plt . grid ( True )
t ra n s la t e d_ t r ia n g le = translate ( triangle_vertices ,
2 , 2)
plot_shape ( translated_triangle , color = ’m ’ , label = ’
Translated ␣ Triangle ␣ ( tx =2 , ␣ ty =2) ’)
10
rotated_triangle = rotate ( triangle_vertices , 45)
plot_shape ( rotated_triangle , color = ’y ’ , label = ’
Rotated ␣ Triangle ␣ (45 ␣ degrees ) ’)
plt . legend ()
plt . title ( ’2 D ␣ Transformations ␣ on ␣ a ␣ Square ␣ and ␣ a ␣
Triangle ’)
plt . xlabel ( ’X - axis ’)
plt . ylabel ( ’Y - axis ’)
plt . show ()
11
5 Program 05
Develop a program to demonstrate 3D transformation on 3D objects.
5.1 Code
import pygame
from pygame . locals import *
from OpenGL . GL import *
from OpenGL . GLU import *
import numpy as np
def draw_cube () :
glBegin ( GL_QUADS )
for surface in surfaces :
for vertex in surface :
glColor3fv ( colors [ vertex ])
glVertex3fv ( vertices [ vertex ])
glEnd ()
glBegin ( GL_LINES )
for edge in edges :
for vertex in edge :
glColor3fv ((0 , 0 , 0) )
glVertex3fv ( vertices [ vertex ])
glEnd ()
def main () :
pygame . init ()
display = (800 , 600)
pygame . display . set_mode ( display , DOUBLEBUF | OPENGL
)
12
gluPerspective (45 , ( display [0] / display [1]) , 0.1 ,
50.0)
glTranslatef (0.0 , 0.0 , -5)
while True :
for event in pygame . event . get () :
if event . type == pygame . QUIT :
pygame . quit ()
return
elif event . type == KEYDOWN :
if event . key == K_LEFT :
angle_y -= 5
elif event . key == K_RIGHT :
angle_y += 5
elif event . key == K_UP :
angle_x -= 5
elif event . key == K_DOWN :
angle_x += 5
elif event . key == K_PLUS or event . key
== K_EQUALS :
scale += 0.1
elif event . key == K_MINUS or event . key
== K_UNDERSCORE :
scale -= 0.1
# Apply transformations
glPushMatrix ()
glTranslatef ( x_translation , y_translation , 0)
glRotatef ( angle_x , 1 , 0 , 0)
glRotatef ( angle_y , 0 , 1 , 0)
glScalef ( scale , scale , scale )
13
# Draw the cube
draw_cube ()
glPopMatrix ()
14
6 Program 06
Develop a program to demonstrate Animation effects on simple objects.
6.1 Code
import pygame
from pygame . locals import *
from OpenGL . GL import *
from OpenGL . GLU import *
import math
import numpy as np
def main () :
pygame . init ()
display = (800 , 600)
pygame . display . set_mode ( display , DOUBLEBUF | OPENGL
)
gluOrtho2D ( -400 , 400 , -300 , 300)
while True :
for event in pygame . event . get () :
if event . type == pygame . QUIT :
pygame . quit ()
return
15
# Clear the screen and set the color
glClear ( G L_ C O LO R _ BU F F ER _ B IT )
glClearColor (0.0 , 0.0 , 0.0 , 1.0)
# Apply transformations
glPushMatrix ()
glTranslatef ( x_translation , y_translation , 0)
glRotatef ( angle , 0 , 0 , 1)
glScalef ( scale , scale , 1.0)
glPopMatrix ()
# Update animations
angle += 1 # Rotate
scale = 1.5 + 0.5 * np . sin ( pygame . time .
get_ticks () * 0.001) # Scale with time
x_translation = 200 * np . sin ( pygame . time .
get_ticks () * 0.001) # Translate with time
y_translation = 150 * np . cos ( pygame . time .
get_ticks () * 0.001) # Translate with time
color_change += 0.01 # Change color over time
16
7 Program 07
Write a Program to read a digital image. Split and display image into 4 quad-
rants, up, down, right and left.
7.1 Code
import cv2
17
8 Program 08
Write a program to show rotation, scaling, and translation on an image.
8.1 Code
import cv2
import numpy as np
# 1. Rotation
# Define the center of rotation and the rotation angle
center = ( width // 2 , height // 2)
angle = 45 # Rotate by 45 degrees
scale = 1.0 # No scaling
18
scaled_image = cv2 . resize ( image , None , fx = scale_x , fy =
scale_y , interpolation = cv2 . INTER_LINEAR )
show_image ( ’ Scaled ␣ Image ’ , scaled_image )
# 3. Translation
# Define the translation matrix
tx , ty = 100 , 50 # Translate by 100 pixels in x and
50 pixels in y
t ran sl at io n_ ma tr ix = np . float32 ([[1 , 0 , tx ] , [0 , 1 , ty
]])
19
9 Program 09
Read an image and extract and display low-level features such as edges, textures
using filtering techniques.
9.1 Code
import cv2
import numpy as np
# Load the image
image_path = ’C :/ Desktop ␣ Folders / rose . jpg ’ # Replace
with the path to your image
img = cv2 . imread ( image_path )
# Edge detection
edges = cv2 . Canny ( gray , 100 , 200) # Use Canny edge
detector
# Texture extraction
kernel = np . ones ((5 , 5) , np . float32 ) / 25 # Define a 5
x5 averaging kernel
texture = cv2 . filter2D ( gray , -1 , kernel ) # Apply the
averaging filter for texture extraction
20
10 Program 10
Write a program to blur and smoothing an image.
10.1 Code
import cv2
# Gaussian Blur
gaussian_blur = cv2 . GaussianBlur ( image , (5 , 5) , 0)
# Median Blur
median_blur = cv2 . medianBlur ( image , 5)
# Bilateral Filter
bilateral_filter = cv2 . bilateralFilter ( image , 9 , 75 ,
75)
21
11 Program 11
Write a program to contour an image.
11.1 Code
import cv2
import numpy as np
# Load the image
image = cv2 . imread ( ’C :/ Desktop ␣ Folders / flower . jpg ’)
# Convert the image to grayscale
gray = cv2 . cvtColor ( image , cv2 . COLOR_BGR2GRAY )
22
12 Program 12
Write a program to detect a face/s in an image.
12.1 Code
import cv2
# Load the cascade classifier for face detection
face_cascade = cv2 . Ca scadeC lassif ier ( cv2 . data .
haarcascades + ’ h a a r c a s c a d e _ f r o n t a l f a c e _ d e f a u l t . xml
’)
# Load the image
image = cv2 . imread ( ’C :/ Desktop ␣ Folders / child . jpg ’)
# Convert the image to grayscale
gray = cv2 . cvtColor ( image , cv2 . COLOR_BGR2GRAY )
# Detect faces in the grayscale image
faces = face_cascade . detectMultiScale ( gray ,
scaleFactor =1.1 , minNeighbors =5 ,
minSize =(30 , 30) )
# Draw rectangles around the detected faces
for (x , y , w , h ) in faces :
cv2 . rectangle ( image , (x , y ) , ( x + w , y + h ) , (0 , 255 ,
0) , 2)
# Display the image with detected faces
cv2 . imshow ( ’ Face ␣ Detection ’ , image )
# Wait for a key press to close the window
cv2 . waitKey (0)
cv2 . d estroy AllWin dows ()
Listing 12: Detect a Face/s in an Image
23