Help us understand the problem. What is going on with this article?

cv2.cvtcolorの注意点

エラー内容

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

こんなエラーが出た.

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&]'

解決

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

原因

np.arrayはデフォルトでdtypeがint64となる。しかし、cv2.cvtColor()の引数はuint8,uint16でなくてはならない。

参考

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

161abcd
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away