gpt4 book ai didi

python - 将 3*n*n 图像的 numpy 数组转换为 1*n*n?

转载 作者:行者123 更新时间:2023-12-04 08:33:43 24 4
gpt4 key购买 nike

我有 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

最佳答案

正如 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, ...]
在许多情况下,每个 channel 的确切权重不是很关键,一个简单的 mean通过 channel 可以做得很好:
train_img = train_img.mean(axis=1)

关于python - 将 3*n*n 图像的 numpy 数组转换为 1*n*n?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64906007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com