How to convert 3d array to 4d array using Mat::reshape

asked 2019-04-16 21:39:28 -0600

iamM gravatar image

Hi, I have 3d RGB array I need to convert it to 4d RGBA array using reshape how to do that kindly help me. Basically x = np.reshape(x, (1,64,64,3)) this python operation i have to do in c++.

edit retag flag offensive close merge delete

Comments

what is the purpose of it ? do you need seperate color planes ?

berak gravatar imageberak ( 2019-04-17 01:37:54 -0600 )edit

Because I need to pass is array to tvm compiled module, tvm module will take input as 4 dimension array in python i am reshaping it using numpy.reshape like (64, 64, 3) to (1. 64, 64, 3).

iamM gravatar imageiamM ( 2019-04-17 02:03:22 -0600 )edit

forget numpy for a moment, what about the order ? NHWC or NCHW ?

do you have some more hints on how you intend to use that ? (does twm even work with cv::Mat ?)

berak gravatar imageberak ( 2019-04-17 02:08:17 -0600 )edit

Yes, I am using cv::Mat read and store the image, it will store the image in cv::Mat as (64, 64, 3) here col = 64, rows = 64 and channels = 3. I have to reshape it to (1, 64, 64, 3).

Basically, I am doing in python:

x = Image.open('cat.ppm')
x = np.array(x)
x = 2. * x / 255. - 1
x = np.reshape(x, (1,64,64,3))

similarly, I have to in c++ using opencv

Mat rgbaImage = imread("cat.ppm");
Mat rgbImage;
cvtColor(rgbaImage, rgbImage, COLOR_RGBA2RGB);
Mat fltImage;
rgbImage.convertTo(fltImage, CV_32FC3, 2.0/255.0, -1.0);
fltImage.reshape() // here i need to reshape it to 4D array like  (1, 64, 64, 3)
iamM gravatar imageiamM ( 2019-04-17 04:09:50 -0600 )edit

does it need "interleaved pixels" or "seperate color channels" ?

berak gravatar imageberak ( 2019-04-17 04:29:36 -0600 )edit