32

I am trying to convert a normalised RGB image to HSV or LAB colour space. Here is the normalisation function:enter image description here

here is the basic code

print ('original image shape: ', image.shape)
print ('normlaised image shape: ', needed_multi_channel_img.shape)
# Converting to LAB color space


lab_image = cv.cvtColor(needed_multi_channel_img, cv.COLOR_RGB2HSV)

cv.imshow('Lab_space.jpg',lab_image.astype('float32'))
cv.waitKey(0)
cv.destroyAllWindows()

Here is the output trace:

    /home/centura/gitlab/Acne_model/Acne Model/rosaceaexperiment1.py:82: RuntimeWarning: invalid value encountered in true_divide
  norm.append(image[i,j,a]/rooted_matrix[i,j])
/home/centura/gitlab/Acne_model/Acne Model/rosaceaexperiment1.py:82: RuntimeWarning: divide by zero encountered in true_divide
  norm.append(image[i,j,a]/rooted_matrix[i,j])
original image shape:  (375, 600, 3)
normlaised image shape:  (375, 600, 3)
Traceback (most recent call last):
  File "/home/centura/gitlab/Acne_model/Acne Model/rosaceaexperiment1.py", line 121, in <module>
    lab_image = cv.cvtColor(needed_multi_channel_img, cv.COLOR_RGB2HSV)
cv2.error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/color.hpp:257: error: (-2:Unspecified error) in function 'cv::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::Set<3, 4>; VDcn = cv::Set<3>; VDepth = cv::Set<0, 5>; cv::SizePolicy sizePolicy = (cv::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Unsupported depth of input image:
>     'VDepth::contains(depth)'
> where
>     'depth' is 6 (CV_64F)

For zero division error, I have replaced it with 0 and nan is also replaced with 0.

I also searched through StackOverflow but could not find any information to debug it. I do not understand the meaning of this error and how to rectify it.

6
  • 15
    your image type is CV_64F (float64) which is not supported by cvtColor function. Convert the image to an appropriate type, e.g. float32 Commented Mar 15, 2019 at 9:49
  • 1
    is there a simple way to convert it to float32. Commented Mar 15, 2019 at 10:11
  • 1
    like any other numpy array, with astype Commented Mar 15, 2019 at 10:12
  • 1
    i tried it normal_image[:,:,0] = normal_image[:,:,0].astype(np.float32) normal_image[:,:,1] = normal_image[:,:,1].astype(np.float32) normal_image[:,:,2] = normal_image[:,:,2].astype(np.float32) still the error persists. Commented Mar 15, 2019 at 10:18
  • Is there any other method. I am still getting error @Miki Commented Mar 15, 2019 at 10:34

2 Answers 2

30

According to this answer https://stackoverflow.com/a/45956247/7683041, try:

img_float32 = np.float32(needed_multi_channel_img)
lab_image = cv.cvtColor(img_float32, cv.COLOR_RGB2HSV)
Sign up to request clarification or add additional context in comments.

Comments

1

I actually had a similar issue, the problem was that the input was in floats but it was expecting integers. when i converted to integers (.astype('int')) this error disappeared. maybe I'll help you too.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.