0% found this document useful (0 votes)
6 views9 pages

CG Lab

Uploaded by

anushashegde.25
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)
6 views9 pages

CG Lab

Uploaded by

anushashegde.25
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/ 9

Program 7 translated_image = cv2.

warpAffine(image,
translation_matrix, (image.shape[1],
import cv2 image.shape[0]))
def split_image(image_path): return translated_image
image = cv2.imread(image_path) def main():
height, width, _ = image.shape # Read the image
left = image[0:height, 0:width//2] image_path = "your_image_path.jpg" # Replace
right = image[0:height, width//2:width] with the path to your image
up = image[0:height//2, 0:width] original_image = cv2.imread(image_path)
down = image[height//2:height, 0:width] # Rotate the image
return left, right, up, down rotated_image = rotate_image(original_image,
def display_quadrants(left, right, up, down): 45)
cv2.imshow('Left Quadrant', left) # Scale the image
cv2.imshow('Right Quadrant', right) scaled_image = scale_image(original_image,
cv2.imshow('Upper Quadrant', up) 1.5)
cv2.imshow('Lower Quadrant', down) # Translate the image
cv2.waitKey(0) translated_image =
cv2.destroyAllWindows() translate_image(original_image, 50, 50)
def main(): # Display the images
image_path = "Your image name.jpg" cv2.imshow('Original Image', original_image)
left, right, up, down = split_image(image_path) cv2.imshow('Rotated Image', rotated_image)
display_quadrants(left, right, up, down) cv2.imshow('Scaled Image', scaled_image)
if __name__ == "__main__": cv2.imshow('Translated Image',
main() translated_image)
cv2.waitKey(0)
Program 8 cv2.destroyAllWindows()
if __name__ == "__main__":
import cv2 main()
import numpy as np
def rotate_image(image, angle):
height, width = image.shape[:2]
rotation_matrix =
cv2.getRotationMatrix2D((width/2, height/2),
angle, 1)
rotated_image = cv2.warpAffine(image,
rotation_matrix, (width, height))
return rotated_image
def scale_image(image, scale_factor):
scaled_image = cv2.resize(image, None,
fx=scale_factor, fy=scale_factor,
interpolation=cv2.INTER_LINEAR)
return scaled_image
def translate_image(image, tx, ty):
translation_matrix = np.float32([[1, 0, tx], [0, 1,
ty]])
Program 10 Program 11

import cv2 import cv2


def display_image(title, image): def display_image(title, image):
cv2.imshow(title, image) cv2.imshow(title, image)
cv2.waitKey(0) cv2.waitKey(0)
cv2.destroyAllWindows() cv2.destroyAllWindows()
def blur_image(image): def contour_image(image):
# Apply Gaussian blur gray = cv2.cvtColor(image,
blurred = cv2.GaussianBlur(image, (5, 5), 0) cv2.COLOR_BGR2GRAY)
return blurred _, binary = cv2.threshold(gray, 127, 255,
def main(): cv2.THRESH_BINARY)
# Read the image contours, _ = cv2.findContours(binary,
image_path = "your_image_path.jpg" # Replace cv2.RETR_EXTERNAL,
with the path to your image cv2.CHAIN_APPROX_SIMPLE)
image = cv2.imread(image_path) contour_image = image.copy()
# Blur the image cv2.drawContours(contour_image, contours,
blurred_image = blur_image(image) -1, (0, 255, 0), 2)
# Display the original and blurred images return contour_image
display_image('Original Image', image) def main():
display_image('Blurred Image', blurred_image) # Read the image
if __name__ == "__main__": image_path = "your_image_path.jpg" #
main() Replace with the path to your image
image = cv2.imread(image_path)
Program 9 contoured_image = contour_image(image)
display_image('Original Image', image)
import cv2 display_image('Contoured Image',
import numpy as np contoured_image)
image_path = "image/atc.jpg" # Replace with if __name__ == "__main__":
the path to your image main()
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)
extraction
cv2.imshow("Original Image", img)
cv2.imshow("Edges", edges)
cv2.imshow("Texture", texture)
cv2.waitKey(0)
cv2.destroyAllWindows()
Program 12 Program 6 A

import cv2 from OpenGL.GL import *


