OpenCV: How to get the number of pixels?
15,869
Solution 1
If you want the total number of pixels, use cv::Mat::total()
.
int nPixels = m.total();
Note that for multi-channeled images, the number of pixels is distinct from the number of elements in the array. Each pixel most commonly has between one (i.e. greyscale) and four (i.e. BGRA) elements per pixel.
Solution 2
Use this
int nPixels = (m.cols*m.channels())*m.rows;
cout << nPixels << endl;
Author by
System.Windows.Form
Updated on July 27, 2022Comments
-
System.Windows.Form almost 2 years
How to get the number of pixels in an image? Following is my code, and I need to get the total number of pixels in Mat "m".
int main() { Mat m = imread("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"); namedWindow("Image"); imshow("Image",m); waitKey(0); }
-
Aurelius almost 11 yearsThis gives the number of elements in the data buffer, but not the number of pixels. (Except for single-channel images, that is.)