OpenCV: how to get the number of pixels?

How to get the number of pixels in an image? Below 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); } 
+4
source share
2 answers

If you want the total number of pixels, use cv::Mat::total() .

 int nPixels = m.total(); 

Please note that for multi-channel images, the number of pixels is different from the number of elements in the array. Each pixel is most often between one (i.e. shade of gray) and four (i.e. BGRA) elements per pixel.

+15
source

Use this

 int nPixels = (m.cols*m.channels())*m.rows; cout << nPixels << endl; 
+1
source

Source: https://habr.com/ru/post/1485094/


All Articles