Size of Matrix OpenCV

OpencvOpencv Mat

Opencv Problem Overview


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?

Opencv Solutions


Solution 1 - Opencv

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

cv::Size s = mat.size();
rows = s.height;
cols = s.width;

Solution 2 - Opencv

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);

Solution 3 - Opencv

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.

Solution 4 - Opencv

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

Solution 5 - Opencv

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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLakshmi NarayananView Question on Stackoverflow
Solution 1 - OpencvMichael OView Answer on Stackoverflow
Solution 2 - OpencvVladView Answer on Stackoverflow
Solution 3 - Opencvuser1914692View Answer on Stackoverflow
Solution 4 - Opencvnimesh00View Answer on Stackoverflow
Solution 5 - OpencvMD. Nazmul KibriaView Answer on Stackoverflow