Size of Matrix OpenCV
Asked Answered
N

5

157

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?

Nanceynanchang answered 25/12, 2012 at 6:15 Comment(0)
I
286
cv:Mat mat;
int rows = mat.rows;
int cols = mat.cols;

cv::Size s = mat.size();
rows = s.height;
cols = s.width;
Illustrious answered 25/12, 2012 at 6:29 Comment(5)
Link to documentation: docs.opencv.org/modules/core/doc/basic_structures.html#mat-sizeMaryalice
Are those two exact equivalents?Supportive
What's the difference between those? The note seems confusing, mentioning a stride for some reason.Sleeping
Yeah, someone edited the answer to add the weirdness about stride. I've rolled that back.Illustrious
Why does the documentation list size as a public attribute and not as a function like you have it? docs.opencv.org/4.x/d3/d63/…Telluride
T
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);
Talamantes answered 23/2, 2014 at 7:19 Comment(1)
This is the only answer that also addresses finding the largest element in an OpenCV Mat.Xenocrates
I
16

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.

Impede answered 16/6, 2016 at 14:43 Comment(2)
just to be clear, there is no Mat::size() member method, rather a Mat::size member variable of type MatSize. The latter overloads the parentheses operator MatSize::operator() to return a Size objectPedestrianize
As of today, I do use the Mat::size() function of opencv 4.7. So I believe this function added later after year 2019.Privative
A
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
Amundsen answered 15/12, 2017 at 21:23 Comment(0)
D
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;
}
Drumlin answered 20/10, 2016 at 6:6 Comment(2)
how do we get the depth of the matrix in cpp?Arie
.channels() command will give you depthLuzluzader

© 2022 - 2024 — McMap. All rights reserved.