Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 867
GPrathap Avatar asked Nov 07 '22 11:11

GPrathap


1 Answers

From OpenCV 2.4 Tutorial:

pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 )

tmp: The current image, it is initialized with the src original image.

dst: The destination image (to be shown on screen, supposedly half the input image)

Size( tmp.cols/2, tmp.rows/2): The destination size. Since we are downsampling, pyrDown expects half the size the input image (in this case tmp).

Notice that it is important that the input image can be divided by a factor of two (in both dimensions). Otherwise, an error will be shown.

This was taken from the C++ tutorial but it should apply just the same for Python.

like image 78
Nicolas Schejtman Avatar answered Nov 24 '22 00:11

Nicolas Schejtman