Skip to content

Instantly share code, notes, and snippets.

@jychstar
Created April 14, 2017 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jychstar/7eef27dbb3b7ac5e77a3f7e9724bd70f to your computer and use it in GitHub Desktop.
Save jychstar/7eef27dbb3b7ac5e77a3f7e9724bd70f to your computer and use it in GitHub Desktop.
create sliding windows to identify objects
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
% matplotlib inline
image = mpimg.imread('bbox-example-image.jpg')
def draw_boxes(img, bboxes, color=(0, 0, 255), thick=6):
imcopy = np.copy(img)
for bbox in bboxes:
cv2.rectangle(imcopy, bbox[0], bbox[1], color, thick)
return imcopy
def slide_window(img, x_start,x_stop, y_start,y_stop,
xy_window=(64, 64), xy_overlap=(0.5, 0.5)):
# Overlap pixels, which slows down the window moving
x_overlap = np.int(xy_window[0]*(xy_overlap[0]))
y_overlap = np.int(xy_window[1]*(xy_overlap[1]))
# Compute the effective moving pixels per step
nx_pix_per_step = xy_window[0]- x_overlap
ny_pix_per_step = xy_window[1]- y_overlap
# Compute the number of windows in x/y
nx_windows = np.int((x_stop - x_start-x_overlap)/nx_pix_per_step)
ny_windows = np.int((y_stop - y_start-y_overlap)/ny_pix_per_step)
window_list = []
for y in range(ny_windows):
for x in range(nx_windows):
startx = x * nx_pix_per_step + x_start
endx = startx + xy_window[0]
starty = y * ny_pix_per_step + y_start
endy = starty + xy_window[1]
window_list.append(((startx, starty), (endx, endy)))
return window_list
y_height, x_width, channel = image.shape
windows = slide_window(image, 0,x_width, 0, y_height,
xy_window=(128, 128), xy_overlap=(0.5, 0.5))
window_img = draw_boxes(image, windows, color=(0, 0, 255), thick=6)
plt.imshow(window_img)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment