Vishal Neekhra
Vishal Neekhra

Reputation: 43

cv2.COLOR_BGR2Lab is not working but cv2.COLOR_BGR2GRAY is working

I am trying to run this code, and it will give error:

import cv2

import numpy as np

img=cv2.imread('image1.jpg',cv2.IMREAD_COLOR)
hsl = cv2.cvtColor(img,cv2.COLOR_BGR2Lab)
cv2.imshow('image',hsl)
cv2.imwrite('hsl.jpg',hsl)
cv2.waitKey(0)
cv2.destroyAllWindows()

The exception:

Traceback (most recent call last):
File "ques3.py", line 7, in <module>
hsl = cv2.cvtColor(img,cv2.COLOR_BGR2Lab)
AttributeError: 'module' object has no attribute 'COLOR_BGR2Lab'

If I change cv2.COLOR_BGR2Lab to cv2.COLOR_BGR2GRAY, it run succesfully.

Why does this happen?.

Upvotes: 2

Views: 7415

Answers (1)

CristiFati
CristiFati

Reputation: 41177

The error (as the last line of the exception thrown indicates) is that the CV2 (Python) module doesn't export a constant named COLOR_BGR2Lab, although [OpenCV.Docs]: Color conversions - RGB <-> CIE L*a*b* states that it should.
According to some URL that no longer exists, in a previous (ancient) version the constant was named COLOR_BGR2LAB (Python is case sensitive).

In any case, if you want to see the names that a module exports (in this case for CV2) you might use:

import cv2
print(dir(cv2))

Upvotes: 2

Related Questions