Divide an Image Into Equal Parts Using OpenCV in Python
We’ll walk over how to divide a picture into equal halves using Python and OpenCV in this post. The Python package known as OpenCV enables us to utilize a variety of image-processing approaches, such as image classification, face detection, and tracking. Analysis of surveillance systems can benefit from it as well. Considering images are composed of pixels, when we divide an image, we often divide its pixels.
Method:
- Pick the picture.
- Extract the desired image’s dimensions and store them in the variable.
- Use list slicing.
What is list slicing, and how will we apply it to this situation?
Images are nothing more than a binary representation of data or a matrix of color intensity. The intended output will be produced by dividing a matrix. List slicing will allow us to reach all the parts of that matrix.
Python Code to divide an image Horizontally using OpenCV
import cv2 image= cv2.imread('image1.png') # cv2.imread() = will take the image as input height, width, channels = image.shape half_height = height//2 top_section = image[:half_height, :] bottom_section = image[half_height:, :] cv2.imshow('Top', top_section) #cv2.imshow is used for displaying the image cv2.imshow('Bottom', bottom_section) """ saving all the images cv2.imwrite() function will save the image into your system """ cv2.imwrite('top.jpg', top_section) cv2.imwrite('bottom.jpg', bottom_section) cv2.waitKey(0)
Image used
Output:
Explanation of the horizontal image division Python code
- Importing all the necessary libraries. Example,
import cv2
- OpenCV reads the image using the
imread
function. We use it to read the provided image. - Then we will extract the dimensions of the image provided.
- We will then try to divide the height as while dividing horizontally we need to divide the height.
- While dividing the height we used floor division, which rounds the height to the nearest whole number.
- After the division, we will store the bisected part into a variable. Starting from the point where the image is bisected.
top_section = image[:half_height, :]
- The imshow function is used to display the divided parts.
cv2.imshow('Top', top_section)
imwrite
takes two args and lets us save the name of the image.cv2.waitKey(0)
takes only one argument while taking time in milliseconds. 0 here represents that the output will be viewed till infinity or till the user hits exit.- We receive the horizontally sliced images.
Code to divide an image Vertically
The vertical division is similar to the horizontal division but in this, we divide the width of the image instead of its height.
import cv2 image= cv2.imread('image1.png') # cv2.imread() = will take the image as input width, channels = image.shape #dividing the width by 2 half = width//2 # this will be the first column left_side = image[:, :half] """ [:,:half] means all the rows and all the columns upto index half """ right_side = image[:, half:] """[:,half:] means all the rows and all the columns from index half to the end """ cv2.imshow('Left part', left_side) #cv2.imshow is used for displaying the image cv2.imshow('Right part', right_side) """ saving all the images cv2.imwrite() function will save the image into your system. """ cv2.imwrite('right.jpg', right_side) cv2.imwrite('left.jpg', left_side) cv2.waitKey(0)
Explanation of the vertical image division code:
- Import all the required libraries using the import function. Example,
import cv2
- OpenCV reads the image using the imread function. We use it to read the provided image.
- Then we will extract the dimensions of the image provided.
- We will then try to divide the width as while dividing vertically we need to divide the width.
- While dividing the width we used floor division, which rounds the width to the nearest whole number.
- After the division, we will store the bisected part into a variable. Starting form the point where the image is bisected.
right_side = image[:, half:]
- The imshow function is used to display the divided parts.
cv2.imshow('Left part', left_side)
imwrite
takes two args and lets us save the name of the image.cv2.waitKey(0)
takes only one argument while taking time in milliseconds. 0 here represents that the output will be viewed till infinity or till the user hits exit.- We receive the vertically sliced images.
Output:
With this code, we have completed our tutorial. learn to draw a circle on an image using OpenCV Python
Leave a Reply