0% found this document useful (0 votes)
38 views

Computer Graphics and Image Processing Lab Manual

image processing lab manual

Uploaded by

azlan
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)
38 views

Computer Graphics and Image Processing Lab Manual

image processing lab manual

Uploaded by

azlan
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/ 32

P.A.

COLLEGE OF ENGINEERING, MANGALURU

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

VI SEMESTER

LAB MANUAL

COURSE: COMPUTER GRAPHICS AND IMAGE


PROCESSING LABORATORY

COURSE CODE: 21CSL66

2023-2024

1
Contents

Sl. No Topic Page No


1 Syllabus 3
2 Develop a program to draw a line using Bresenham’s line drawing technique 4
3 Develop a program to demonstrate basic geometric operations on the 2D 6
object
4 Develop a program to demonstrate basic geometric operations on the 3D 13
object
5 Develop a program to demonstrate 2D transformation on basic objects 16
6 Develop a program to demonstrate 3D transformation on 3D objects 18
7 Develop a program to demonstrate Animation effects on simple objects 22
8 Write a Program to read a digital image. Split and display image into 4 27
quadrants, up, down, right and left
9 Write a program to show rotation, scaling, and translation on an image 33
10 Read an image and extract and display low-level features such as edges, 42
textures using filtering techniques
11 Write a program to blur and smoothing an image 44
12 Write a program to contour an image
13 Write a program to detect a face/s in an image.
14 Viva Questions 4

2
SYLLABUS

EXPERIMENT 1

Develop a program to draw a line using Bresenham’s line drawing technique

#include<GL/freeglut.h>
#include<stdio.h>
int x1, y1, x2, y2;
void setPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void drawLine(int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2;
int e2;
while (1) {
setPixel(x1, y1);
if (x1 == x2 && y1 == y2) break;
e2 = err;
if (e2 > -dx) { err -= dy; x1 += sx; }
if (e2 < dy) { err += dx; y1 += sy; }
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0); // Set color to white
drawLine(x1, y1, x2, y2);

3
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv) {
printf("Enter the coordinates of two points (x1, y1) and (x2, y2): ");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing");
glClearColor(0.0, 0.0, 0.0, 1.0); // Set clear color to black
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

4
Output

EXPERIMENT 2

Develop a program to demonstrate basic geometric operations on the 2D object

#include <GL/freeglut.h>
#include <stdio.h>
#include<math.h>
int objectX = 300;
int objectY = 300;
int objectWidth = 100;

5
int objectHeight = 100;
void drawObject() {
glBegin(GL_POLYGON);
glVertex2i(objectX, objectY);
glVertex2i(objectX + objectWidth, objectY);
glVertex2i(objectX + objectWidth, objectY + objectHeight);
glVertex2i(objectX, objectY + objectHeight);
glEnd();
}
void translateObject(int dx, int dy) {
objectX += dx;
objectY += dy;
}

void scaleObject(float scaleX, float scaleY) {


objectWidth *= scaleX;
objectHeight *= scaleY;
}
void rotateObject(float angle) {
float newX, newY;
float pivotX = objectX + objectWidth / 2.0;
float pivotY = objectY + objectHeight / 2.0;
angle = angle * 3.14159 / 180.0;
newX = (objectX - pivotX) * cos(angle) - (objectY - pivotY) * sin(angle) + pivotX;
newY = (objectX - pivotX) * sin(angle) + (objectY - pivotY) * cos(angle) + pivotY;
objectX = newX;
objectY = newY;
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0); // Set color to white
drawObject();

6
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 't':
translateObject(20, 20);
break;
case 's':
scaleObject(1.1, 1.1);
break;
case 'r':
rotateObject(30);
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Basic Geometric Operations");
glClearColor(0.0, 0.0, 0.0, 1.0); // Set clear color to black
glutDisplayFunc(display);

7
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Output

8
EXPERIMENT 3
Develop a program to demonstrate basic geometric operations on the 3D object.

#include <GL/freeglut.h>
#include <stdio.h>
GLfloat angleX = 0.0;
GLfloat angleY = 0.0;
GLfloat angleZ = 0.0;
void drawCube() {
glBegin(GL_QUADS);
// Front face
glColor3f(1.0, 0.0, 0.0); // Red
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
// Back face
glColor3f(0.0, 1.0, 0.0); // Green
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
// Top face
glColor3f(0.0, 0.0, 1.0); // Blue
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
// Bottom face

9
glColor3f(1.0, 1.0, 0.0); // Yellow
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
// Right face
glColor3f(1.0, 0.0, 1.0); // Magenta
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
// Left face
glColor3f(0.0, 1.0, 1.0); // Cyan
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0); // Move back 5 units
glRotatef(angleX, 1.0, 0.0, 0.0); // Rotate around x-axis
glRotatef(angleY, 0.0, 1.0, 0.0); // Rotate around y-axis
glRotatef(angleZ, 0.0, 0.0, 1.0); // Rotate around z-axis
drawCube();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);

10
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 'x':
angleX += 5.0;
break;
case 'y':
angleY += 5.0;
break;
case 'z':
angleZ += 5.0;
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Basic Geometric Operations - 3D Cube");
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0); // Set clear color to black
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;

11
}

Output

EXPERIMENT 4
Develop a program to demonstrate 2D transformation on basic objects

#include <GL/glut.h>
#include<math.h>
#include<stdio.h>
// Rectangle parameters
GLfloat rectWidth = 100.0;
GLfloat rectHeight = 50.0;
GLfloat rectPosX = 0.0;
GLfloat rectPosY = 0.0;

12
// Rotation angle
GLfloat rotationAngle = 0.0;
// Scaling factors
GLfloat scaleX = 1.0;
GLfloat scaleY = 1.0;
void drawRectangle() {
glPushMatrix();
// Apply translation
glTranslatef(rectPosX, rectPosY, 0.0);
// Apply rotation
glRotatef(rotationAngle, 0.0, 0.0, 1.0);
// Apply scaling
glScalef(scaleX, scaleY, 1.0);
// Draw rectangle
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0); // Red color
glVertex2f(-rectWidth / 2, -rectHeight / 2);
glVertex2f(rectWidth / 2, -rectHeight / 2);
glVertex2f(rectWidth / 2, rectHeight / 2);
glVertex2f(-rectWidth / 2, rectHeight / 2);
glEnd();

glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Draw rectangle
drawRectangle();
glFlush();
}
void reshape(int w, int h) {

13
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w / 2, w / 2, -h / 2, h / 2, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'w':
rectPosY += 10.0; // Move rectangle up
break;
case 's':
rectPosY -= 10.0; // Move rectangle down
break;
case 'a':
rectPosX -= 10.0; // Move rectangle left
break;
case 'd':
rectPosX += 10.0; // Move rectangle right
break;
case 'r':
rotationAngle += 10.0; // Rotate clockwise
break;
case 'e':
rotationAngle -= 10.0; // Rotate counterclockwise
break;
case '+':
scaleX += 0.1; // Scale up
scaleY += 0.1;
break;
case '-':

14
scaleX -= 0.1; // Scale down
scaleY -= 0.1;
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Basic Geometric Operations in OpenGL");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glClearColor(1.0, 1.0, 1.0, 1.0); // White background color
glutMainLoop();
return 0;
}
Output

15
EXPERIMENT 5
Develop a program to demonstrate 3D transformation on 3D objects

#include <GL/glut.h>
#include <stdio.h>
// Cube parameters
GLfloat cubeSize = 50.0;
GLfloat cubePosX = -50.0;
GLfloat cubePosY = -25.0;
GLfloat cubePosZ = -50.0;
// Rotation angles
GLfloat rotateX = 0.0;
GLfloat rotateY = 0.0;
GLfloat rotateZ = 0.0;
void drawCube() {
glPushMatrix();
// Apply translation
glTranslatef(cubePosX, cubePosY, cubePosZ);

16
// Apply rotation
glRotatef(rotateX, 1.0, 0.0, 0.0);
glRotatef(rotateY, 0.0, 1.0, 0.0);
glRotatef(rotateZ, 0.0, 0.0, 1.0);
// Draw cube
glColor3f(1.0, 0.0, 0.0); // Red color
glutSolidCube(cubeSize);
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Set perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 200.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// Draw cube
drawCube();
glutSwapBuffers();
}
void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
cubePosY += 10.0; // Move cube up
break;
case GLUT_KEY_DOWN:
cubePosY -= 10.0; // Move cube down
break;
case GLUT_KEY_LEFT:

17
cubePosX -= 10.0; // Move cube left
break;
case GLUT_KEY_RIGHT:
cubePosX += 10.0; // Move cube right
break;
}
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'w':
rotateX += 5.0; // Rotate clockwise around X-axis
break;
case 's':
rotateX -= 5.0; // Rotate counterclockwise around X-axis
break;
case 'a':
rotateY += 5.0; // Rotate clockwise around Y-axis
break;
case 'd':
rotateY -= 5.0; // Rotate counterclockwise around Y-axis
break;
case 'q':
rotateZ += 5.0; // Rotate clockwise around Z-axis
break;
case 'e':
rotateZ -= 5.0; // Rotate counterclockwise around Z-axis
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {

18
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("3D Transformations in OpenGL");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glutKeyboardFunc(keyboard);
glClearColor(1.0, 1.0, 1.0, 1.0); // White background color
glutMainLoop();
return 0;
}

Output

19
EXPERIMENT 6
Develop a program to demonstrate Animation effects on simple objects

#include <GL/glut.h>
#include <stdio.h>
GLfloat x = 0.0;
GLfloat y = 0.0;
GLfloat deltaX = 0.05;
GLfloat deltaY = 0.05;
void drawSquare() {
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0); // Red
glVertex2f(x - 0.1, y - 0.1);
glVertex2f(x + 0.1, y - 0.1);
glVertex2f(x + 0.1, y + 0.1);
glVertex2f(x - 0.1, y + 0.1);
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
drawSquare();
glutSwapBuffers();
}

void update(int value) {


if (x > 1.0 || x < -1.0)
deltaX = -deltaX;
if (y > 1.0 || y < -1.0)
deltaY = -deltaY;
x += deltaX;

20
y += deltaY;
glutPostRedisplay();
glutTimerFunc(10, update, 0);
}

void reshape(int w, int h) {


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Animation Effects - Moving Square");
glClearColor(0.0, 0.0, 0.0, 1.0); // Set clear color to black
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}
Output

21
EXPERIMENT 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('images.jpg')
# cv2.imread() -> takes an image as an input
h, w, channels = img.shape
half = w//2
# this will be the first column
left_part = img[:, :half]
# [:,:half] means all the rows and
# all the columns upto index half
# this will be the second column

22
right_part = img[:, half:]
# [:,half:] means all the rows and all
# the columns from index half to the end
# cv2.imshow is used for displaying the image
cv2.imshow('Left part', left_part)
cv2.imshow('Right part', right_part)
# this is horizontal division
half2 = h//2
top = img[:half2, :]
bottom = img[half2:, :]
cv2.imshow('Top', top)
cv2.imshow('Bottom', bottom)
# saving all the images
# cv2.imwrite() function will save the image
# into your pc
cv2.imwrite('top.jpg', top)
cv2.imwrite('bottom.jpg', bottom)
cv2.imwrite('right.jpg', right_part)
cv2.imwrite('left.jpg', left_part)
cv2.waitKey(0)

Output

23
EXPERIMENT 8
Write a program to show rotation, scaling, and translation on an image

import cv2
import numpy as np
# Load the image
image = cv2.imread('images.jpg')
# Define rotation angle (in degrees), scaling factor, and translation values
rotation_angle = 45
scaling_factor = 1.5
translation_x = 50
translation_y = 50
# Get image dimensions

24
height, width = image.shape[:2]
# Define the rotation matrix
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), rotation_angle, 1)
# Perform rotation
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
# Perform scaling
scaled_image = cv2.resize(image, None, fx=scaling_factor, fy=scaling_factor)
# Perform translation
translation_matrix = np.float32([[1, 0, translation_x], [0, 1, translation_y]])
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
# Display the original, rotated, scaled, and translated images
cv2.imshow('Original Image', image)
cv2.imshow('Rotated Image', rotated_image)
cv2.imshow('Scaled Image', scaled_image)
cv2.imshow('Translated Image', translated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

25
EXPERIMENT 9
Read an image and extract and display low-level features such as edges, textures using

26
filtering techniques

import cv2
import numpy as np
# Read the image
image = cv2.imread('images.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Sobel filter for edge detection
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
sobel_edge = cv2.magnitude(sobel_x, sobel_y)
# Apply Laplacian filter for edge detection
laplacian_edge = cv2.Laplacian(image, cv2.CV_64F)
# Apply Gaussian blur for texture extraction
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
# Display the original image and extracted features
cv2.imshow('Original Image', image)
cv2.imshow('Sobel Edge Detection', np.uint8(sobel_edge))
cv2.imshow('Laplacian Edge Detection', np.uint8(laplacian_edge))
cv2.imshow('Gaussian Blurred Image', gaussian_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

27
EXPERIMENT 10
Write a program to blur and smoothing an image

import cv2
# Read the image

28
image = cv2.imread('images.jpg')
# Apply Gaussian blur
blurred_image = cv2.GaussianBlur(image, (15, 15), 0)
# Display the original and blurred images
cv2.imshow('Original Image', image)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

EXPERIMENT 11
Write a program to contour an image

import cv2
# Read the image in grayscale

29
image = cv2.imread('images.jpg', cv2.IMREAD_GRAYSCALE)
# Apply binary thresholding
ret, thresh = cv2.threshold(image, 127, 255, 0)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# Draw contours on a blank image
contour_image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 2)
# Display the original and contoured images
cv2.imshow('Original Image', image)
cv2.imshow('Contoured Image', contour_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

30
EXPERIMENT 12
Write a program to detect a face/s in an image.

import cv2
# Load the pre-trained face detection classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
# Read the input image
image = cv2.imread('images.jpg')
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale image
faces = face_cascade.detectMultiScale(gray_image, 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 result
cv2.imshow('Detected Faces', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

31
32

You might also like