Ask Your Question
1

How to specify a custom output size in cv2.pyrDown() or cv2.pyrUp() method

asked 2018-01-30 13:03:10 -0600

GPrathap gravatar image

I want to explicitly specify the output size of the image when applying cv2.pyrDown() on images.

def gaussian_pyramid(image, scale=1.5, minSize=(30, 30)):
    yield image
    while True:
        w = int(image.shape[1] / scale)
        h = int(image.shape[0] / scale)
        image = cv2.pyrDown(image, dstsize=(w, h))
        if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:
            break
        yield image

But it is throwing an error something similar to this.

OpenCV Error: Assertion failed (ssize.width > 0 && ssize.height > 0 && std::abs(dsize.width*2 - ssize.width) <= 2 && std::abs(dsize.height*2 - ssize.height) <= 2) in pyrDown_, file /io/opencv/modules/imgproc/src/pyramids.cpp, line 873

Any idea how to specify the output size of the image as a method argument?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-30 13:07:26 -0600

berak gravatar image

updated 2018-01-30 13:08:11 -0600

pyrdown, pyrup go with a factor of 2. all you can variate the dstsize is +- 1 pixel (to adjust for odd sizes)

edit flag offensive delete link more

Comments

Please provide an example how should be dstsize is provided. cv2.pyrDown(image, dstsize=(-1,-1)) nothing seems to be working.

GPrathap gravatar imageGPrathap ( 2018-01-30 13:37:29 -0600 )edit
1

sorry, not much of a python guy here, but it's probably:

h = image.shape[0] / 2
w = image.shape[1] / 2

dstsize = (w+1, h-1) # yea, absurd...

(and no idea, if you wanted that. i'm only pointing out, that you cannot use arbitrary shapes with pyramids(think of it !!), they're basically restricted to a factor of 2 )

berak gravatar imageberak ( 2018-01-30 13:44:12 -0600 )edit

Yeap, It worked. Thanks for the prompt response.

GPrathap gravatar imageGPrathap ( 2018-01-30 14:01:19 -0600 )edit

deal: i'll upvote you, but please don't upvote me. (it's a bane , not a profit, once you go over the average...)

berak gravatar imageberak ( 2018-01-30 14:05:41 -0600 )edit

again -- you'll only need to specify dstsize, if you have an odd dimension, and if you want to clarify, how that should be handled.

berak gravatar imageberak ( 2018-01-30 14:08:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-30 13:03:10 -0600

Seen: 1,726 times

Last updated: Jan 30 '18