1.
Develop a program to draw a line using Bresenham’s line
drawing technique.
import turtle
def bresenham_line(x1, y1, x2, y2):
dx = abs(x2 - x1)
dy = abs(y2 - y1)
x_step = 1 if x1 < x2 else -1
y_step = 1 if y1 < y2 else -1
error = 2 * dy - dx
line_points = []
x, y = x1, y1
for _ in range(dx + 1):
line_points.append((x, y))
if error > 0:
y += y_step
error -= 2 * dx
error += 2 * dy
x += x_step
return line_points
turtle.setup(500, 500)
turtle.speed(0)
x1, y1 = 100, 100
x2, y2 = 400, 300
line_points = bresenham_line(x1, y1, x2, y2)
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
for x, y in line_points:
turtle.goto(x, y)
turtle.exitonclick()
2. Develop a program to demonstrate basic geometric operations on
the 2D object
import numpy as np
import matplotlib.pyplot as plt
triangle = np.array([[0,0],[1,0],[0.5,1],[0,0]])
translated_triangle = triangle +[1,2]
plt.plot(triangle[:,0],triangle[:,1],'r-',label = 'Original Triangle')
plt.plot(translated_triangle[:,0],translated_triangle[:,1],'g-',label =
'Translated Triangle')
plt.axis('equal')
plt.legend()
plt.show()
3. Develop a program to demonstrate basic geometric operations on
the 3D object
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def square():
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]])
edges = [(0, 1), (1, 2), (2, 3), (3, 0)]
return vertices, edges
def translate(vertices, tx, ty, tz):
return vertices + np.array([tx, ty, tz])
def plot_wireframe(ax, vertices, edges, color='blue'):
for start, end in edges:
ax.plot([vertices[start, 0], vertices[end, 0]],
[vertices[start, 1], vertices[end, 1]],
[vertices[start, 2], vertices[end, 2]], color=color)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
vertices, edges = square()
plot_wireframe(ax, vertices, edges, color='blue')
translated_vertices = translate(vertices, 1, 1, 1)
plot_wireframe(ax, translated_vertices, edges, color='green')
ax.set_xlim([-2, 3])
ax.set_ylim([-2, 3])
ax.set_zlim([-2, 3])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
4. Develop a program to demonstrate 2D transformation on basic
objects
import numpy as np
import matplotlib.pyplot as plt
triangle = np.array([[0, 0], [1, 0], [0.5, 1], [0, 0]])
translate = lambda shape, tx, ty: shape + [tx, ty]
translated_triangle = translate(triangle, 1, 2)
plt.plot(triangle[:, 0], triangle[:, 1], 'r-', label='Original Triangle')
plt.plot(translated_triangle[:, 0], translated_triangle[:, 1], 'g-',
label='Translated Triangle')
plt.axis('equal')
plt.legend()
plt.show()
6. Develop a program to demonstrate Animation effects on simple
objects.
import turtle
import colorsys
t = turtle.Turtle()
s = turtle.Screen().bgcolor('black')
t.speed(0)
n = 70
h = 0
for i in range (360):
c = colorsys.hsv_to_rgb(h, 1, 0.8)
h+= 1/n
t.color(c)
t.left(1)
t.fd(1)
for j in range (2):
t.left (2)
t.circle(90)
7. Write a Program to read a digital image. Split and display image
into 4 quadrants, up, down, right and left.
import cv2
img = cv2.imread("desktop/image.jpg")
if img is None:
print("Error reading image!")
exit()
height, width, channels = img.shape
half_height = height // 2
half_width = width // 2
top_left = img[:half_height, :half_width]
top_right = img[:half_height, half_width:]
bottom_left = img[half_height:, :half_width]
bottom_right = img[half_height:, half_width:]
cv2.imshow("Top Left", top_left)
cv2.imshow("Top Right", top_right)
cv2.imshow("Bottom Left", bottom_left)
cv2.imshow("Bottom Right", bottom_right)
cv2.waitKey(0)
cv2.destroyAllWindows()
8. Write a program to show rotation, scaling, and translation on an
image.
import cv2
import numpy as np
image_path = "image/atc.jpg"
img = cv2.imread(image_path)
height, width, _ = img.shape
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 45, 1)
scaling_matrix = np.float32([[1.5, 0, 0], [0, 1.5, 0]])
translation_matrix = np.float32([[1, 0, 100], [0, 1, 50]])
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))
cv2.imshow("Original Image", img)
cv2.imshow("Rotated Image", rotated_img)
cv2.imshow("Scaled Image", scaled_img)
cv2.imshow("Translated Image", translated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
9. Read an image and extract and display low-level features such as
edges, textures using filtering techniques.
import cv2
import numpy as np
image_path = "image/atc.jpg"
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
kernel = np.ones((5, 5), np.float32) / 25
texture = cv2.filter2D(gray, -1, kernel)
cv2.imshow("Original Image", img)
cv2.imshow("Edges", edges)
cv2.imshow("Texture", texture)
cv2.waitKey(0)
cv2.destroyAllWindows()
10. Write a program to blur and smoothing an image.
import cv2
image = cv2.imread('image/atc.jpg')
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
median_blur = cv2.medianBlur(image, 5)
bilateral_filter = cv2.bilateralFilter(image, 9, 75, 75)
cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Blur', gaussian_blur)
cv2.imshow('Median Blur', median_blur)
cv2.imshow('Bilateral Filter', bilateral_filter)
cv2.waitKey(0)
cv2.destroyAllWindows()
11. Write a program to contour an image.
import cv2
image = cv2.imread('desktop/image2.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
cv2.imshow('Image with contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
12. Write a program to detect a face/s in an image.
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades
+'haarcascade_frontalface_default.xml')
image = cv2.imread('image/face.jpeg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5,
minSize=(30, 30))
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Face Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()