sem3
sem4
sem5
sem6
sem7
MADL
SSL
CGL
#include <GL/glut.h> #include <iostream> int x1, y1, x2, y2 ; void plot (int x, int y){ glBegin(GL_POINTS); glVertex2i (x, y); glEnd (); } void bresenhamLine (){ int dx = abs ( x2-x1); int dy = abs ( y2-y1); int slope=dy > dx ? 1:0 ; if (slope) { std :: swap (x1,y1); std ::swap(x2, y2); std :: swap (dx, dy); } int x =x1 , y =y1; int p = 2 * dy - dx; plot(x,y); for (int k = 0 ;k < dx;++k ){ if (p < 0) { if (!slope) x++; else y++; p+= 2*dy; } else { x++; if (!slope) y++; else x++; p+= 2*(dy - dx) ; } plot(x, y); } } void display(){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0,1.0); bresenhamLine(); glFlush(); } void init() { glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D (0, 500, 0,500); } int main (int argc, char ** argv) { std::cout << "Enter the starting point (x1, y1):"; std:: cin >> x1 >>y1; std::cout <<"Enter the ending point (x2, y2):"; std:: cin >> x2 >> y2; glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("Bresenham's Line Drawing"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }
Copy
PROGRAM 1
#include <GL/glut.h> #include <iostream> float squareSize=50.0f; float squarePosX=250.0f, squarePosY=250.0f; float angle=0.0f, scale=1.0f; void drawSquare(){ glPushMatrix(); glTranslatef(squarePosX,squarePosY,0.0f); glRotatef(angle,1.0f,0.0f,0.0f); glScalef(scale,scale,1.0f); glBegin(GL_QUADS); glColor3f(1.0f,0.0f,0.0f); glVertex2f(-squareSize/2,-squareSize/2); glVertex2f(squareSize/2,-squareSize/2); glVertex2f(squareSize/2,squareSize/2); glVertex2f(-squareSize/2,squareSize/2); glEnd(); glPopMatrix(); } void display(){ glClear(GL_COLOR_BUFFER_BIT); drawSquare(); glFlush(); } void init(){ glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PROJECTION); gluOrtho2D(0,500,0,500); } void keyboard(unsigned char key, int x, int y){ switch(key){ case 'w': squarePosY+=10.0f; break; case 's': squarePosY-=10.0f; break; case 'a': squarePosX-=10.0f; break; case 'd': squarePosX+=10.0f; break; case 'r': angle+=10.0f; break; case 'e': angle-=10.0f; break; case '+': scale+=0.1f; break; case '-': scale-=0.1f; break; default: break; } glutPostRedisplay(); } int main(int argc, char **argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB); glutInitWindowSize(500,500); glutCreateWindow("Geometric operations on 2D object"); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
Copy
PROGRAM 2
#include <GL/glut.h> #include <iostream> // Cube properties GLfloat cubeSize = 1.0f; GLfloat cubePosX = 0.0f; GLfloat cubePosY = 0.0f; GLfloat cubePosZ = -5.0f; GLfloat angleX = 0.0f; GLfloat angleY = 0.0f; GLfloat scale = 1.0f; void drawCube() { glPushMatrix(); glTranslatef(cubePosX, cubePosY, cubePosZ); glRotatef(angleX, 1.0f, 0.0f, 0.0f); glRotatef(angleY, 0.0f, 1.0f, 0.0f); glScalef(scale, scale, scale); glBegin(GL_QUADS); // Front face glColor3f(1.0f, 0.0f, 0.0f); // Red glVertex3f(-cubeSize, -cubeSize, cubeSize); glVertex3f(cubeSize, -cubeSize, cubeSize); glVertex3f(cubeSize, cubeSize, cubeSize); glVertex3f(-cubeSize, cubeSize, cubeSize); // Back face glColor3f(0.0f, 1.0f, 0.0f); // Green glVertex3f(-cubeSize, -cubeSize, -cubeSize); glVertex3f(-cubeSize, cubeSize, -cubeSize); glVertex3f(cubeSize, cubeSize, -cubeSize); glVertex3f(cubeSize, -cubeSize, -cubeSize); // Top face glColor3f(0.0f, 0.0f, 1.0f); // Blue glVertex3f(-cubeSize, cubeSize, -cubeSize); glVertex3f(-cubeSize, cubeSize, cubeSize); glVertex3f(cubeSize, cubeSize, cubeSize); glVertex3f(cubeSize, cubeSize, -cubeSize); // Bottom face glColor3f(1.0f, 1.0f, 0.0f); // Yellow glVertex3f(-cubeSize, -cubeSize, -cubeSize); glVertex3f(cubeSize, -cubeSize, -cubeSize); glVertex3f(cubeSize, -cubeSize, cubeSize); glVertex3f(-cubeSize, -cubeSize, cubeSize); // Right face glColor3f(1.0f, 0.0f, 1.0f); // Magenta glVertex3f(cubeSize, -cubeSize, -cubeSize); glVertex3f(cubeSize, cubeSize, -cubeSize); glVertex3f(cubeSize, cubeSize, cubeSize); glVertex3f(cubeSize,-cubeSize, cubeSize); // Left face glColor3f(0.0f, 1.0f, 1.0f); // Cyan glVertex3f(-cubeSize, -cubeSize, -cubeSize); glVertex3f(-cubeSize, -cubeSize, cubeSize); glVertex3f(-cubeSize, cubeSize, cubeSize); glVertex3f(-cubeSize, cubeSize, -cubeSize); glEnd(); glPopMatrix(); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawCube(); glutSwapBuffers(); } void init() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glMatrixMode(GL_PROJECTION); gluPerspective(45.0f, 1.0f, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'w': cubePosY += 0.1f; break; case 's': cubePosY -= 0.1f; break; case 'a': cubePosX -= 0.1f; break; case 'd': cubePosX += 0.1f; break; case 'q': cubePosZ -= 0.1f; break; case 'e': cubePosZ += 0.1f; break; case 'r': angleX += 5.0f; break; case 't': angleY += 5.0f; break; case '+': scale += 0.1f; break; case '-': scale -= 0.1f; break; default: break; } glutPostRedisplay(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("Geometric Operations on 3D Objects"); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
Copy
PROGRAM 3
#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; // 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) { 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 '-': 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; }
Copy
PROGRAM 44
#include <GL/glut.h> #include <stdio.h> 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); // 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: 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) { 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); glutMainLoop(); return 0; }
Copy
PROGRAM 5
#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; 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; }
Copy
PROGRAM 6
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 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)
Copy
PROGRAM 7
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 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()
Copy
PROGRAM 8
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()
Copy
PROGRAM 9
import cv2 # Read the image 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()
Copy
PROGRAM 10
import cv2 # Read the image in grayscale 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()
Copy
PROGRAM 11
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()
Copy
PROGRAM 12
Would you like to upload Programs?
no
yes