0% found this document useful (0 votes)
51 views4 pages

Search Creators CG LAB Program-07

lab11
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)
51 views4 pages

Search Creators CG LAB Program-07

lab11
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/ 4

21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Subject Code:21CSL66

Subject: COMPUTER GRAPHICS AND IMAGE PROCESSING


LABORATORY

Program-07

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

Program

import cv2

import numpy as np

# Load the image

image_path = "image.jpg" # Replace with the path to your image

img = cv2.imread(image_path)

# Get the height and width of the image

height, width, _ = img.shape

# Split the image into four quadrants

up_left = img[0:height//2, 0:width//2]

up_right = img[0:height//2, width//2:width]

down_left = img[height//2:height, 0:width//2]

down_right = img[height//2:height, width//2:width]

Search Creators…. Page 1


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

# Create a blank canvas to display the quadrants

canvas = np.zeros((height, width, 3), dtype=np.uint8)

# Place the quadrants on the canvas

canvas[0:height//2, 0:width//2] = up_left

canvas[0:height//2, width//2:width] = up_right

canvas[height//2:height, 0:width//2] = down_left

canvas[height//2:height, width//2:width] = down_right

# Display the canvas

cv2.imshow("Image Quadrants", canvas)

cv2.waitKey(0)

cv2.destroyAllWindows()

Search Creators…. Page 2


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Output

Search Creators…. Page 3


21CSL66 | COMPUTER GRAPHICS AND IMAGE PROCESSING LABORATORY|

Explanation

1. The necessary libraries, cv2 (OpenCV) and numpy, are imported.


2. The path to the input image file is specified (image_path). In this case, it's set
to "image/atc.jpg", assuming the image file named "atc.jpg" is located in a
directory named "image" relative to the script's location.
3. The image is loaded using cv2.imread().
4. The height and width of the image are obtained from the shape attribute of the
image.
5. The image is split into four quadrants using NumPy array slicing:
6. up_left: Top-left quadrant
7. up_right: Top-right quadrant
8. down_left: Bottom-left quadrant
9. down_right: Bottom-right quadrant
10.A blank canvas with the same dimensions as the original image is created
using np.zeros(). This canvas will be used to display the four quadrants.
11.The four quadrants are placed on the canvas using NumPy array assignment.
12.The canvas containing the four quadrants is displayed using cv2.imshow().
13.The script waits for a key press (cv2.waitKey(0)) before closing the window.
14.Finally, all windows are closed using cv2.destroyAllWindows().

Search Creators…. Page 4

You might also like