'divide image into two equal parts python opencv

can someone tell me how to implement a bisection of the image into upper and lower part? so that I could overlap them. for example, I have an image and I should divide it to calculate a number of pixels on each part. I am new to OpenCV and don't exactly understand the geometry of the image.



Solution 1:[1]

You can crop the top and bottom portion of the image down the middle horizontally.

Open the image.

import cv2
import numpy as np
image = cv2.imread('images/blobs1.png')
cv2.imshow("Original Image", image)
cv2.waitKey(0) 

Use image.shape to let us capture the height and width variables.

height, width = image.shape[:2]
print image.shape

Now we can start cropping.

# 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()

Finally, we can use image.size to give use the number of pixels in each part.

cropped_top.size
cropped_bot.size

You can do the same thing with contours but it will involve bounding boxes.

Solution 2:[2]

To simplify @avereux's answer:

In Python you can use splicing to break down an image into sub-images. The syntax for this is:

sub_image = full_image[y_start: y_end, x_start:x_end]

Note that for images, the origin is the top-left corner of the image. So a pixel on the first row of the image (that is the topmost row) would have coordinates x_coordinate = x, y_coordinate = 0

To get the shape of the image, use image.shape. This returns (no_of_rows, no_of_cols)

You can use these to break down the image any which way you want.

Solution 3:[3]

Here is much more flexible method by which you can cut image in half or into 4 equal parts or 6 parts how ever parts you may need.

This code will crop image first into 2 piece Horizontally Then for each of that 2 piece it will crop another 3 images Leaving total 6 cropped images Change the CROP_W_SIZE and CROP_H_SIZE value to adjust your crop settings. you will need a CROP folder where this code will save images to.

import cv2,time

img = cv2.imread('image.png')
img2 = img

height, width, channels = img.shape
# Number of pieces Horizontally 
CROP_W_SIZE  = 3 
# Number of pieces Vertically to each Horizontal  
CROP_H_SIZE = 2 

for ih in range(CROP_H_SIZE ):
    for iw in range(CROP_W_SIZE ):

        x = width/CROP_W_SIZE * iw 
        y = height/CROP_H_SIZE * ih
        h = (height / CROP_H_SIZE)
        w = (width / CROP_W_SIZE )
        print(x,y,h,w)
        img = img[y:y+h, x:x+w]



        NAME = str(time.time()) 
        cv2.imwrite("CROP/" + str(time.time()) +  ".png",img)
        img = img2

Solution 4:[4]

Check this out for two left-right and bottom-up division:

import cv2
  
img = cv2.imread('img.jpg')
  
h, w, channels = img.shape
  

for left-right division:

half = w//2
      
 
left_part = img[:, :half]           
      
right_part = img[:, half:]  
      
cv2.imshow('Left part', left_part)
cv2.imshow('Right part', right_part)
  

for top-bottom division:

half2 = h//2
          
top = img[:half2, :]
bottom = img[half2:, :]
          
cv2.imshow('Top', top)
cv2.imshow('Bottom', bottom)

I edited a bit this source: https://www.geeksforgeeks.org/dividing-images-into-equal-parts-using-opencv-in-python/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 avereux
Solution 2 Shawn Mathew
Solution 3 amitnair92
Solution 4