【问题标题】:Convert numpy array of 3*n*n images to 1*n*n?将 3*n*n 图像的 numpy 数组转换为 1*n*n?
【发布时间】:2020-11-19 05:42:01
【问题描述】:

我有 800 张图像存储在大小为 (800, 3,256, 256) 的 numpy 数组中,我想将其转换为 (800, 1,256, 256)。

我尝试使用 cv2 库来实现这一点,但出现错误。最简单的实现方法是什么?

train_img[i]= cv2.cvtColor(train_img[i], cv2.COLOR_BGR2RGB)
train_img[i]= cv2.cvtColor(train_img[i], cv2.COLOR_BGR2GRAY)
Traceback (most recent call last):

  File "<ipython-input-82-e993ce67611f>", line 3, in <module>
    train_img[i]= cv2.cvtColor(train_img[i], cv2.COLOR_BGR2RGB)

error: C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp:11010: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor



OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11016
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
2020-11-19 05:55:59.003947: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-11-19 05:56:13.086709: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-11-19 05:56:13.088369: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
2020-11-19 05:56:13.108395: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-FAJP7DN
2020-11-19 05:56:13.109348: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-FAJP7DN
2020-11-19 05:56:13.125396: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010

【问题讨论】:

  • 是3256还是3和256
  • 所以在第二维中你有 3 个元素,你的图像是 256*256 像素,如果有错误请纠正我。为了使用cv2.cvtColor(),您需要您的数组为每个图像的形状 (256,256,3),而不是 (3,256,256)。

标签: python computer-vision opencv3.0


【解决方案1】:

正如Elyas Karimi 所指出的,cv2.cvtColor 期望输入图像是256x256x3 而不是3x256x256。您可以来回transpose 图片。
但是,由于从 RGB 转换为灰色是一个相当简单的操作:

0.2989 * R + 0.5870 * G + 0.1140 * B

您可以明确有效地进行转换:

# assuming your input is in BGR order
train_img = 0.289 * train_img[:, 2, ...] + 0.587 * train_img[:, 1, ...] + 0.114 * train_img[:, 0, ...]

在许多情况下,每个通道的确切权重并不太重要,一个简单的mean over channels 就可以了:

train_img = train_img.mean(axis=1)

【讨论】:

    【解决方案2】:

    如果我错了,请纠正我,你有 256*256 的图像,他们有 3 个频道使它成为 256*256*3 现在您想让它们成为256*26*1 的一个通道。 通过丢失通道信息,您将 RGB/BGR 图像转换为灰度。现在要使用 OpenCV 做到这一点,您需要将它们设为 256*256*3,它不适用于 3*256*256

    所以让我们采取行动

     from numpy import moveaxis
     #image[i].shape=3*256*256
     data = moveaxis(image[i], 2, 0)
     #data.shape=256*256*3 
     
    

    现在将它传递给 OpenCv cvt.color

    data= cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
    data= cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
    

    或者使用这个

     data = 0.289 * data[:, 2, ...] + 0.587 * data[:, 1, ...] + 0.114 * data[:, 0, ...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 2013-07-26
      相关资源
      最近更新 更多