def display_image(title, image): from OpenGL.GLUT import *
cv2.imshow(title, image) from OpenGL.GLU import *
cv2.waitKey(0) import time
cv2.destroyAllWindows() angle = 0
def detect_faces(image_path): scale = 1.0
scale_direction = 1
face_cascade = translation = [0.0, 0.0, 0.0]
cv2.CascadeClassifier(cv2.data.haarcascades + translation_direction = [1, 1, 1]
'haarcascade_frontalface_default.xml') vertices = (
image = cv2.imread(image_path) (1, -1, -1),
gray = cv2.cvtColor(image, (1, 1, -1),
cv2.COLOR_BGR2GRAY) (-1, 1, -1),
faces = face_cascade.detectMultiScale(gray, (-1, -1, -1),
scaleFactor=1.1, minNeighbors=5, (1, -1, 1),
minSize=(30, 30)) (1, 1, 1),
for (x, y, w, h) in faces: (-1, -1, 1),
cv2.rectangle(image, (x, y), (x+w, y+h), (0, (-1, 1, 1)
255, 0), 2) )
return image edges = (
def main(): (0, 1),
image_path = "your_image_path.jpg" # Replace (1, 2),
with the path to your image (2, 3),
image_with_faces = detect_faces(image_path) (3, 0),
display_image('Image with Faces Detected', (4, 5),
image_with_faces) (5, 6),
if __name__ == "__main__": (6, 7),
main() (7, 4),
(0, 4),
(1, 5),
(2, 6),
(3, 7)
)
surfaces = (
(0, 1, 2, 3),
(3, 2, 7, 6),
(6, 7, 5, 4),
(4, 5, 1, 0),
Program 6 B Program 6 C

(1, 5, 7, 2), global scale, scale_direction, translation,


(4, 0, 3, 6) translation_direction
) # Update scale
colors = ( scale += 0.01 * scale_direction
(1, 0, 0), if scale >= 1.5 or scale <= 0.5:
(0, 1, 0), scale_direction *= -1
(0, 0, 1), # Update translation
(1, 1, 0), for i in range(3):
(1, 0, 1), translation[i] += 0.01 *
(0, 1, 1) translation_direction[i]
) if translation[i] >= 1 or translation[i] <= -1:
def draw_cube(): translation_direction[i] *= -1
glBegin(GL_QUADS) glutPostRedisplay()
for i, surface in enumerate(surfaces): time.sleep(0.01)
glColor3fv(colors[i]) def keyboard(key, x, y):
for vertex in surface: if key == b'\x1b': # ESC key
glVertex3fv(vertices[vertex]) glutLeaveMainLoop()
glEnd() def reshape(width, height):
glBegin(GL_LINES) if height == 0:
glColor3fv((0, 0, 0)) height = 1
for edge in edges: glViewport(0, 0, width, height)
for vertex in edge: glMatrixMode(GL_PROJECTION)
glVertex3fv(vertices[vertex]) glLoadIdentity()
glEnd() gluPerspective(45, width / height, 0.1, 50.0)
def display(): glMatrixMode(GL_MODELVIEW)
global angle, scale, translation glLoadIdentity()
glClear(GL_COLOR_BUFFER_BIT | def main():
GL_DEPTH_BUFFER_BIT) glutInit()
glLoadIdentity() glutInitDisplayMode(GLUT_DOUBLE |
glTranslatef(0.0, 0.0, -10) GLUT_RGB | GLUT_DEPTH)
glScalef(scale, scale, scale) glutInitWindowSize(800, 600)
glTranslatef(*translation) glutCreateWindow("Cube with Animation")
glRotatef(angle, 1, 1, 1) glEnable(GL_DEPTH_TEST)
draw_cube() glutDisplayFunc(display)
glutSwapBuffers() glutIdleFunc(animate)
angle += 0.5 glutReshapeFunc(reshape)
def animate(): glutKeyboardFunc(keyboard)
glutMainLoop()
if __name__ == "__main__":
main(
Program 1 Program 2

from OpenGL.GL import * from OpenGL.GL import *


from OpenGL.GLUT import * from OpenGL.GLUT import *
from OpenGL.GLU import * from OpenGL.GLU import *
def init():
glClearColor(0.0,0.0,0.0,1.0) def Init():
gluOrtho2D(0,100,0,100) glMatrixMode(GL_PROJECTION)
def plotLine(x1,y1,x2,y2): glLoadIdentity()
m = 2 * (y2 - y1) gluOrtho2D(0, 100, 0, 100)
pk = m - (x2 - x1) glClearColor(1.0, 1.0, 1.0, 1.0)
y=y1 glClear(GL_COLOR_BUFFER_BIT)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0,0.0,0.0) def draw():
glPointSize(10.0) glColor3f(0.0, 1.0, 0.0)
glBegin(GL_POINTS) glPointSize(10.0)
for x in range(x1,x2+1): glBegin(GL_POLYGON)
glVertex2f(x,y) glVertex2i(10, 20)
pk =pk + m glVertex2i(80, 20)
if (pk>= 0): glVertex2i(80, 60)
y=y+1 glVertex2i(10, 60)
pk =pk - 2 * (x2 - x1) glEnd()
glEnd() glFlush()
glFlush()
x1 = int(input("Enter x1: ")) def main():
y1 = int(input("Enter y1: ")) glutInit()
x2 = int(input("Enter x2: ")) glutInitWindowSize(500, 500)
y2 = int(input("Enter y2: ")) glutCreateWindow("OpenGL Window")
print("starting window....") glutDisplayFunc(draw)
glutInit(sys.argv) Init()
glutInitDisplayMode(GLUT_RGB) glutMainLoop()
glutInitWindowSize(500,500)
glutInitWindowPosition(0,0) if _name_ == "_main_":
glutCreateWindow("Bresenham Line main()
Algorithm")
glutDisplayFunc(lambda:plotLine(x1,y1,x2,y2))
init()
glutMainLoop()
Program 4 elif key == b'f' or key == b'F':
translate_x -= 0.1
import sys elif key == b'g' or key == b'G':
from OpenGL.GL import * translate_y += 0.1
from OpenGL.GLUT import * elif key == b'h' or key == b'H':
from OpenGL.GLU import * translate_y -= 0.1
angle = 0.0 glutPostRedisplay()
scale_factor = 1.0 def main():
translate_x = 0.0 glutInit(sys.argv)
translate_y = 0.0 glutInitDisplayMode(GLUT_DOUBLE |
def Init(): GLUT_RGB)
glClearColor(1.0, 1.0, 1.0, 1.0) glutInitWindowSize(500, 500)
glMatrixMode(GL_PROJECTION) glutInitWindowPosition(100, 100)
glLoadIdentity() glutCreateWindow(b"2D Transformation")
gluOrtho2D(-1.0, 1.0, -1.0, 1.0) glutDisplayFunc(display)
def draw_square(): glutKeyboardFunc(keyboard)
glBegin(GL_POLYGON) Init()
glVertex2f(0.1, -0.1) glutMainLoop()
glVertex2f(0.1, 0.1) if _name_ == "_main_":
glVertex2f(-0.1, 0.1) main()
glVertex2f(-0.1, -0.1)
glEnd()
def display():
global angle, scale_factor, translate_x,
translate_y
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(translate_x, translate_y,
0.0)
glRotatef(angle, 0.0, 0.0, 1.0)
glScalef(scale_factor, scale_factor, 1.0)
glColor3f(0.0, 0.0, 1.0)
draw_square()
glutSwapBuffers()
def keyboard(key, x, y):
global angle, scale_factor, translate_x,
translate_y
if key == b'q' or key == b'Q':
sys.exit()
elif key == b'r' or key == b'R':
angle += 10.0
elif key == b's' or key == b'S':
scale_factor += 0.1
elif key == b't' or key == b'T':
translate_x += 0.1
Program 3 glBegin(GL_QUADS)
for i, surface in enumerate(surfaces):
from OpenGL.GL import * glColor3fv(colors[i])
from OpenGL.GLU import * for vertex in surface:
from OpenGL.GLUT import * glVertex3fv(vertices[vertex])
vertices = ( glEnd()
(1, -1, -1), glBegin(GL_LINES)
(1, 1, -1), glColor3fv((0, 0, 0))
(-1, 1, -1), for edge in edges:
(-1, -1, -1), for vertex in edge:
(1, -1, 1), glVertex3fv(vertices[vertex])
(1, 1, 1), glEnd()
(-1, -1, 1), def display():
(-1, 1, 1) glClear(GL_COLOR_BUFFER_BIT |
) GL_DEPTH_BUFFER_BIT)
edges = ( glRotatef(1, 3, 1, 1)
(0, 1), cube()
(1, 2), glutSwapBuffers()
(2, 3), def timer(value):
(3, 0), glutPostRedisplay()
(4, 5), glutTimerFunc(10, timer, 0)
(5, 6), def main():
(6, 7), glutInit()
(7, 4), glutInitDisplayMode(GLUT_RGBA |
(0, 4), GLUT_DOUBLE | GLUT_DEPTH)
(1, 5), glutInitWindowSize(800, 600)
(2, 6), glutCreateWindow("Rotating Cube")
(3, 7) glEnable(GL_DEPTH_TEST)
) gluPerspective(45, 800/600, 0.1, 50.0)
surfaces = ( glTranslatef(0.0, 0.0, -5)
(0, 1, 2, 3), glutDisplayFunc(display)
(3, 2, 7, 6), glutTimerFunc(10, timer, 0)
(6, 7, 5, 4), glutMainLoop()
(4, 5, 1, 0), if _name_ == "_main_":
(1, 5, 7, 2), main()
(4, 0, 3, 6)
)
colors = (
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(1, 1, 0),
(1, 0, 1),
(0, 1, 1)
)
def cube():
Program 5 def keyboard(key, x, y):
global scale
from OpenGL.GL import * if key == b'\x1b': # Esc key
from OpenGL.GLUT import * glutLeaveMainLoop()
from OpenGL.GL import * elif key == b'+':
from OpenGL.GLUT import * scale += 0.1
from OpenGL.GLU import * elif key == b'-':
angle = 0 scale -= 0.1
scale = 1.0 def special_keyboard(key, x, y):
translation = [0.0, 0.0, 0.0] global translation
vertices = ((1, -1, -1), (1, 1, -1), (-1, 1, -1), if key == GLUT_KEY_LEFT:
(-1, -1, -1), (1, -1, 1), (1, 1, 1), (-1, -1, 1), translation[0] -= 0.1
(-1, 1, 1)) elif key == GLUT_KEY_RIGHT:
edges = ((0, 1), (1, 2), (2, 3), (3, 0), (4, 5), translation[0] += 0.1
(5, 6), (6, 7), (7, 4), (0, 4), (1, 5),(2, 6), elif key == GLUT_KEY_UP:
(3, 7)) translation[1] += 0.1
surfaces = ((0, 1, 2, 3), (3, 2, 7, 6), (6, 7, 5, elif key == GLUT_KEY_DOWN:
4), (4, 5, 1, 0), (1, 5, 7, 2), (4, 0, 3, 6)) translation[1] -= 0.1
colors = ((1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, def reshape(width, height):
0), (1, 0, 1), (0, 1, 1)) if height == 0:
def draw_cube(): height = 1
glBegin(GL_QUADS) glViewport(0, 0, width, height)
for i, surface in enumerate(surfaces): glMatrixMode(GL_PROJECTION)
glColor3fv(colors[i]) glLoadIdentity()
for vertex in surface: gluPerspective(45, width / height, 0.1, 50.0)
glVertex3fv(vertices[vertex]) glMatrixMode(GL_MODELVIEW)
glEnd() glLoadIdentity()
glBegin(GL_LINES) def main():
glColor3fv((0, 0, 0)) glutInit()
for edge in edges: glutInitDisplayMode(GLUT_DOUBLE |
for vertex in edge: GLUT_RGB | GLUT_DEPTH)
glVertex3fv(vertices[vertex]) glutInitWindowSize(800, 600)
glEnd() glutCreateWindow("Rotating, Scaling, &
def display(): Translating Cube")
global angle glEnable(GL_DEPTH_TEST)
glClear(GL_COLOR_BUFFER_BIT | glutDisplayFunc(display)
GL_DEPTH_BUFFER_BIT) glutIdleFunc(display)
glLoadIdentity() glutReshapeFunc(reshape)
glTranslatef(0.0, 0.0, -5) glutKeyboardFunc(keyboard)
glScalef(scale, scale, scale) glutSpecialFunc(special_keyboard)
glTranslatef(*translation) glutMainLoop()
glRotatef(angle, 3, 1, 1) if _name_ == "_main_":
draw_cube() main()
glutSwapBuffers()
angle += 0.2 # Reduced increment for
slower rotation

You might also like