Convert numpy array of 3*n*n images to 1*n*n?

Viewed 228

I have 800 images stored in numpy array of size (800, 3,256, 256), which I want to convert to (800, 1,256, 256).

I tried using cv2 library to achieve this but I am getting an error. What the easiest way to implement this?

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
2 Answers

As pointed by Elyas Karimi cv2.cvtColor expects the input images to be 256x256x3 and not 3x256x256. You can transpose the images back and forth.
However, since converting from RGB to gray is a rather simple operation:

0.2989 * R + 0.5870 * G + 0.1140 * B

You can explicitly and efficiently do the conversion:

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

In many cases the exact weight of each channel is not too critical, and a simple mean over channels can do just fine:

train_img = train_img.mean(axis=1)

Correct me if i am wrong, you have images of 256*256 and they have 3 channels making it 256*256*3 Now you want to make them one channeled as 256*26*1. By losing channel information, you are converting RGB/BGR image into grey scale. Now to do that with OpenCV you will need to make them 256*256*3, it wont work on 3*256*256

So to do that lets take

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

Now pass this into OpenCv cvt.color

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

OR Use this

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