OpenCV python inRange方法导致C++;模块

OpenCV python inRange方法导致C++;模块,python,c++,opencv,opencv3.0,opencv3.1,Python,C++,Opencv,Opencv3.0,Opencv3.1,当我调用我的方法[错误后给出]时,我得到以下错误 error: /feedstock_root/build_artefacts/opencv_1496434080029/work/opencv-3.2.0/modules/core/src/arithm.cpp:1984: error: (-215) lb.type() == ub.type() in function inRange 生成代码的代码就在这里。我没有发现我输入到函数cv2.inRange def red_filter(rgb_i

当我调用我的方法[错误后给出]时,我得到以下错误

error: /feedstock_root/build_artefacts/opencv_1496434080029/work/opencv-3.2.0/modules/core/src/arithm.cpp:1984: error: (-215) lb.type() == ub.type() in function inRange
生成代码的代码就在这里。我没有发现我输入到函数
cv2.inRange

def red_filter(rgb_image):
    hsv_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2HSV)

    avg_brightness = average_brightness(hsv_image)
    avg_saturation = average_saturation(hsv_image)

    # Define our color selection boundaries in HSV values
    lower_red = np.array([0, avg_saturation, avg_brightness]) 
    upper_red = np.array([20,255,255])

    # Define the masked area
    hsv_mask = cv2.inRange(hsv_image, lower_red, upper_red)

    # Copy image
    hsv_masked_image = np.copy(hsv_image)

    #  Mask the image to let the light show through
    hsv_masked_image[hsv_mask != 0] = [0, 0, 0]

    # Display it!
    plt.imshow(hsv_masked_image)

你知道问题是什么吗?我在其他相关问题中找不到解决方案:&

让我们从解释错误开始:

lb.type() == ub.type() in function inRange
这意味着,在检查函数inRange中的下限(lb)和上限(up)是否属于同一类型的断言中失败

查看您的代码,上界看起来是整数:

upper_red = np.array([20,255,255])
print (upper_red.dtype) # this prints dtype('int32')
现在,下限有2个变量,我不知道它们是什么(float,int,等等)。我假设它们是浮点数,让我们看看如果我放置两个浮点数会发生什么

lower_red  = np.array([0, 1.2, 2.7])
print (lower_red .dtype) # this prints out dtype('float64')
正如你所看到的,它们不是同一类型的。现在问题已经解释清楚了,让我们继续讨论可能的解决方案:

最简单的一个,如果要将其截断:

lower_red  = np.array([0, 1.2, 2.7], dtype=np.int32)
print (lower_red.dtype) # this prints out dtype('int32')
print (lower_red) # [0, 1, 2]
这将生成与以下相同的结果:

lower_red  = np.array([0, int(1.2), int(2.7)])
如果您不想截断,您可以始终执行round或ceil(floor与截断相同)

例如:

avg_saturation = int(np.round(average_saturation(hsv_image)))
或者如果它是非负的:

avg_saturation = int( average_saturation(hsv_image) + 0.5 )

我猜,
avg_饱和度
avg_亮度
可能是浮点数,而不是整数。。。因此,下限(lb)和上限(ub)具有不同的特性types@api55你想把这个写进一个答案吗?当我确定了这些值时,它就起作用了。好的,给我一分钟,我会把它作为答案