Description
I am trying to convert a numpy array and then, apply a color map to a 2d numpy array that’s filled with float values between 0 – 1. I have written this piece of code for that:
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
arr[i,j] = arr[i,j] * 255
arr[i,j] = int(round(arr[i,j]))
arr.astype(np.uint)
colorMapImage = cv2.cvtColor(arr,cv2.COLOR_GRAY2BGR)
colorMapImage = cv2.applyColorMap(colorMapImage, cv2.COLORMAP_VIRIDIS)
But, when I try to run the code I see the following error:
[ERROR] [1719390004.370940]: Error processing image: OpenCV(4.2.0) /home/myPC/opencv_build/opencv/modules/imgproc/src/colormap.cpp:714: error: (-5:Bad argument) cv::ColorMap only supports source images of type CV_8UC1 or CV_8UC3 in function 'operator()'
Notes
-
The numpy array was normalized to interval 0 – 1 before the conversion to OpenCV image.
-
Shape of arr is (300, 300)
-
When I print colorMapImage[0,0] just before applying color map, It prints: [5. 5. 5.]
-
When I print out arr.dtype(), I see the following error:
Error processing image: 'numpy.dtype[float32]' object is not callable
-
When I print types of both arr and colorMapImage with
print(str(type()))
, it prints, <class ‘numpy.ndarray’> for both.