shouto
shouto

Reputation: 31

Size of input arguments do not match due to pyrUp/pyrDown?

I have searched and couldn't find anything regarding this so apologies if it's a duplicate question.

I'm currently learning OpenCV and have encountered a problem with the size of arrays while trying to get laplacian images. Basically the result returned from cv2.pyrUp() has one more 'row' compared to the same level from the Gaussian pyramid, which causes it to throw the size of input argument error. Has anyone encountered this before and what can I do to fix it?

Here's my initial code:

img = cv2.imread('test.jpg', 1)
G = img.copy()
gpA =[G]
for i in range(0,5):
  G = cv2.pyrDown(G)
  gpA.append(G)

lpA = [gpA[5]]
for i in xrange(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    w,d,h = GE.shape        #debug
    print w,d,h

    x,y,z = gpA[i-1].shape  #debug
    print x,y,z

    L = cv2.subtract(gpA[i-1], GE)
    lpA.append(L)

I used the code from this:http://docs.opencv.org/3.1.0/dc/dff/tutorial_py_pyramids.html#gsc.tab=0

I get the size of input arguments error, with the debug results being

58 90 3 
57 90 3

However by changing the code starting from the pyrUp section to iterate from the first level:

lpA = [gpA[5]]
for i in xrange(0,5):
 GE = cv2.pyrUp(gpA[i+1])
    w,d,h = GE.shape          #debug
    print w,d,h
    x,y,z = gpA[i].shape      #debug
    print x,y,z 
    L = cv2.subtract(gpA[i], GE)
    lpA.append(L)

The results I get from the debug are:

900 1440 3
900 1440 3
450 720 3
450 720 3
226 360 3
225 360 3

So in this case the loop managed to iterate for 2 times before encountering the same problem.

Upvotes: 3

Views: 2814

Answers (1)

Sunreef
Sunreef

Reputation: 4552

The problem here is that at one point you get an image with an uneven number of rows. Let's say you have 2 * n + 1 rows. pyrDown will give you an image with n + 1 rows. So when you do a pyrUp, you will end up with an image with 2*n + 2 rows, one more than before.

The solution is, when you do your pyrUp, you should do it like this:

size = (gpA[i].shape[1], gpA[i].shape[0])
GE = cv2.pyrUp(gpA[i+1], dstsize = size)

This way your image GE will have the same size as gpA[i].

Upvotes: 7

Related Questions