8

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


}
4

2 回答 2

24

如果您想要总像素数,请使用cv::Mat::total().

int nPixels = m.total();

请注意,对于多通道图像,像素数与数组中的元素数不同。每个像素最常见的每个像素具有一个(即灰度)和四个(即BGRA)元素。

于 2013-06-07T18:32:56.140 回答
1

用这个

int nPixels = (m.cols*m.channels())*m.rows;
cout << nPixels << endl;
于 2013-06-07T18:20:17.987 回答