Ask Your Question
0

python opencv sliding window issue

asked 2017-07-05 04:12:45 -0600

U.Swap gravatar image

updated 2017-07-06 03:58:27 -0600

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 ...

(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-07-05 23:36:50 -0600

berak gravatar image

updated 2017-07-06 01:25:08 -0600

maybe you can apply padding to your image using copyMakeBorder, like this:

 # how many pixels do we need to get an additional window ?
 x_padding = windowsize_c - (test_image.shape[1] % windowsize_c)
 y_padding = windowsize_r - (test_image.shape[0] % windowsize_r)

 image_padded = cv2.copyMakeBorder(image, 0,y_padding, 0, x_padding, cv2.BORDER_REPLICATE)
edit flag offensive delete link more

Comments

Thanks for your answer, but i have just edited my rolling_window fun. just see my question, but it throws me exception when I get 3D color histogram of that. the exception is: "Assertion failed (j < nimages) in histPrepareImages" , I have added the 3D histogram func. in my question pls see that as well.

U.Swap gravatar imageU.Swap ( 2017-07-06 03:53:41 -0600 )edit

sorry, but i don't see any code related to histograms, and the error seems unrelated.

(also, what on earth do you want to do with a 3d histogram?)

last, i don't see any reason, why you have to make a (quite complicated) list of windows before iterating. and tbh, i don't quite understand that code.

berak gravatar imageberak ( 2017-07-06 03:58:36 -0600 )edit

your histogram problem should go into an entirely seperate question, imho.

berak gravatar imageberak ( 2017-07-06 04:03:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-07-05 04:12:45 -0600

Seen: 1,216 times

Last updated: Jul 06 '17