Sliding Windows for Object Detection with Python

Sliding window is a rectangular region that slides across an image with a fixed width and height.

import cv2
import matplotlib.pyplot as plt
import numpy as np

# read the image and define the stepSize and window size
# (width,height)
image = cv2.imread("cell.png") # your image path
tmp = image # for drawing a rectangle
stepSize = 50
(w_width, w_height) = (50, 50) # window size
for x in range(0, image.shape[1] - w_width , stepSize):
for y in range(0, image.shape[0] - w_height, stepSize):
window = image[x:x + w_width, y:y + w_height, :]

# classify content of the window with your classifier and
# determine if the window includes an object (cell) or not
      # draw window on image
cv2.rectangle(tmp, (x, y), (x + w_width, y + w_height), (255, 0, 0), 2) # draw rectangle on image
plt.imshow(np.array(tmp).astype('uint8'))
# show all windows
plt.show()

Source: Deep Learning on Medium