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.
imagine you have a function called preprocessing() that preprocess your images with cv2, if you try to apply it as:
data = np.array(list(map(preprocessing,data)))
it won't work and that because np.array creates int64and you are trying to assign np.uint8 to it, what you should do instead is adding dtype parameter as follow:
data = np.array(list(map(preprocessing,data)), dtype = np.uint8)
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 your np.full()
function to include the data type:
img = np.full((100,80,3), 12, np.uint8)
The error occured because the datatype of numpy array returned by cv2.imread
is uint8
, which is different from the datatype of numpy array returned by np.full()
. To make the data-type as uint8, add the dtype
parameter-
img = np.full((100,80,3), 12, dtype = np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
It may be easier to initialize new numpy array with initial image as source and dtype=np.uint8
:
import numpy as np
img = cv2.imread('path//to//image//file')
img = np.array(img, dtype=np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)