IndexError: too many indications for array due to operation on element of numpy array

Asked 2 years ago, Updated 2 years ago, 65 views

When I wrote the following Opencv program on Python,

#-*-coding:utf-8-*-
import cv2
import numpy as np

th = 100

# Acquisition of background and input images
im_bg = cv2.imread('kyousitu2.jpeg')
im_in=cv2.imread('kyousitu1.jpeg')
# differential calculation
diff=cv2.absdiff(im_in,im_bg)
# True if the difference is less than the threshold
mask = diff <th
# Height and width of array (image)
height=im_bg.shape[0]
width=im_bg.shape[1]
#resize_img = cv2.resize(img_dst, (high/2, width/2)))

# array generation of the same size as the background image
im_mask=np.zeros(high, width), np.uint8)
# The True part (background) is painted white.
im_mask [mask] = 255
# sesame salt noise removal
im_mask=cv2.medianBlur(im_mask,blur)
# edge detection
im_edge=cv2.Canny(im_mask, 100, 200)

cv2.imshow("detected2.jpg", im_mask)

The following error occurred:

Traceback (most recent call last):
File "sabun7.py", line 22, in <module>
im_mask [mask] = 255
IndexError: too many indications for array

What does this mean?How can I fix it again?

python opencv numpy

2022-09-30 16:28

1 Answers

shape in im_mask is (height, width), but shape in mask is (height, width,3) if it is an RGB image, it is shape.For example, it looks like this

 height=im_bg.shape[0]
width=im_bg.shape[1]
channel=im_bg.shape[2]

# array generation of the same size as the background image
im_mask = np.zeros ((hight, width, channel), np.uint 8)


2022-09-30 16:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.