john
john

Reputation: 629

ValueError when stacking images in python

I am attempting to focus stack several images but I keep receiving the error below. Why am I receiving this error and how should I fix it?

Any advice and code snippets on how to fix this problem would be greatly appreciated.

I have taken a look at this post but am still unsure of the meaning of this error in my scenario.

File "/Users/...", line 32, in stack maximum = abs_laps.max(axis=0)

File "/anaconda3/lib/python3.5/site-packages/numpy/core/_methods.py", line 26, in _amax return umr_maximum(a, axis, None, out, keepdims)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The stack method indicated in the error above is provided below and so is the stacker method.

def stacker(folder, num):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder,filename))
        if img is not None:
            images.append(img)
    stacked = stack(images)
    newpath = "key-frames" #destination of final image
    os.chdir(newpath)
    cv2.imwrite("Stacked%d.png" % num, stacked)

The stack method is below

def stack(imgs):
    #aligns the images
    images = imageAlignment(imgs)
    laps = []

    #loop through images and compute lap
    for i in range(len(images)):
        grayImg = cv2.cvtColor(images[i],cv2.COLOR_BGR2GRAY)
        laps.append(findLap(grayImg))

    #converts input to array
    laps = np.asarray(laps)

    #creates empty array
    output = np.zeros(shape=images[0].shape, dtype=images[0].dtype)

    #find absolute value of laps
    abs_laps = np.absolute(laps)

    #find maximum of laps
    maximum = abs_laps.max(axis=0)

    #boolean to determine if lap for image is max
    booleanChecker = abs_laps == maximum

    #pixels are unit8 and uint8 will wrap
    mask = booleanChecker.astype(np.uint8)

    #inverts every bit of array using mask that specifies of output array 
    #to be changed
    for i in range(0,len(images)):
        output = cv2.bitwise_not(images[i],output, mask=mask[i])

    return 255 - output

EDIT

Below is a sample of what abs_laps is made of.

[0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00 >0.000e+00 0.000e+00 0.000e+00 2.000e+00 1.600e+01 6.400e+01 1.800e+02 >3.800e+02]

Upvotes: 2

Views: 273

Answers (1)

ASingh
ASingh

Reputation: 74

There is an error in your input data. You should print out on which image the code breaks and inspect the data. Most likely you have an empty image in the set of images to stack.

Upvotes: 2

Related Questions