问题
My objective is to have a sliding window slide over an image in overlapping steps so that I can run a classifier in each window and detect if an interesting object is there.
For that, I need to make sure that
- windows I extract for classification truly do over the whole image, and
- grab the top and left coordinates of each sliding window on the original image.
Following up from here: Sliding window - how to get window location on image?
and based on this code for sliding windows:
https://github.com/keepitsimple/ocrtest/blob/master/sliding_window.py#blob_contributors_box
I'm doing the following, given that winh and winw are the sliding window width and height, and window_size is (winh, winw):
grid_h_max =(imgheight/winh)
grid_w_max= (imgwidth / winw)
win = sliding_window(img, window_size, shiftSize=None, flatten=False)
for h in range (grid_h_max):
for w in range (grid_w_max):
win = windows[h,w]
grid_pos = (h,w), (winh, winw))
t, b, l, r = get_win_pixel_coords(grid_pos, window_size)
This is working just fine - giving me sliding windows as well as the coordinates for each window. But I'm not getting overlapping sliding windows.
So if my stepsize is 10 and my first window starts at (top, left): (0,0), then my second window should be (0, 10).
But in the above code, my second window is (0, 60).
Earlier, I was using this code:
windows = sw.sliding_window(image1, window_size, step_size, flatten=True)
for w in windows:
# and then I grabbed each w and printed it out
And this was giving me all the sliding windows, including overlapping windows. However in this method, I wasn't able to grab the top and left coordinates of each sliding window.
回答1:
Alright so I figured out what the issue was.
This code didn't cause overlaps:
grid_h_max =(imgheight/winh)
grid_w_max= (imgwidth / winw)
win = sliding_window(img, window_size, shiftSize=None, flatten=False)
Dividing the entire image dimensions with those of the window dimensions are obviously going to give non-overlapping results.
To get the right number of windows per dimension, I simply allow the sliding_window function to tell me itself what the number of windows is, vertically and horizontally:
win = sliding_window(img, window_size, shiftSize=None, flatten=False)
grid_h_max = win.shape[0]
grid_w_max = win.shape[1]
This gave me approximately 5000 windows again.
来源:https://stackoverflow.com/questions/27621831/overlapping-sliding-windows-over-image