0% found this document useful (0 votes)
33 views6 pages

CG & Ip Lab

Uploaded by

sachin m
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)
33 views6 pages

CG & Ip Lab

Uploaded by

sachin m
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/ 6

COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY (21CSL66)

PROGRAM NO.7: Write a Program to read a digital Image. Split and display into 4 quadrants, up,
down, right and left.

# import opencv
import cv2
import numpy as np
import os

# Open an image file


img = cv2.imread('test.jpg')

# display the image using cv2


cv2.imshow('image', img)

cv2.waitKey(0)

# split the image into four quadrants


height, width = img.shape[:2]
quadrant1 = img[0:height//2, 0:width//2]
quadrant2 = img[0:height//2, width//2:width]
quadrant3 = img[height//2:height, 0:width//2]
quadrant4 = img[height//2:height, width//2:width]

cv2.destroyAllWindows()
# display the quadrants
cv2.imshow('quadrant1', quadrant1)
# cv2.waitKey(0)
cv2.imshow('quadrant2', quadrant2)
# cv2.waitKey(0)
cv2.imshow('quadrant3', quadrant3)
# cv2.waitKey(0)
cv2.imshow('quadrant4', quadrant4)
cv2.waitKey(0)

cv2.destroyAllWindows()

Dept of CSE, JCE-Belagavi Page 1


COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY (21CSL66)

PROGRAM NO.8: Write a Program to show rotation, scaling, and translation on an image.

import cv2
import numpy as np

# Open an image file


img = cv2.imread('test.jpg')

# rotate the image by 30 degrees


rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)
rotated_img = cv2.warpAffine(img, M, (cols, rows))

# display the rotated image


cv2.imshow('rotated image', rotated_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

# translate the image by 200 pixels in both x and y directions


rows, cols = img.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 200]])
translated_img = cv2.warpAffine(img, M, (cols, rows))

# display the translated image


cv2.imshow('translated image', translated_img)

cv2.waitKey(0)

# Scale the image by 1.5 in both x and y directions


rows, cols = img.shape[:2]
M = np.float32([[1.5, 0, 0], [0, 1.5, 0]])
scaled_img = cv2.warpAffine(img, M, (cols, rows))

# display the scaled image


cv2.imshow('scaled image', scaled_img)

cv2.waitKey(0)

Dept of CSE, JCE-Belagavi Page 2


COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY (21CSL66)

PROGRAM NO.9: Read an image and extract and display low-level features such as edges, textures
using filtering techniques.

import cv2

# Load an image using 'imread' specifying the path to image


image = cv2.imread('test.jpg')

# Display the image using 'imshow'


cv2.imshow('image', image)

# Wait for a key stroke; the argument is the time in milliseconds


cv2.waitKey(0)
cv2.destroyWindow('image')

# extract edges from the image


edges = cv2.Canny(image, 100, 200)

# display the edges


cv2.imshow('edges', edges)
# Wait for a key stroke; the argument is the time in milliseconds
cv2.waitKey(0)
cv2.destroyWindow('edges')

# extract texture from the image


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# display the texture


cv2.imshow('texture', gray)

# Wait for a key stroke; the argument is the time in milliseconds


cv2.waitKey(0)

Dept of CSE, JCE-Belagavi Page 3


COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY (21CSL66)

PROGRAM NO.10: Write a program to blur and smoothing an image.

import cv2
import numpy as np
# Load an image using 'imread' specifying the path to image
image = cv2.imread('test.jpg')

# Blur the image


blurred = cv2.GaussianBlur(image, (3, 3), 0)

# Display the image using 'imshow'


cv2.imshow('blurred image', blurred)

# Wait for a key stroke; the argument is the time in milliseconds


cv2.waitKey(0)

# smoothen the image


kernel = np.ones((5, 5), np.float32)/25
smoothed = cv2.filter2D(image, -1, kernel)

# display the smoothed image


cv2.imshow('smoothed image', smoothed)

# Wait for a key stroke; the argument is the time in milliseconds


cv2.waitKey(0)

Dept of CSE, JCE-Belagavi Page 4


COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY (21CSL66)

PROGRAM NO.11: Write a program to contour an image.

import cv2
import numpy as np

# Load an image using 'imread' specifying the path to image


image = cv2.imread('test.jpg')

# Create contours from the image


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Draw the contours


cv2.drawContours(image, contours, -1, (0, 255, 0), 3)

# Display the image using 'imshow'


cv2.imshow('contours', image)

# Wait for a key stroke; the argument is the time in milliseconds


cv2.waitKey(0)

Dept of CSE, JCE-Belagavi Page 5


COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY (21CSL66)

PROGRAM NO.12: Write a program to detect a face/s in an image.

import cv2

# detect face in an image


def detect_face(image):
# load pre-trained data on face frontals from opencv
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5,
minSize=(30, 30))
return faces

# draw rectangle around the faces


def draw_rectangle(image, faces):
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

# read image
image = cv2.imread('face.jpg')
# detect face
faces = detect_face(image)
# draw rectangle around the faces
draw_rectangle(image, faces)
# display the image
cv2.imshow('Face Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Dept of CSE, JCE-Belagavi Page 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy