1

I am getting the following error when I call my method [given after the error].

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

The code that produces the code is right here. I don't see anything wrong with the code I feed to the function 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)

Any idea what the issue may be? I couldn't find the solution in other related questions: Question 1 & Question 2

3
  • 2
    my guess is that avg_saturation and avg_brightness are probably floats and not int... thus the lower bound (lb) and upper bound(ub) have different types Commented Mar 4, 2018 at 19:58
  • @api55 Would you like to put that in an answer? It works when I floor the values. Commented Mar 4, 2018 at 21:10
  • Ok give me a minute and I will put it as answer Commented Mar 5, 2018 at 11:56

1 Answer 1

2

Lets start by explaining the error:

lb.type() == ub.type() in function inRange

This means that it failed in the assert that checks if the lower bound (lb) and upper bound (up) are of the same type in the function inRange.

Looking at your code, the upper bound looks like it is int numbers:

upper_red = np.array([20,255,255])
print (upper_red.dtype) # this prints dtype('int32')

Now, the lower bound has 2 variables, which I do not know what are they (float, int, etc). I will assume they are floats, lets see what happens if I put two float numbers.

lower_red  = np.array([0, 1.2, 2.7])
print (lower_red .dtype) # this prints out dtype('float64')

As you can see they are not the same type. Now that the problem is explained, lets continue to the possible solutions:

Easiest one, if you want to have it truncated:

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]

This generates the same result as:

lower_red  = np.array([0, int(1.2), int(2.7)])

If you do not want to truncated you can always do round or ceil (floor is the same as truncating).

For example:

avg_saturation = int(np.round(average_saturation(hsv_image)))

or if it is non negative:

avg_saturation = int( average_saturation(hsv_image) + 0.5 )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.