How can I Get Laplacian pyramid using opencv

0 votes

​​I'm trying to get a layer of the Laplacian pyramid using the opencv functions: pyrUp and pyrDown.

In the documentation and in more detail in this book, I found that the i-th Laplacian layer should be obtained by the following expression:

Li = Gi - pyrDown(Gi+1)

where Gi is the i-th layer of the Gaussian pyramid.

What I've tried is:

def get_laplacian_pyramid_layer(img, n):
    gi = img
    for i in range(n):
        gi_prev = gi
        gi = cv2.pyrDown(gi_prev)
    pyrup = cv2.pyrUp(gi)
    return cv2.addWeighted(gi_prev, 1.5, pyrup, -0.5, 0)

But I get different sizes of the images involved in the substraction. I don't understand it because pyrUp is suppposed to invert the process of the Gaussian pyramid, i.e. pyrDown (with a lose of information of course but that should not affect the size, right?).

UPDATE

I refactored my code to:

def get_laplacian_pyramid_layer(img, n):
'''Returns the n-th layer of the laplacian pyramid'''  
    currImg, i = img, 0
    while i < n: # and currImg.size > max_level (83)
        down, up = new_empty_img(img.shape), new_empty_img(img.shape)
        down = cv2.pyrDown(img)
        up = cv2.pyrUp(down, dstsize=currImg.shape)
        lap = currImg - up
        currImg = down
        i += 1
    return lap

As you can see, I force the destination image to be of the same size of the source with the parameter dstsize of the pyrUp function.

However, this code also gives me an error when when executing the pyrUp function. The message of the error is:

OpenCV Error: Assertion failed (std::abs(dsize.width - ssize.width*2) == dsize.width % 2 && std::abs(dsize.height - ssize.height*2) == dsize.height % 2) in pyrUp_,

In debug mode I checked the expression of the assertion with:

up.shape[1]-down.shape[1]*2 == up.shape[1] %2 and up.shape[0]-down.shape[0]*2 == up.shape[0] %2

and it is satisfied.

So, I don't have a clue of what is happening.

Sep 4, 2018 in Python by bug_seeker
• 15,350 points
231 views

1 answer to this question.

0 votes

As far as I can see you use pyrDown on your input image img in every iteration

down = cv2.pyrDown(img)

I suggest you change that line to

down = cv2.pyrDown(currImg)

so you actually compute the next pyramid layer.

The reason for the error is your down image. Its shape is width/2 x height/2 compared to the input image

down = cv2.pyrDown(img)

yet you try to store its pyrUp result (width * height) in a much smaller up image with its shape beeing smaller (width/2 x height/2) due to

up = cv2.pyrUp(down, dstsize=currImg.shape)
...
currImg = down
answered Sep 4, 2018 by Priyaj
• 56,520 points

Related Questions In Python

+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
1,289 views
+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,350 points
5,630 views
+2 votes
2 answers

How can I plot a k-dsitance graph using python?

Hi there, instead of sklearn you could ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
1,171 views
0 votes
1 answer

How can I get a list of locally installed Python modules?

Solution My 50 cents for getting a pip freeze-like ...READ MORE

answered May 23, 2018 in Python by charlie_brown
• 7,720 points
96 views
0 votes
1 answer

How to resize image canvas to maintain square aspect ratio in Python, OpenCv

Building on Alexander-Reynolds answer above, here is ...READ MORE

answered Sep 4, 2018 in Python by Priyaj
• 56,520 points
1,188 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 225 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
1,692 views
+1 vote
1 answer

How can I map input to output without using dynamic()

Here am talking about my example you ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 56,520 points
132 views