How to make a laplacian pyramid using OpenCV python?

+2 votes

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

I'm trying to create a Laplacian pyramid using OpenCV. According to the openCV documentation, there is a way to do this using the following expression:

Li = Gi - pyrDown(Gi+1)

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

This is is what I've been able to do till now

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)

The problem is that I get different sizes of the images that are used in the subtractions. And I have no clue why this is happening

Apr 3, 2018 in Python by ariaholic
• 7,340 points
1,720 views

2 answers to this question.

+2 votes
Best answer

down voteacceptTheeThe problem is that you're iterating pyrDown everytime 

down = cv2.pyrDown(img)

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 


down voteaccepted

answered Apr 3, 2018 by charlie_brown
• 7,720 points

selected Oct 12, 2018 by Omkar
0 votes

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_,

Many years late to the party here, but here we go. When passing the upscaled image size using 'dstsize:

up = cv2.pyrUp(down, dstsize=currImg.shape)

the .shape method returns height x width. However, the pyrup function requires these dimensions as width x height. Therefore try the following code:

up = cv2.pyrUp(down, dstsize=(currImg.shape[1],currImg.shape[0])
answered Oct 12, 2018 by findingbugs
• 4,770 points

Related Questions In Python

0 votes
1 answer

How can I Get Laplacian pyramid using opencv

As far as I can see you ...READ MORE

answered Sep 4, 2018 in Python by Priyaj
• 56,960 points
330 views
0 votes
1 answer

How to download a file over HTTP using Python?

In Python 2, use urllib2 which comes ...READ MORE

answered Oct 18, 2018 in Python by SDeb
• 13,210 points
66 views
0 votes
1 answer

How to put a variable inside a String using Python?

In the easiest way, you can create ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,520 points

edited Dec 12, 2018 by Nymeria 60 views
0 votes
1 answer

How to convert an integer to a string using Python?

Here is an easy solution: def numberToBase(n, b): ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,520 points

edited Dec 12, 2018 by Nymeria 55 views
0 votes
1 answer

How to get the return value from a thread using python?

You don't need to change your existing ...READ MORE

answered Dec 3, 2018 in Python by Nymeria
• 3,520 points

edited Dec 19, 2018 by Nymeria 15,907 views
0 votes
1 answer

How to fit a histogram using Python?

Here is an example working on py2.6 ...READ MORE

answered Dec 26, 2018 in Python by Nymeria
• 3,520 points
894 views
0 votes
0 answers

How to implement multiple try codes in a single block using Python?

Hi all, As per the title, I am ...READ MORE

Jan 14 in Python by Anirudh
• 2,070 points
55 views
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21 in Python by Nymeria
• 3,520 points
752 views
0 votes
1 answer

How to correctly return an a dictionary as an output in zappier code using python?

David here, from the Zapier Platform team. ...READ MORE

answered Dec 3, 2018 in Python by charlie_brown
• 7,720 points
146 views
0 votes
1 answer