Lab Manual CG and Image Processing 21 Scheme
Lab Manual CG and Image Processing 21 Scheme
Lab Manual CG and Image Processing 21 Scheme
)
Jain Institute of Technology, Davangere
(A UnitofJain Group ofInstitutions, Bengaluru)
# 323, Near Veereshwara Punyashrama, Avaragere, Davangere- 577005.
LAB MANUAL
#include <GL/glut.h>
#include <iostream>
using namespace std;
while (true) {
glBegin(GL_POINTS);
glVertex2i(x0, y0);
glEnd();
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;
glClear(GL_COLOR_BUFFER_BIT);
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");
glutDisplayFunc(display);
}
// Main function
int main(int argc, char** argv) {
initializeOpenGL(argc, argv);
glutMainLoop();
return 0;
}
Output:
Enter coordinates for x1 and y1
50 50
Enter coordinates for x1 and y1
500 500
Develop a program to demonstrate basic geometric operations on the 2D object using
python
import turtle
import math
# Draw a rectangle
draw_rectangle(-200, 0, 100, 50, "blue")
# Draw a circle
draw_circle(100, 100, 50, "red")
# Draw a cylinder
cylinder = draw_cylinder((2, 2, 0), 1, 10, color.red)
# Translate the cylinder
translate(cylinder, 0, -2, 0)
# Rotate the cylinder
rotate(cylinder, angle=30, axis=(1, 0, 0))
# Scale the cylinder
scale(cylinder, 1.5, 1.5, 1.5)
# Keep the 3D scene interactive
while True:
rate(30) # Set the frame rate to 30 frames per second
Develop a program to demonstrate 2D transformation on basic objects Using opengl
#include "stdafx.h"
#include <GL/glut.h>
#include <stdio.h>
// 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;
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;
}
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
}
// Main function
int main(int argc, char** argv) {
initializeOpenGL(argc, argv);
glutMainLoop();
return 0;
}
Develop a program to demonstrate 3D transformation on 3D objects Using opengl
#include "stdafx.h"
#include <GL/glut.h>
#include <stdio.h>
// Global variables
int width = 800;
int height = 600;
GLfloat rotationX = 0.0f;
GLfloat rotationY = 0.0f;
GLfloat scale = 1.0f;
// 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();
}
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();
}
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;
}
Develop a program to demonstrate Animation effects on simple objects.using openGL
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
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;
}
}
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)
Input image
Write a program to show rotation, scaling, and translation on an image.
import cv2
import numpy as np
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]))
cv2.waitKey(0)
cv2.destroyAllWindows()
Input image
Read an image and extract and display low-level features such as edges, textures using
filtering techniques.
import cv2
import numpy as np
if image is None:
print("Failed to load the image.")
else:
# Display the original image
cv2.imshow("Original Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input image
Write a program to blur and smoothing an image.
import cv2
if image is None:
print("Failed to load the image.")
else:
# Display the original image
cv2.imshow("Original Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input image
Write a program to contour an image.
import cv2
if image is None:
print("Failed to load the image.")
else:
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input image
Write a program to detect a face/s in an image.
import cv2
if image is None:
print("Failed to load the image.")
else:
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)