简体   繁体   中英

Size of Matrix (image) OpenCV

How to get the dimensions of an image (width and height)?

I just saw this answer , which shows two methods, and doesn't explicitly state if they are completely equivalent:

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

Are those two methods completely equivalent in every case?

What are the differences if there are any?


OpenCV version: 4

The two methods are exactly the same. I believe these functions exist for convenience since there are several cases where each method is useful.

For example, to create a new cv::Mat of the same size as another, its more convenient to write and easier to read

cv::Mat new_mat = cv::Mat(old_mat.size(), old_mat.type()) 

than

cv::Mat new_mat = cv::Mat(old_mat.rows, old_mat.cols, old_mat.type()

Also, fewer function calls I believe. And it's more convenient to use and easier to read with fewer function calls

for(int i=0; i<old_mat.rows; i++)
   for(int j=0; l< old_mat.cols; j++)

than

for(int i=0; i<old_mat.size().width; i++)
   for(int j=0; j<old_mat.size().height; j++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM