CG Manual 21 Scheme

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Question 1

Bresenham’s line drawing technique


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

#include <GL/glut.h>
#include <iostream>
using namespace std;

// Bresenham's line drawing algorithm


void drawLine(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;

while (true) {
glBegin(GL_POINTS);
glVertex2i(x0, y0);
glEnd();

if (x0 == x1 && y0 == y1) break;

int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}
}

// OpenGL display callback


void display() {
int x1, x2, y1, y2;

cout << "Enter coordinates for x1 and y1" << endl;


cin >> x1 >> y1;
cout << "Enter coordinates for x2 and y2" << endl;
cin >> x2 >> y2;

glClear(GL_COLOR_BUFFER_BIT);
// Draw line using Bresenham's algorithm
glColor3f(1.0f, 1.0f, 1.0f);
drawLine(x1, y1, x2, y2);

glFlush();
}

// OpenGL initialization
void initializeOpenGL(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Bresenham's Line Algorithm");

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 800, 0, 600);

glutDisplayFunc(display);
}

// Main function
int main(int argc, char** argv) {

initializeOpenGL(argc, argv);

glutMainLoop();

return 0;
}

In this program:

We include for GLUT functions and OpenGL headers.


The drawLine function implements Bresenham’s line drawing algorithm, just like in the previous
example.
The display function is the GLUT display callback, where we clear the screen and draw the
line using drawLine.
The initializeOpenGL function sets up the GLUT window and OpenGL context. It specifies the
display mode, window size, and title. It also sets up the projection matrix and registers the
display callback.
In main, we initialize the OpenGL context using initializeOpenGL and start the GLUT main loop
with glutMainLoop().
Compile this program with a C++ compiler that links against GLUT and OpenGL libraries.
When you run the compiled program, it should open an OpenGL window displaying a white line
drawn using Bresenham’s algorithm from (50, 50) to (500, 500) given as input from the user.
Adjust the window size and line coordinates as needed.
Question 2
Basic geometric operations on the 2D object
Develop a program to demonstrate basic geometric operations on the 2D object

C++ Program
#include <GL/glut.h>
#include <iostream>

// Global variables
int width = 800;
int height = 600;
float rectWidth = 100.0f;
float rectHeight = 50.0f;
float rectPositionX = (width - rectWidth) / 2.0f;
float rectPositionY = (height - rectHeight) / 2.0f;
float rotationAngle = 0.0f;
float scaleFactor = 1.0f;

// Function to draw a rectangle


void drawRectangle(float x, float y, float width, float height) {
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}

// Function to handle display


void display() {
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Apply transformations
glTranslatef(rectPositionX, rectPositionY, 0.0f);
glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f);
glScalef(scaleFactor, scaleFactor, 1.0f);

// Draw rectangle
glColor3f(1.0f, 0.0f, 0.0f); // Red color
drawRectangle(0.0f, 0.0f, rectWidth, rectHeight);

glFlush();
}

// Function to handle keyboard events


void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 't':
// Translate the rectangle by 10 units in the x-direction
rectPositionX += 10.0f;
break;
case 'r':
// Rotate the rectangle by 10 degrees clockwise
rotationAngle += 10.0f;
break;
case 's':
// Scale the rectangle by 10% (scaleFactor = 1.1f)
scaleFactor *= 1.1f;
break;
case 'u':
// Reset transformations (translate back to center, reset rotation and scaling)
rectPositionX = (width - rectWidth) / 2.0f;
rectPositionY = (height - rectHeight) / 2.0f;
rotationAngle = 0.0f;
scaleFactor = 1.0f;
break;
case 27: // Escape key to exit
exit(0);
break;
}

glutPostRedisplay(); // Trigger a redraw


}

// Function to initialize OpenGL


void initializeOpenGL(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("Geometric Operations in 2D");

glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
}
// Main function
int main(int argc, char** argv) {
initializeOpenGL(argc, argv);
glutMainLoop();
return 0;
}

