I am a newbie to openCV and I am stuck at this error with no resolution. I am trying to convert an image from BGR to grayscale format using this code-
img = cv2.imread('path//to//image//file')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
This seems to be working fine. I checked the data type of the img
variable which turns out to be numpy ndarray and shape to be (100,80,3)
. However if I give an an image already present in code of numpy ndarray data type and of same dimensions as input to the cvtColor
function, it gives me the following error-
Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file D:\Build\OpenCV\opencv-3.4.1\modules\imgproc\src\color.cpp, line 11109
cv2.error: OpenCV(3.4.1) D:\Build\OpenCV\opencv-3.4.1\modules\imgproc\src\color.cpp:11109: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor
The code for the second case is (making a custom np.ndarray over here)-
img = np.full((100,80,3), 12)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Can anyone clarify what is the reason for this error and how to rectify it?
Kenil Vasani
This is because your numpy array is not made up of the right data type. By default makes an array of type
np.int64
(64 bit), however,cv2.cvtColor()
requires 8 bit (np.uint8
) or 16 bit (np.uint16
). To correct this change yournp.full()
function to include the data type:img = np.full((100,80,3), 12, np.uint8)