Precautions for cv2.cvtcolor

error contents

a = np.array([[[255,0,0]]*100]*100)
a = cv2.cvtColor(a, cv2.COLOR_RGB2BGR)
plt.imshow(a)

I got this error.

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:94: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<3, 4>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = (cv::impl::<unnamed>::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'

Solution

a = np.array([[[255,0,0]]*100]*100,np.uint8)
a = cv2.cvtColor(a, cv2.COLOR_RGB2BGR)
plt.imshow(a)

Cause

By default, np.array defaults to int64. However, the argument of cv2.cvtColor () must be uint8, uint16.

reference

https://stackoverflow.com/questions/50319617/opencv-error-cv2-cvtcolor https://postd.cc/image-processing-101/

Recommended Posts

Precautions for cv2.cvtcolor
Precautions for integer rounding by casting
Precautions for handling png and jpg images
Precautions when using for statements in pandas
Precautions when using tf.keras.layers.TimeDistributed for tf.keras custom layer
[2020 version] Development procedure for personal crawlers and precautions