0

I have an image with 9056 x 4476 pixels. Since I need to recognize small objects and get theirs positions in the large image, I want to extract overlapping blocks from it to process them separately. These blocks need to have 100 x 300 pixels each, because I have a machine learning trained with images 100 x 300. When I execute the code below, I get images with (100+2 * margin, 300+2 * margin) pixels.

This code produces a list of arrays with a length equal to 1260. From the position 0 (out[0]) to 30 (out[30]) I've got the expected results. However, from the position 31 to 89, not. At these positions, the 'out shape' is (500, 0). Why that happens?

I got this code from another stackoverflow post. I just modified it a little bit.

def splitted(img,window_size, margin):
sh = list(img.shape)
sh[0], sh[1] = sh[0] + margin * 2, sh[1] + margin * 2
img_ = np.zeros(shape=sh)
img_[margin:-margin, margin:-margin] = img

stride,step = [] , []
stride = window_size
step.append(window_size[0] + 2 * margin)
step.append(window_size[1] + 2 * margin)


nrows, ncols = img.shape[0] // window_size[0], img.shape[1] // window_size[1]
splitted = []
for i in range(nrows):
    for j in range(ncols):
        h_start = j*stride[0]
        v_start = i*stride[1]
        cropped = img_[v_start:v_start+step[0], h_start:h_start+step[1]]
        splitted.append(cropped)
return splittedenter

I call it running this:

import cv2
import numpy as np

img = np.arange(9056 * 4476).reshape(9056,4476)
out = splitted(img, window_size=[300,100], margin=100)

If there is an easier way to get this blocks and theirs positions, I’m open to suggestions.

0 Answers0