2
votes

I'm trying to convert an image from RGB to HSV color space, here's a segment of my code:

import cv2
import numpy as np
import imutils

lower = np.array([0, 48, 80], dtype= "uint8")
upper = np.array([20, 255, 255], dtype= "uint8")

img = cv2.imread('3.JPG', 0)
img = imutils.resize(img, width = 400)
cv2.waitKey(1)

converted = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
new_skinMask = cv2.inRange(converted, lower, upper)

but I'm getting an error on:

converted = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

the error is:

OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cv::cvtColor

can anybody help me with this?

1
Could you post your error as well? - tartaruga_casco_mole
I edited my question and included the error, thanks for pointing that out :)) - user3818372
seems like your input failed the assertion. img should have 3 or 4 channels. You should check it. - tartaruga_casco_mole

1 Answers

3
votes

Last parameter in

img = cv2.imread('3.JPG', 0)

stands for flags, and 0 is equal to CV_LOAD_IMAGE_GRAYSCALE (or IMREAD_GRAYSCALE). That's why you get the assertion about the number of channels.

If you want to load the image in color either:

  • remove this parameter, using default IMREAD_COLOR flag, or
  • replace it with CV_LOAD_IMAGE_COLOR (or IMREAD_COLOR).

Consider the doc for more detail.