简体   繁体   中英

How to solve AxisError: axis 1 is out of bounds for array of dimension 0

I'm trying out Harry's invisibility cloak but getting Axis error and the display it shows while I run the code wouldn't just go away. I mean it's already showing error and I can't close that display tab either unless I close the IDE totally. Putting down the code here? Can somebody help me figure this out? How do I solve this axis issue. I guess the tab's also got frozen for this reason.

import cv2
import numpy as np
import time

cap = cv2.VideoCapture(0)
time.sleep(3)
background=0

for i in range(30):
    ret,background = cap.read()

background = np.flip(background,axis=1)

while(cap.isOpened()):
    ret, img = cap.read()
    img = np.flip(img, axis = 1)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    blurred = cv2.GaussianBlur(hsv, (35, 35), 0)
    lower = np.array([0,120,70])
    upper = np.array([10,255,255])
    mask1 = cv2.inRange(hsv, lower, upper)
    lower_red = np.array([170,120,70])
    upper_red = np.array([180,255,255])
    mask2 = cv2.inRange(hsv, lower_red, upper_red)

    mask = mask1 + mask2
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, 
                            np.ones((5,5),np.uint8))
    img[np.where(mask == 255)] = background[np.where(mask == 255)]
    cv2.imshow('Display',img)
    k = cv2.waitKey(10)
    if k == 27:
        break

I'm getting error inside the for loop of the code, let me note the error notification:

     8 for i in range(60):
     9     ret,background = cap.read()
---> 10 background = np.flip(background,axis=1)

AxisError: axis 1 is out of bounds for array of dimension 0

The code works without modification on my laptop. Probably cap.read() returns no images on your system. You should always check the ret value to check if the cap.read() succeeds.

It seems you are using odd colour ranges. Shadows in my face are vanishing under the cloak. I tried with green colour only (hue 40-80) which works very nice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM