Reem Al-Assaf
Reem Al-Assaf

Reputation: 1

Image Pyramids calculation error "Sizes of input arguments do not match"

I am learning about image pyramids by following the official Python tutorials here.

I get the following error as well as a warning that highlights the one-before-the-last ref to cols.

Can someone please help me understand what the error is caused by and how I can change the code to resolve the warning on real = np.hstack((A[:, :cols/2], B[:, cols/2:]))?

line 29, in L = cv.subtract(gpA[i - 1], GE) cv2.error: OpenCV(4.4.0) /private/var/folders/9m/h_ts52xj0jdgs6w0m2lqv9wr0000gn/T/pycharm-packaging/opencv-python/opencv/modules/core/src/arithm.cpp:669: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'arithm_op'

import cv2 as cv
import numpy as np

path1 = "resources/apple.jpg"
A = cv.imread(path1, 0)

path2 = "resources/orange.jpg"
B = cv.imread(path2, 0)

# Generate Gaussian pyramid for A.
G = A.copy()
gpA = [G]
for i in range(6):
    G = cv.pyrDown(G)
    gpA.append(G)

# Generate Gaussian pyramid for B.
G = B.copy()
gpB = [G]
for i in range(6):
    G = cv.pyrDown(G)
    gpB.append(G)

# Generate Laplacian Pyramid for A.
lpA = [gpA[5]]
for i in range(5, 0, -1):
    GE = cv.pyrUp(gpA[i])
    L = cv.subtract(gpA[i - 1], GE)
    lpA.append(L)

# Generate Laplacian Pyramid for B.
lpB = [gpB[5]]
for i in range(5, 0, -1):
    GE = cv.pyrUp(gpB[i])
    L = cv.subtract(gpB[i - 1], GE)
    lpB.append(L)

# Now, add left and right halves of images in each level.
LS = []
for la, lb in zip(lpA, lpB):
    rows, cols, dpt = la.shape
    ls = np.hstack((la[:, :cols/2], lb[:, cols/2:]))
    LS.append(ls)

# Now, re-construct.
ls_ = LS[0]
for i in range(1, 6):
    ls_ = cv.pyrUp(ls_)
    ls_ = cv.add(ls_, LS[i])

# Image with direct connecting each half.
real = np.hstack((A[:, :cols/2], B[:, cols/2:]))

cv.imwrite('Pyramid_blending.jpg', ls_)
cv.imwrite('Direct_blending.jpg', real)

These are the images I am using:

enter image description here enter image description here

Upvotes: 0

Views: 264

Answers (1)

DrBwts
DrBwts

Reputation: 3657

The problem arises when you use the default values in cv.pyrUp.

The upsampled images dimensions by default are calculated using,

|dstsize.width−src.cols∗2| ≤ (dstsize.widthmod2)
|dstsize.height−src.rows∗2| ≤ (dstsize.heightmod2)

So in your case this is an issue when you get to gpA[4] which has an odd width dimension & the upsampler uses the defaults and gives an even width dimension for upsampled gpA[5]

gpA[4].shape: (13, 16)

GE.shape: (14, 16)

You end up with arrays of different dimensions and hence cant subtract them from one another.

To avoid this you will need to explcitly define the dimensions you want from your upsampling eg you could find the dimensions of gpA[i-1] & use those

Upvotes: 1

Related Questions