2

My original python code reads an image and transpose it using:

image = cv2.imread(image_name)
image = numpy.transpose(image, (2, 0, 1))

So the original 160x160x3 BGR image would be 3x160x160.

I'd like to implement this functionality in c++ but have no idea how to do transpose on cv::Mat object.

Any help?

Miki
  • 40,887
  • 13
  • 123
  • 202
Wenbin Xu
  • 134
  • 2
  • 14
  • opencv has a [`transpose`](https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#transpose) method already, did you try this? – EdChum Mar 12 '19 at 09:06
  • In RGB cv::Mat with size 3x3 the data is contained in the linear piece of memory (I added indexing): > R00G00B00 R01G01B01 R02G02B02 R10G10B10 R11G11B11 R12G12B12 R20G20B20 R21G21B21 R22G22B22 What do you want see after transpose? – Nuzhny Mar 12 '19 at 09:20
  • @Miki The cv::transpose function is not an analog numpy.transpose – Nuzhny Mar 12 '19 at 09:21
  • @Nuzhny I'm not familiar with c++ or memory allocation. According to your indexing, seems that cv in c++ reads the image the same as in python. They both store the image in a matrix of height width channel order. Assume the original image was stored in your indexing scheme, what I want is transpose the matrix and make it like (assuming the image is of size (h, w, 3)): R00R01R02R03R04....R0w....R10R11R12R13....R1w....Rh0Rh1Rh2Rh3....Rhw, G00G01G02G03G04....G0w....G10G11G12G13....G1w....Gh0Gh1Gh2Gh3....Ghw, B00B01B02B03B04....B0w....B10B11B12B13....B1w....Bh0Bh1Bh2Bh3....Bhw. – Wenbin Xu Mar 13 '19 at 01:22
  • 1
    @EdChum Thank you for your reply, cv::transpose seems to only work for 2 dimensional matrix, but what I want to transpose a 3-dimensional matrix. – Wenbin Xu Mar 13 '19 at 01:28
  • Please check my answer here: https://stackoverflow.com/questions/46017418/how-can-i-permute-dimensions-in-cvmat-from-cxwxh-to-wxhxc/76270351#76270351 – Chaohsiung Huang May 17 '23 at 09:07

0 Answers0