Computer Graphics and Image Processing Laboratory Manual
Computer Graphics and Image Processing Laboratory Manual
LABORATORY-21CSL66
1. Bresenham’s Algorithm
9. Filtering techniques
import turtle
dx = abs(x2 - x1)
dy = abs(y2 - y1)
error = 2 * dy - dx
line_points = []
x, y = x1, y1
if error > 0:
y += y_step
error -= 2 * dx
error += 2 * dy
x += x_step
return line_points
# Example usage
turtle.setup(500, 500)
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
for x, y in line_points:
turtle.goto(x, y)
turtle.exitonclick()
OUTPUT:
Program-02:Develop a program to demonstrate basic geometric operations on the
2D object
import turtle
import math
screen = turtle.Screen()
screen.bgcolor("white")
t = turtle.Turtle()
t.penup()
t.goto(x, y)
t.pendown()
t.color(color)
for _ in range(2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
# Define a function to draw a circle
t.penup()
t.goto(x, y - radius)
t.pendown()
t.color(color)
t.circle(radius)
t.penup()
t.pendown()
t.penup()
t.goto(x, y)
t.setheading(angle)
t.pendown()
t.penup()
# Draw a rectangle
translate(-200, 0, 200, 0)
rotate(0, 0, 45)
scale(0, 0, 2, 2)
# Draw a circle
scale(300, 100, 2, 2)
turtle.done()
OUTPUT:
Program-03:Develop a program to demonstrate basic geometric operations on the
3D object
# Create a 3D canvas
scene = canvas(width=800, height=600, background=color.white)
# Draw a cuboid
cuboid = draw_cuboid((-2, 0, 0), 2, 2, 2, color.blue)
# Draw a cylinder
cylinder = draw_cylinder((2, 2, 0), 1, 10, color.red)
OUTPUT:
Program-04:Develop a program to demonstrate 2D transformation on basic objects
import cv2
import numpy as np
# Apply transformations
translated_obj = np.array([np.dot(translation_matrix, [x, y, 1])[:2] for x, y in obj_points],
dtype=np.int32)
rotated_obj = np.array([np.dot(rotation_matrix, [x, y, 1])[:2] for x, y in translated_obj], dtype=np.int32)
scaled_obj = np.array([np.dot(scaling_matrix, [x, y, 1])[:2] for x, y in rotated_obj], dtype=np.int32)
OUTPUT:
Program-05:Develop a program to demonstrate 3D transformation on 3D objects
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
# Initialize Pygame
pygame.init()
# Set up OpenGL
glClearColor(0.0, 0.0, 0.0, 1.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display_width / display_height), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
edges = np.array([
[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7]
], dtype=np.uint32)
# Main loop
running = True
angle = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Apply transformations
glLoadIdentity()
glMultMatrixf(translation_matrix)
glRotatef(angle, 1, 1, 0)
glMultMatrixf(rotation_matrix)
glMultMatrixf(scaling_matrix)
# Quit Pygame
pygame.quit()
OUTPUT:
Program-06:Develop a program to demonstrate Animation effects on simple objects.
import pygame
import random
# Initialize Pygame
pygame.init()
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Main loop
running = True
clock = pygame.time.Clock()
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Quit Pygame
pygame.quit()
OUTPUT:
Program-07:Write a Program to read a digital image. Split and display image into 4
quadrants, up, down, right and left.
import cv2
import numpy as np
OUTPUT:
Program-08:Write a program to show rotation, scaling, and translation on an image
import cv2
import numpy as np
# Apply transformations
rotated_img = cv2.warpAffine(img, rotation_matrix, (width, height))
scaled_img = cv2.warpAffine(img, scaling_matrix, (int(width*1.5), int(height*1.5)))
translated_img = cv2.warpAffine(img, translation_matrix, (width, height))
OUTPUT:
Program-09:Read an image and extract and display low-level features such as edges,
textures using filtering techniques.
import cv2
import numpy as np
# Edge detection
edges = cv2.Canny(gray, 100, 200) # Use Canny edge detector
# Texture extraction
kernel = np.ones((5, 5), np.float32) / 25 # Define a 5x5 averaging kernel
texture = cv2.filter2D(gray, -1, kernel) # Apply the averaging filter for texture extraction
OUTPUT:
Program-10:Write a program to blur and smoothing an image.
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)
OUTPUT:
Program-11:Write a program to contour an image.
import cv2
import numpy as np
# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
OUTPUT:
Program-12:Write a program to detect a face/s in an image.
import cv2
OUTPUT: