CG&IP LAB Manual (21CSL66) Updated
CG&IP LAB Manual (21CSL66) Updated
LAB MANUAL
On
Anitha L
Assistant Professor
Computer Science and Engineering Department
East West College of Engineering, Yelahanka New Town, Bangalore - 560064
Computer Graphics and Image Processing Lab(21CSL66)
2|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
3|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
4|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
glVertex2i(x,y);
}
glEnd();
glFlush();
}
void Init()
{
glClearColor(1.0,1.0,1.0,0);
glColor3f(0.0,0.0,0.0);
glPointSize(4.0);
glViewport(0,0,50,50);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,50,0,50);
}
int main(int argc,char **argv)
{
printf("enter two points for lineBresenham drawing:\n");
printf("\n enter point1(X1,Y1):");
scanf("%d%d",&X1,&Y1);
printf("\n enter point2(X2,Y2):");
scanf("%d%d",&X2,&Y2);
glutInit(&argc,argv);
5|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,400);
glutInitWindowPosition(0,0);
glutCreateWindow("LineBresenham");
Init();
glutDisplayFunc(LineBres);
glutMainLoop();
}
Output:
6|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
7|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
}
else(triangle(a,b,c)); /* draw triangle at end of recursion */
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
divide_triangle(v[0], v[1], v[2], n);
glFlush();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor (1.0, 1.0, 1.0, 1.0);
glColor3f(0.0,0.0,0.0);
}
void main(int argc, char **argv)
{
printf(“Enter number of Subdivisions : “);
scanf(“%d”,&n);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutCreateWindow(“2D triangle”);
glutDisplayFunc(display);
myinit();
glutMainLoop();}
8|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
Output:
9|Pag e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
10 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
11 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w<=h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0,
10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0,
10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
int i = 0;
printf("Enter value of N:");
scanf("%d", &i);
n = i;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D tetrahedron ");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor (1.0, 1.0, 1.0, 1.0);
glutMainLoop();
}
12 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
Output:
13 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
GLfloat t[3][3]={{10.0,30.0,20.0},{20.0,20.0,40.0},{1.0,1.0,1.0}};
GLfloat rotatemat[3][3]={{0},{0},{0}};
GLfloat result[3][9]={{0},{0},{0}};
GLfloat xr=10.0;
GLfloat yr=20.0;
GLfloat theta;
GLint h;
void multiply(){
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<9;j++){
result[i][j]=0;
for(k=0;k<3;k++)
result[i][j]=result[i][j]+rotatemat[i][k]*t[k][j];
}
}
void rotate_about_origin(){
rotatemat[0][0]=cos(theta);
rotatemat[0][1]=-sin(theta);
rotatemat[0][2]=0;
rotatemat[1][0]=sin(theta);
rotatemat[1][1]=cos(theta);
rotatemat[1][2]=0;
rotatemat[2][0]=0;
14 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
rotatemat[2][1]=0;
rotatemat[2][2]=1;
multiply();
}
void rotate_about_fixed_point(){
GLfloat m,n;
m=xr*(1-cos(theta))+yr*sin(theta);
n=yr*(1-cos(theta))-xr*sin(theta);
rotatemat[0][0]=cos(theta);
rotatemat[0][1]=-sin(theta);
rotatemat[0][2]=m;
rotatemat[1][0]=sin(theta);
rotatemat[1][1]=cos(theta);
rotatemat[1][2]=n;
rotatemat[2][0]=0;
rotatemat[2][1]=0;
rotatemat[2][2]=1;
multiply();
}
void draw_triangle(){
glLineWidth(10);
glBegin(GL_LINE_LOOP);
glColor3f(1.0,0.0,0.0);
glVertex2f(t[0][0],t[1][0]);
glColor3f(0.0,1.0,0.0);
glVertex2f(t[0][1],t[1][1]);
glColor3f(0.0,0.0,1.0);
glVertex2f(t[0][2],t[1][2]);
glEnd();
glFlush();
15 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
}
void draw_rotated_triangle(){
glLineWidth(10);
glBegin(GL_LINE_LOOP);
glColor3f(1.0,0.0,0.0);
glVertex2f(result[0][0],result[1][0]);
glColor3f(0.0,1.0,0.0);
glVertex2f(result[0][1],result[1][1]);
glColor3f(0.0,0.0,1.0);
glVertex2f(result[0][2],result[1][2]);
glEnd();
glFlush();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
if(ch==1){
draw_triangle();
rotate_about_origin();
draw_rotated_triangle();
glFlush();
}
if(ch==2){
draw_triangle();
rotate_about_fixed_point();
draw_rotated_triangle();
glFlush();
}
}
void myinit(){
glClearColor(1.0,1.0,1.0,1.0);
16 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-50.0,50.0,-50.0,50.0);
}
int main(int argc,char** argv){
printf("***Rotation***\n1.Rotation about origin\n2.Rotation about a fixed point(xr,yr)\n");
printf("Enter choice\n");
scanf("%d",&ch);
printf("Enter the rotation angle\n");
scanf("%f",&theta);
theta=theta*(3.14/180);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Triangle Rotation\n");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
17 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
Output:
Output 2:
18 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
19 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
polygon(0,1,5,4);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Update viewer position in modelview matrix */
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/* rotate cube */
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube(); /* draw the rotated color cube */
glFlush();
glutSwapBuffers();
}
20 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
21 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
Output:
22 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
#include<windows.h>
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glutSwapBuffers();
}
// If the triangle goes beyond the right edge of the window, reset its position
if (trianglePosX > 1.1f)
trianglePosX = -0.5f;
void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background color
}
23 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
init();
glutDisplayFunc(display);
glutTimerFunc(16, update, 0); // Call update() after 16 milliseconds
glutMainLoop();
return 0;
}
OUTPUT:
24 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read the image
img = cv2.imread("C:\\users\\hp\\saved pictures\\img.jpg")
# Get the height and width of the image
height, width = img.shape[:2]
# Split the image into four quadrants
quad1 = img[:height//2, :width//2]
quad2 = img[:height//2, width//2:]
quad3 = img[height//2:, :width//2]
quad4 = img[height//2:, width//2:]
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(quad1)
plt.title("1")
plt.axis("off")
plt.subplot(1, 2, 2)
plt.imshow(quad2)
plt.title("2")
plt.axis("off")
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(quad3)
plt.title("3")
plt.axis("off")
plt.subplot(1, 2, 2)
25 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
plt.imshow(quad4)
plt.title("4")
plt.axis("off")
plt.show()
Output:
26 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
27 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
Output:
Enter the degree of Rotation: 45
Enter the zooming factor: 200
How many pixels you want the image to be translated horizontally? 100
How many pixels you want the image to be translated vertically? 100
28 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
import cv2
import numpy as np
# Load the image
image_path = "C:\\Users\\HP\\Pictures\\23.jpg" # Replace with the path to your image
img = cv2.imread(image_path)
# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Edge detection
edges = cv2.Canny(gray, 100, 200) # Use Canny edge detector
# Texture extraction
kernel = np.ones((5, 5), np.float32) / 25 # Define a 5x5 averaging kernel
texture = cv2.filter2D(gray, -1, kernel) # Apply the averaging filter for texture extraction
# Display the original image, edges, and texture
cv2.imshow("Original Image", img)
cv2.imshow("Edges", edges)
cv2.imshow("Texture", texture)
29 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
Output:
30 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
31 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
output:
32 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
image_path ="C:\\Users\\HP\\Pictures\\IMG.jpg"
image = cv2.imread(image_path)
# Convert the image to grayscale (contours work best on binary images)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding (you can use other techniques like Sobel edges)
_, binary_image = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# Draw all contours on the original image
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
# Display the result
cv2.imshow("Contours", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
33 | P a g e
CSE Dept. ,EWCE
Computer Graphics and Image Processing Lab(21CSL66)
Output:
34 | P a g e
CSE Dept. ,EWCE