Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

python opencv sliding window issue

Am making use of sliding/rolling window technique to devide the input image into equal chunks of given size so for that am making use of following function to devide image into specified window size.

def rolling_window(base_cord,test_image, window):
    """Very basic multi dimensional rolling window. window should be the shape of
    of the desired subarrays. Window is either a scalar or a tuple of same size
    as `arr.shape`.
    """
    windowsize_r = window[0]
    windowsize_c = window[1]
    chunk_arr = {}
    for r in range(0, test_image.shape[0] - windowsize_r, windowsize_r):
        for c in range(0, test_image.shape[1] - windowsize_c, windowsize_c):
            win = test_image[r:r + windowsize_r, c:c + windowsize_c]
            cord = (r+base_cord[0], c+base_cord[1])
            chunk_arr[cord] = win
    return chunk_arr

So, this function returns me the window of specified size lets say 50x50 along with its base cordinates in dictionary format.

But problem is suppose I pass down the image of any odd size like 968x885 then spliting equal size chunks of size 50x50 will left some odd boundary pixels at the end of the image , the resuling image after first round of sliding window looks something like below.

enter image description here

As we can see above , the pixels at right side are not chunked into window coz it's < 50x50 window size.

So what i want is to modify rolling_window function in order to form seperate window chunk for remaining pixels in every possible row/column. So how to achieve this.

Any help would be greatly appreciated!

click to hide/show revision 2
retagged

updated 2017-07-05 04:19:07 -0600

berak gravatar image

python opencv sliding window issue

Am making use of sliding/rolling window technique to devide the input image into equal chunks of given size so for that am making use of following function to devide image into specified window size.

def rolling_window(base_cord,test_image, window):
    """Very basic multi dimensional rolling window. window should be the shape of
    of the desired subarrays. Window is either a scalar or a tuple of same size
    as `arr.shape`.
    """
    windowsize_r = window[0]
    windowsize_c = window[1]
    chunk_arr = {}
    for r in range(0, test_image.shape[0] - windowsize_r, windowsize_r):
        for c in range(0, test_image.shape[1] - windowsize_c, windowsize_c):
            win = test_image[r:r + windowsize_r, c:c + windowsize_c]
            cord = (r+base_cord[0], c+base_cord[1])
            chunk_arr[cord] = win
    return chunk_arr

So, this function returns me the window of specified size lets say 50x50 along with its base cordinates in dictionary format.

But problem is suppose I pass down the image of any odd size like 968x885 then spliting equal size chunks of size 50x50 will left some odd boundary pixels at the end of the image , the resuling image after first round of sliding window looks something like below.

enter image description here

As we can see above , the pixels at right side are not chunked into window coz it's < 50x50 window size.

So what i want is to modify rolling_window function in order to form seperate window chunk for remaining pixels in every possible row/column. So how to achieve this.

Any help would be greatly appreciated!

python opencv sliding window issue

Am making use of sliding/rolling window technique to devide the input image into equal chunks of given size so for that am making use of following function to devide image into specified window size.

[Edited below fun. defination]

def rolling_window(base_cord,test_image, window):
    """Very basic multi dimensional rolling window. window should be the shape of
    of the desired subarrays. Window is either a scalar or a tuple of same size
    as `arr.shape`.
    """
    rest_c_ls = []
    windowsize_r = window[0]
    windowsize_c = window[1]
    chunk_arr = {}
     for r in range(0, test_image.shape[0] - windowsize_r, windowsize_r):
        for c in range(0, test_image.shape[1] - windowsize_c, windowsize_c):
            win = test_image[r:r + windowsize_r, c:c + windowsize_c]
            cord = (r+base_cord[0], c+base_cord[1])
            rest_c_ls.append(cord[1])
            chunk_arr[cord] = win
        else:
            rest_c = (test_image.shape[1] - windowsize_c)
            rest_c_ls.append(rest_c)
            win = test_image[r:r + windowsize_r, rest_c:rest_c + windowsize_c]
            cord = (r + base_cord[0], rest_c + base_cord[1])
            chunk_arr[cord] = win
            #print test_image.shape[1] - windowsize_c
    else:
        for rest_c in rest_c_ls:
            rest_r = (test_image.shape[0] - windowsize_r)
            win = test_image[rest_r:rest_r + windowsize_r, rest_c:rest_c + windowsize_c]
            cord = (rest_r + base_cord[0], rest_c + base_cord[1])
            chunk_arr[cord] = win

            #print 'else'+str(test_image.shape[0] - windowsize_r)
        #test_image[r:r + windowsize_r, c:c + windowsize_c]

    return chunk_arr

So, this function returns me the window of specified size lets say 50x50 along with its base cordinates in dictionary format.

But problem is suppose I pass down the image of any odd size like 968x885 then spliting equal size chunks of size 50x50 will left some odd boundary pixels at the end of the image , the resuling image after first round of sliding window looks something like below.

enter image description here

As we can see above , the pixels at right side are not chunked into window coz it's < 50x50 window size.

So what i want is to modify rolling_window function in order to form seperate window chunk for remaining pixels in every possible row/column. So how to achieve this.

Any help would be greatly appreciated!

python opencv sliding window issue

Am making use of sliding/rolling window technique to devide the input image into equal chunks of given size so for that am making use of following function to devide image into specified window size.

[Edited below fun. defination]

def rolling_window(base_cord,test_image, window):
    """Very basic multi dimensional rolling window. window should be the shape of
    of the desired subarrays. Window is either a scalar or a tuple of same size
    as `arr.shape`.
    """
    rest_c_ls = []
    windowsize_r = window[0]
    windowsize_c = window[1]
    chunk_arr = {}

    for r in range(0, test_image.shape[0] - windowsize_r, windowsize_r):
        for c in range(0, test_image.shape[1] - windowsize_c, windowsize_c):
            win = test_image[r:r + windowsize_r, c:c + windowsize_c]
            cord = (r+base_cord[0], c+base_cord[1])
            rest_c_ls.append(cord[1])
            chunk_arr[cord] = win
        else:
            rest_c = (test_image.shape[1] - windowsize_c)
            rest_c_ls.append(rest_c)
            win = test_image[r:r + windowsize_r, rest_c:rest_c + windowsize_c]
            cord = (r + base_cord[0], rest_c + base_cord[1])
            chunk_arr[cord] = win
            #print test_image.shape[1] - windowsize_c
    else:
        for rest_c in rest_c_ls:
            rest_r = (test_image.shape[0] - windowsize_r)
            win = test_image[rest_r:rest_r + windowsize_r, rest_c:rest_c + windowsize_c]
            cord = (rest_r + base_cord[0], rest_c + base_cord[1])
            chunk_arr[cord] = win

            #print 'else'+str(test_image.shape[0] - windowsize_r)
        #test_image[r:r + windowsize_r, c:c + windowsize_c]

    return chunk_arr

So, this function returns me the window of specified size lets say 50x50 along with its base cordinates in dictionary format.

But problem is suppose I pass down the image of any odd size like 968x885 then spliting equal size chunks of size 50x50 will left some odd boundary pixels at the end of the image , the resuling image after first round of sliding window looks something like below.

enter image description here

As we can see above , the pixels at right side are not chunked into window coz it's < 50x50 window size.

So what i want is to modify rolling_window function in order to form seperate window chunk for remaining pixels in every possible row/column. So how to achieve this.

Also, I have updated this question as have a function to calculate 3D histogram for given image window , so after updating the above window function I am able to get all the remaining chunks of the windows but it thows me exception while calculating the 3D histogram, here function.

def extract_color_histogram(image, bins=(8, 8, 8)):
    # extract a 3D color histogram from the HSV color space using
    # the supplied number of `bins` per channel
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    hist = cv2.calcHist([hsv], [0, 1, 2], None, bins,
                        [0, 180, 0, 256, 0, 256])

    # handle normalizing the histogram if we are using OpenCV 2.4.X
    if imutils.is_cv2():
        hist = cv2.normalize(hist)

    # otherwise, perform "in place" normalization in OpenCV 3 (I
    # personally hate the way this is done
    else:
        cv2.normalize(hist, hist)

    # return the flattened histogram as the feature vector
    return hist.flatten()
    #except Exception as e:
    #    print e

Now it's throwing exception at this line :

hist = cv2.calcHist([hsv], [0, 1, 2], None, bins,
                            [0, 180, 0, 256, 0, 256])

OpenCV Error: Assertion failed (j < nimages) in histPrepareImages, file /tmp/yaourt-tmp-swap/aur-opencv2/src/opencv-2.4.13/modules/imgproc/src/histogram.cpp, line 148 Traceback (most recent call last): File "/run/media/swap/a673bea3-9088-494e-bad5-5b80d6870b9b/home/swap/openCV/Satellite Image Processing/svm/svm_predict.py", line 253, in <module> round2(rolling_window(c,w, step_size)) File "/run/media/swap/a673bea3-9088-494e-bad5-5b80d6870b9b/home/swap/openCV/Satellite Image Processing/svm/svm_predict.py", line 100, in round2 pixels = extract_color_histogram(win) File "/run/media/swap/a673bea3-9088-494e-bad5-5b80d6870b9b/home/swap/openCV/Satellite Image Processing/svm/svm_predict.py", line 50, in extract_color_histogram [0, 180, 0, 256, 0, 256]) cv2.error: /tmp/yaourt-tmp-swap/aur-opencv2/src/opencv-2.4.13/modules/imgproc/src/histogram.cpp:148: error: (-215) j < nimages in function histPrepareImages

Any help would be greatly appreciated!