In this program:
 The glMatrixMode(GL_PROJECTION) and glLoadIdentity() in
the initializeOpenGL function ensure that the projection matrix is properly set up.
 We define a simple drawRectangle function that draws a rectangle using OpenGL
primitives.
 The display function is responsible for rendering the scene. It applies translation
(glTranslatef), rotation (glRotatef), and scaling (glScalef) transformations to the
rectangle before drawing it.
 The glTranslatef, glRotatef, and glScalef calls in the display function are applied
correctly within the GL_MODELVIEW matrix mode.
 The glColor3f(1.0f, 0.0f, 0.0f) call sets the drawing color to red (RGB values: 1.0, 0.0,
0.0).
 Keyboard input is handled by the keyboard function. Pressing keys ‘t’, ‘r’, ‘s’, and ‘u’
triggers translation, rotation, scaling, and reset operations, respectively.
 The initializeOpenGL function sets up the GLUT window and initializes OpenGL
settings.
 In main, we initialize the OpenGL context using initializeOpenGL and start the GLUT
main loop.
To compile and run this program, make sure you have GLUT installed and set up in your
development environment. Compile the program using a C++ compiler that links against GLUT
and OpenGL libraries (e.g., using -lglut -lGL -lGLU flags). When you run the compiled
program, a window will appear displaying a red rectangle. Use the keyboard keys (‘t’, ‘r’, ‘s’,
‘u’) to perform translation, rotation, scaling, and reset operations on the rectangle interactively.
Question 3
Basic geometric operations on the 3D object
Develop a program to demonstrate basic geometric operations on the 3D object

To demonstrate basic geometric operations on a 3D object using GLUT (OpenGL Utility


Toolkit), we can create a program that allows you to perform translation, rotation, and scaling
operations interactively on a simple 3D object (e.g., a cube). Below is an example program
written in C++ using GLUT that demonstrates these geometric transformations on a 3D cube.

#include <GL/glut.h>
#include <iostream>

// Global variables
int width = 800;
int height = 600;
GLfloat rotationX = 0.0f;
GLfloat rotationY = 0.0f;
GLfloat scale = 1.0f;

// Function to draw a cube


void drawCube() {
glBegin(GL_QUADS);

// Front face
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);

// Back face
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);

// Top face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);

// Bottom face
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);

// Right face
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);

// Left face
glColor3f(0.0f, 1.0f, 1.0f); // Cyan
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);

glEnd();
}
// Function to handle display
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Apply transformations
glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(rotationX, 1.0f, 0.0f, 0.0f);
glRotatef(rotationY, 0.0f, 1.0f, 0.0f);
glScalef(scale, scale, scale);

// Draw cube
drawCube();

glutSwapBuffers();
}

// Function to handle keyboard events


void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'x':
rotationX += 5.0f;
break;
case 'X':
rotationX -= 5.0f;
break;
case 'y':
rotationY += 5.0f;
break;
case 'Y':
rotationY -= 5.0f;
break;
case '+':
scale += 0.1f;
break;
case '-':
if (scale > 0.1f)
scale -= 0.1f;
break;
case 27: // Escape key to exit
exit(0);
break;
}

glutPostRedisplay(); // Trigger a redraw


}

// Function to initialize OpenGL


void initializeOpenGL(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("Geometric Operations in 3D");

glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100.0f);

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
}

// Main function
int main(int argc, char** argv) {
initializeOpenGL(argc, argv);
glutMainLoop();
return 0;
}

In this program:

 We define a drawCube function that draws a simple cube using GL_QUADS for each face.
 The display function sets up the model-view matrix and applies translation (glTranslatef),
rotation (glRotatef), and scaling (glScalef) transformations to the cube before drawing it.
 Keyboard input is handled by the keyboard function. Pressing keys ‘x’, ‘X’, ‘y’, and ‘Y’ rotates
the cube around the X and Y axes, while ‘+’ and ‘-‘ keys scale the cube up and down,
respectively.
 The initializeOpenGL function sets up the GLUT window, initializes OpenGL settings
(including depth testing for 3D rendering), and registers display and keyboard callback
functions.
 The main function initializes the OpenGL context using initializeOpenGL and starts the GLUT
main loop.

Compile and run this program, and you should see a window displaying a rotating and scalable cube.
Use the keyboard keys (‘x’, ‘X’, ‘y’, ‘Y’, ‘+’, ‘-‘) to perform rotation and scaling operations on the cube
interactively. The modifications made in this version should ensure that the cube is visible and that
the geometric transformations work as expected in a 3D context.
Question 6

Animation effects
Develop a program to demonstrate Animation effects on simple objects.

C++ Program

#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>

const double TWO_PI = 6.2831853;


GLsizei winWidth = 500, winHeight = 500;
GLuint regHex;
static GLfloat rotTheta = 0.0;

// Initial display window size.


// Define name for display list.
class scrPt {
public:
GLint x, y;
};

static void init(void)


{
scrPt hexVertex;
GLdouble hexTheta;
GLint k;
glClearColor(1.0, 1.0, 1.0, 0.0);
/* Set up a display list for a red regular hexagon.
* Vertices for the hexagon are six equally spaced
* points around the circumference of a circle.
*/
regHex = glGenLists(1);
glNewList(regHex, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
for(k = 0; k < 6; k++) {
hexTheta = TWO_PI * k / 6;
hexVertex.x = 150 + 100 * cos(hexTheta);
hexVertex.y = 150 + 100 * sin(hexTheta);
glVertex2i(hexVertex.x, hexVertex.y);
}
glEnd( );
glEndList( );
}

void displayHex(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix( );
glRotatef(rotTheta, 0.0, 0.0, 1.0);
glCallList(regHex);
glPopMatrix( );
glutSwapBuffers( );
glFlush( );
}

void rotateHex(void)
{
rotTheta += 3.0;
if(rotTheta > 360.0)
rotTheta -= 360.0;
glutPostRedisplay( );
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0, 0,(GLsizei) newWidth,(GLsizei) newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluOrtho2D(-320.0, 320.0, -320.0, 320.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
glClear(GL_COLOR_BUFFER_BIT);
}
void mouseFcn(GLint button, GLint action, GLint x, GLint y)
{
switch(button) {
case GLUT_MIDDLE_BUTTON:
// Start the rotation.
if(action == GLUT_DOWN)
glutIdleFunc(rotateHex);
break;
case GLUT_RIGHT_BUTTON:
// Stop the rotation.
if(action == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(150, 150);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Animation Example");
init( );
glutDisplayFunc(displayHex);
glutReshapeFunc(winReshapeFcn);
glutMouseFunc(mouseFcn);
glutMainLoop( );
return 0;
}
OpenCV
Question 7

Split and Display Image

Write a Program to read a digital image. Split and display image into 4
quadrants, up, down, right and left.

import cv2

# Function to split the image into four quadrants


def split_image(image):
height, width, _ = image.shape
half_height = height // 2
half_width = width // 2

# Split the image into four quadrants


top_left = image[:half_height, :half_width]
top_right = image[:half_height, half_width:]
bottom_left = image[half_height:, :half_width]
bottom_right = image[half_height:, half_width:]

return top_left, top_right, bottom_left, bottom_right

# Function to display images


def display_images(images, window_names):
for img, name in zip(images, window_names):
cv2.imshow(name, img)

print("Press any key to terminate.")


cv2.waitKey(0)
cv2.destroyAllWindows()

# Read the image


image_path = "image.jpg" # Replace "image.jpg" with the path to your image
image = cv2.imread(image_path)

if image is None:
print("Failed to load the image.")
else:
# Split the image into quadrants
top_left, top_right, bottom_left, bottom_right = split_image(image)

# Display the quadrants


display_images([top_left, top_right, bottom_left, bottom_right],
["Top Left", "Top Right", "Bottom Left", "Bottom Right"])
Program-08
8.Write a program to show rotation, scaling, and translation on an image

import cv2
import numpy as np

# Read the image


image_path = "img8.jpg" # Replace "your_image.jpg" with the path to your image
image = cv2.imread(image_path)

if image is None:
print("Failed to load the image.")
else:
# Display the original image
cv2.imshow("Original Image", image)

# Rotation
angle = 45 # Rotation angle in degrees
center = (image.shape[1] // 2, image.shape[0] // 2)
# Center of rotation
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
# Rotation matrix
rotated_image = cv2.warpAffine(image, rotation_matrix, (image.shape[1], image.shape[0]))

# Scaling
scale_factor = 0.5 # Scaling factor (0.5 means half the size)
scaled_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor)

# Translation
translation_matrix = np.float32([[1, 0, 100], [0, 1, -50]])
# Translation matrix (100 pixels right, 50 pixels up)
translated_image = cv2.warpAffine(image, translation_matrix, (image.shape[1],
image.shape[0]))

# Display the transformed images


cv2.imshow("Rotated Image", rotated_image)
cv2.imshow("Scaled Image", scaled_image)
cv2.imshow("Translated Image", translated_image)

cv2.waitKey(0)
cv2.destroyAllWindows()
Question 9

Feature Extraction
Read an image and extract and display low-level features such as edges, textures using filtering
techniques.

import cv2
import numpy as np

# Read the image


image_path = "gandhi.jpg" # Replace "your_image.jpg" with the path to your image
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if image is None:
print("Failed to load the image.")
else:
# Display the original image
cv2.imshow("Original Image", image)

# Apply Sobel filter to extract edges


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_edges = cv2.magnitude(sobel_x, sobel_y)
sobel_edges = cv2.normalize(sobel_edges, None, 0, 255, cv2.NORM_MINMAX,
dtype=cv2.CV_8U)

# Display edges extracted using Sobel filter


cv2.imshow("Edges (Sobel Filter)", sobel_edges)

# Apply Laplacian filter to extract edges


laplacian_edges = cv2.Laplacian(image, cv2.CV_64F)
laplacian_edges = cv2.normalize(laplacian_edges, None, 0, 255, cv2.NORM_MINMAX,
dtype=cv2.CV_8U)

# Display edges extracted using Laplacian filter


cv2.imshow("Edges (Laplacian Filter)", laplacian_edges)

# Apply Gaussian blur to extract textures


gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)

# Display image with Gaussian blur


cv2.imshow("Gaussian Blur", gaussian_blur)

cv2.waitKey(0)
cv2.destroyAllWindows()
Question 10

Blur and Smoothing


Write a program to blur and smoothing an image.

Blurring and smoothing are common image processing techniques used to reduce noise and detail
in images. OpenCV provides various functions to perform blurring and smoothing operations. Below
is a Python program using OpenCV to read an image, apply blur and smoothing filters, and display
the results:

import cv2

# Read the image


image_path = "art.png" # Replace "your_image.jpg" with the path to your image
image = cv2.imread(image_path)

if image is None:
print("Failed to load the image.")
else:
# Display the original image
cv2.imshow("Original Image", image)

# Apply blur to the image


blur_kernel_size = (5, 5) # Kernel size for blur filter
blurred_image = cv2.blur(image, blur_kernel_size)

# Display the blurred image


cv2.imshow("Blurred Image", blurred_image)

# Apply Gaussian blur to the image


gaussian_blur_kernel_size = (5, 5) # Kernel size for Gaussian blur filter
gaussian_blurred_image = cv2.GaussianBlur(image, gaussian_blur_kernel_size, 0)

# Display the Gaussian blurred image


cv2.imshow("Gaussian Blurred Image", gaussian_blurred_image)

# Apply median blur to the image


median_blur_kernel_size = 5 # Kernel size for median blur filter (should be odd)
median_blurred_image = cv2.medianBlur(image, median_blur_kernel_size)

# Display the median blurred image


cv2.imshow("Median Blurred Image", median_blurred_image)

cv2.waitKey(0)
cv2.destroyAllWindows()

You might also like