18641

How to divide 3D matrix into bunches of 2D matrix with opencv C++

Question:

I have a, for example, 4*5*6 3D matrix. I would like to divide it into 6 2D matrix. The purpose is to make data operation on these 2D matrix and get results. Tried row(), rowRange(), I got errors. No clues right now. Anyone throw any better ideas? Thanks~

Answer1:

Remember that last index varies fastest, so maybe you mean you have a 6*5*4 Mat and would like to divide it into six 5x4 Mats. According to the <a href="http://docs.opencv.org/modules/core/doc/basic_structures.html#mat" rel="nofollow">documentation</a> "3-dimensional matrices are stored plane-by-plane".

However, assuming your 3D Mat was created like this:

int dims[] = {4, 5, 6}; Mat m3(3, dims, CV_8UC1, data);

You can do something like this to do what you asked (but possibly not what you actually want):

Mat m2(4, 30, CV_8UC1, m3.data); Mat m2x6 = m2.reshape(6); std::vector<cv::Mat> channels; cv::split(m2x6, channels);

However, to get out 4 images from m3 that have 5 rows x 6 cols:

Mat p0(5, 6, CV_8UC1, m3.data + m3.step[0] * 0); Mat p1(5, 6, CV_8UC1, m3.data + m3.step[0] * 1); Mat p2(5, 6, CV_8UC1, m3.data + m3.step[0] * 2); Mat p3(5, 6, CV_8UC1, m3.data + m3.step[0] * 3);

Because support for 3D Mats in OpenCV is not great, avoid using them if you can. An alternative would be to use a 2D Mat that has multiple channels. That is often much easier to handle.

Recommend

  • GoogleTest Fixture for blitz++ class with arguments in constructor
  • Create image from RGB data?
  • How to get images from raster bands using OpenCV?
  • Recursive formula for skewness in F#
  • Getting AWS Instance limits via console
  • Excel - leave blank if reference cell is blank
  • Combining year, month, day columns into a single date column with pandas [duplicate]
  • Dividing a value by number of days in a month in a date field in SQL table
  • Is covariant return type overriding according jls?
  • AspectJ load time weaving not working for Spring beans
  • How to delegate mouse events to all underlying overlapped panes in JavaFX?
  • Using outer to produce an array of lists
  • Print ggplot y axis values in 10 thousands
  • C comparison of array of pointers
  • Using numpy.unique on multiple columns of a pandas.DataFrame
  • Matrix power for sparse matrix in python
  • SQL Server 2008 INSERT Optimization
  • Using Java's built-in Set classes to count EACH(keyword) unique element in List values from a l
  • what does Tilde image mean in MATLAB?
  • Multiply all columns of one matrix by another matrix with bsxfun
  • How to read csv file into an Array of arrays in scala
  • Fastest array lookup algorithm in C for embedded system?
  • What is the equivalent of Matlab's imadjust in python?
  • Check if coordinate in selected area
  • Doctrine DQL returning flat array from join
  • Fold function in Octave
  • Resource for understanding view, projection, 'virtual camera' in OpenGL or graphics in gen
  • Multiplying column elements of sparse Matrix
  • Creating BackgroundSubtractorMOG2 in Android by OpenCV 3.0 RC1
  • How to transform Byte[](decoded as PNG or JPG) to Tensorflows Tensor
  • C++ Armadillo Access Triangular Matrix Elements
  • Runtime error in UVA Online Judge [closed]
  • perl, mysql - fasting way to upload a csv file into mysql?
  • Java applet as stand-alone Windows application?
  • Jquery - Jquery Wysiwyg return html as a string
  • SQL merge duplicate rows and join values that are different
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • Reading document lines to the user (python)
  • java string with new operator and a literal