Program 7: Write a Program to read a digital image.
Split and display image
into 4 quadrants, up, down, right and left.
import cv2
img = [Link]("D:/DSATM/CG/CG&DIP/Lab_Images/Prog7_flower.jpg")
original= img
h, w, channels = [Link]
half_width = w//2
half_height = h//2
TopLeft_quadrant = img[:half_height, :half_width]
TopRight_quadrant = img[:half_height, half_width:]
BottomLeft_quadrant = img[half_height:, :half_width]
BottomRight_quadrant = img[half_height:, half_width:]
[Link]('originalImage',original)
[Link]('TopLeft_Quadrant', TopLeft_quadrant)
[Link]('TopRight_Quadrant', TopRight_quadrant)
[Link]('BottomLeft_Quadrant', BottomLeft_quadrant)
[Link]('BottomRight_Quadrant', BottomRight_quadrant)
[Link](0)
[Link]()
Output:
Explanation:
Importing the OpenCV Library
This line imports the OpenCV library, which provides tools for image processing and computer
vision tasks.
Reading the Image
[Link]() reads the image from the specified path.
img is a NumPy array that holds the pixel values of the image.
Storing the Original Image
This line simply copies the reference of img to original. Both img and original now point to the same
image data.
Getting Image Dimensions
[Link] returns the dimensions of the image: height (h), width (w), and the number of color
channels (channels).
Calculating Half Dimensions
w // 2 calculates half of the image's width.
h // 2 calculates half of the image's height.
These values will be used to divide the image into quadrants.
Dividing the Image into Quadrants
These lines use array slicing to extract each quadrant of the image:
TopLeft_quadrant contains the top-left quarter of the image.
TopRight_quadrant contains the top-right quarter of the image.
BottomLeft_quadrant contains the bottom-left quarter of the image.
BottomRight_quadrant contains the bottom-right quarter of the image.
Displaying the Images
[Link]() creates a window to display an image.
The first argument is the window title.
The second argument is the image data to be displayed.
This code displays the original image and each of the four quadrants in separate windows.
Waiting for User Input and Closing Windows
[Link](0) waits indefinitely for a key press. The argument 0 means wait forever until a key is
pressed.
[Link]() closes all the windows opened by OpenCV.
This script divides an image into four quadrants and displays them along with the original
image, illustrating basic image manipulation and display capabilities of OpenCV.