Hello,
If you are interested in :
When you get colour image (RGB format), if you want to make some calculus with this image, you may need to get separate channels arrays (2D) instead of the 3D array of the original image.
If you want 3 2D arrays (R, G and B), you will have to split your RGB image.
OpenCV gives you a useful function : cv2.split
ImgR, ImgG, ImgB = cv2.split(ImgRGB)
ImgR, ImgG, ImgB are 2D numpy arrays
This will give you real 2D arrays but cv2.split() cost a lot of time.
If you try this :
ImgR, ImgG, ImgB = ImgRGB[:, :, 0], ImgRGB[:, :, 1], ImgRGB[:, :, 2]
it will be fast but you will be disappointed because you won’t get real 2D arrays and you will get problems when you will use those arrays in a function which needs 2D arrays.
To avoid cv2.split() use with numpy or cupy, you can use numpy.ascontiguousarray() or cupy.ascontiguousarray().
As i want to use cupy instead of numpy, here are some useful functions that will split 1 3D array into 3 2D arrays :
import cupy as cp
import numpy as np
def cupy_RGBImage_2_cupy_separateRGB(cupyImageRGB):
cupy_B = cp.ascontiguousarray(cupyImageRGB[:,:,0], dtype=cp.uint8)
cupy_G = cp.ascontiguousarray(cupyImageRGB[:,:,1], dtype=cp.uint8)
cupy_R = cp.ascontiguousarray(cupyImageRGB[:,:,2], dtype=cp.uint8)
return cupy_B,cupy_G,cupy_R
def numpy_RGBImage_2_numpy_separateRGB(numpyImageRGB):
numpy_B = np.ascontiguousarray(numpyImageRGB[:,:,0], dtype=np.uint8)
numpy_G = np.ascontiguousarray(numpyImageRGB[:,:,1], dtype=np.uint8)
numpy_R = np.ascontiguousarray(numpyImageRGB[:,:,2], dtype=np.uint8)
return numpy_B,numpy_G,numpy_R
def numpy_RGBImage_2_cupy_separateRGB(numpyImageRGB):
cupyImageRGB = cp.asarray(numpyImageRGB)
cupy_B = cp.ascontiguousarray(cupyImageRGB[:,:,0], dtype=cp.uint8)
cupy_G = cp.ascontiguousarray(cupyImageRGB[:,:,1], dtype=cp.uint8)
cupy_R = cp.ascontiguousarray(cupyImageRGB[:,:,2], dtype=cp.uint8)
return cupy_B,cupy_G,cupy_R
def cupy_RGBImage_2_numpy_separateRGB(cupyImageRGB):
cupy_B = cp.ascontiguousarray(cupyImageRGB[:,:,0], dtype=cp.uint8)
cupy_G = cp.ascontiguousarray(cupyImageRGB[:,:,1], dtype=cp.uint8)
cupy_R = cp.ascontiguousarray(cupyImageRGB[:,:,2], dtype=cp.uint8)
numpy_B = cupy_B.get()
numpy_G = cupy_G.get()
numpy_R = cupy_R.get()
return numpy_B,numpy_G,numpy_R
Using cupy to split a 3D array is at least as fast as cv2.split() (in fact, cv2.cuda.split()).
With JetsonSky V40_XX, as i mainly use cupy, my GPU load (GTX1060 or Jetson AGX Orin GPU) has dramatically increase (about 40% with heavy filtering instead of about 10 to 20% with pycuda JetsonSky old versions).
That’s all.
Alain