156

I know this might be very rudimentary, but I am new to OpenCV. Could you please tell me how to obtain the size of a matrix in OpenCV?. I googled and I am still searching, but if any of you know the answer, please help me.

Size as in number of rows and columns.

And is there a way to directly obtain the maximum value of a 2D matrix?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92

5 Answers5

283
cv:Mat mat;
int rows = mat.rows;
int cols = mat.cols;

cv::Size s = mat.size();
rows = s.height;
cols = s.width;
Michael O
  • 3,335
  • 1
  • 16
  • 9
21

Note that apart from rows and columns there is a number of channels and type. When it is clear what type is, the channels can act as an extra dimension as in CV_8UC3 so you would address a matrix as

uchar a = M.at<Vec3b>(y, x)[i];

So the size in terms of elements of elementary type is M.rows * M.cols * M.cn

To find the max element one can use

Mat src;
double minVal, maxVal;
minMaxLoc(src, &minVal, &maxVal);
Vlad
  • 4,425
  • 1
  • 30
  • 39
15

For 2D matrix:

mat.rows – Number of rows in a 2D array.

mat.cols – Number of columns in a 2D array.

Or: C++: Size Mat::size() const

The method returns a matrix size: Size(cols, rows) . When the matrix is more than 2-dimensional, the returned size is (-1, -1).

For multidimensional matrix, you need to use

int thisSizes[3] = {2, 3, 4};
cv::Mat mat3D(3, thisSizes, CV_32FC1);
// mat3D.size tells the size of the matrix 
// mat3D.size[0] = 2;
// mat3D.size[1] = 3;
// mat3D.size[2] = 4;

Note, here 2 for z axis, 3 for y axis, 4 for x axis. By x, y, z, it means the order of the dimensions. x index changes the fastest.

user1914692
  • 3,033
  • 5
  • 36
  • 61
  • 2
    just to be clear, there is no `Mat::size()` member method, rather a [`Mat::size`](https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1) member variable of type [`MatSize`](https://docs.opencv.org/3.4/df/d63/structcv_1_1MatSize.html). The latter overloads the parentheses operator [`MatSize::operator()`](https://docs.opencv.org/3.4/df/d63/structcv_1_1MatSize.html#abdc35c3d1d636243a9fa4b7e3a4810fe) to return a [`Size`](https://docs.opencv.org/3.4/d6/d50/classcv_1_1Size__.html) object – Amro Feb 02 '19 at 20:59
  • As of today, I do use the `Mat::size()` function of opencv 4.7. So I believe this function added later after year 2019. – ollydbg23 Mar 21 '23 at 01:57
5

If you are using the Python wrappers, then (assuming your matrix name is mat):

  • mat.shape gives you an array of the type- [height, width, channels]

  • mat.size gives you the size of the array

Sample Code:

import cv2
mat = cv2.imread('sample.png')
height, width, channel = mat.shape[:3]
size = mat.size
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
nimesh00
  • 73
  • 1
  • 5
4

A complete C++ code example, may be helpful for the beginners

#include <iostream>
#include <string>
#include "opencv/highgui.h"

using namespace std;
using namespace cv;

int main()
{
    cv:Mat M(102,201,CV_8UC1);
    int rows = M.rows;
    int cols = M.cols;

    cout<<rows<<" "<<cols<<endl;

    cv::Size sz = M.size();
    rows = sz.height;
    cols = sz.width;

    cout<<rows<<" "<<cols<<endl;
    cout<<sz<<endl;
    return 0;
}
Eldelshell
  • 6,683
  • 7
  • 44
  • 63
MD. Nazmul Kibria
  • 1,080
  • 10
  • 21