Divide Image into Two equal Parts using Open Cv
by vigneshchennai74 Updated: Feb 6, 2023
OpenCV is a library of programming functions mainly aimed at real-time computer vision. It is written in C, C++, and Python, and runs on Windows, Linux, Android, and macOS.OpenCV is widely used in the field of computer vision for tasks such as object recognition, face detection, and image and video analysis. It has a large community of developers and users and is continuously updated and improved.
OpenCV provides a large collection of algorithms and functions for image and video processing, including:
- Image processing operations like filtering, morphological transformations, thresholding, etc.
- Object detection and recognition, including face detection and recognition, object tracking, etc.
- Image and video analysis, including edge detection, feature extraction, and optical flow.
- Camera calibration and 3D reconstruction.
- Machine learning algorithms, including support for deep learning frameworks like TensorFlow and Caffe.
You can divide an image into two equal parts vertically or horizontally using OpenCV by simply slicing the image array. Here's an example of how you could divide an image into two equal parts horizontally in Python using OpenCV:
This code splits the image into two equal parts, horizontally. It first retrieves the shape of the image and calculates the height and width of the image. It then calculates the starting and ending row and column pixel coordinates for the top and bottom halves of the image. The image is then sliced and each half is stored in the cropped_top and cropped_bot variables. Finally, each of the two cropped images is displayed using the OpenCV function cv2.imshow() and is shown until a key is pressed using the cv2.waitKey(0) function
Here is an example of how you can Divide the image into two equal parts using OpenCV
Preview of the output that you will get on running this code from your IDE
In this solution we use the Imread function of the OpenCV.
import cv2 import numpy as np image = cv2.imread('images/blobs1.png') cv2.imshow("Original Image", image) cv2.waitKey(0) height, width = image.shape[:2] print image.shape # Let's get the starting pixel coordiantes (top left of cropped top) start_row, start_col = int(0), int(0) # Let's get the ending pixel coordinates (bottom right of cropped top) end_row, end_col = int(height * .5), int(width) cropped_top = image[start_row:end_row , start_col:end_col] print start_row, end_row print start_col, end_col cv2.imshow("Cropped Top", cropped_top) cv2.waitKey(0) cv2.destroyAllWindows() # Let's get the starting pixel coordiantes (top left of cropped bottom) start_row, start_col = int(height * .5), int(0) # Let's get the ending pixel coordinates (bottom right of cropped bottom) end_row, end_col = int(height), int(width) cropped_bot = image[start_row:end_row , start_col:end_col] print start_row, end_row print start_col, end_col cv2.imshow("Cropped Bot", cropped_bot) cv2.waitKey(0) cv2.destroyAllWindows() cropped_top.size cropped_bot.size
- Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
- Modify the name, location of the image to display in the code.
- Run the file to divide the image to Top and Bottom
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
i found this code snippet by searching for "divide image into tow equal parts python opencv" in kandi. You can try any such use case!
I tested this solution in the following versions. Be mindful of changes when working with other versions
- The solution is created and executed in python version 3.7.15 .
- The solution is tested on OpenCV 4.6.0
- The solution is tested on numpy 1.21.6
Using this solution, we are able to divide an image using the OpenCV library in Python with simple steps. This process also facilities an easy-to-use, hassle-free method to create a hands-on working version of code which would help us divide an image in Python
The fundamental package for scientific computing with Python.
Python 23663 Version:v1.25.0rc1 License: Permissive (BSD-3-Clause)
- For any support on kandi solution kits, please use the chat
- For further learning resources, visit the Open Weaver Community learning page.