简体   繁体   English

OpenCV:如何获得像素数?

[英]OpenCV: How to get the number of pixels?

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". 以下是我的代码,我需要得到Mat“m”中的总像素数。

int main()
{
    Mat m = imread("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");


    namedWindow("Image");
    imshow("Image",m);



    waitKey(0);


}

If you want the total number of pixels , use cv::Mat::total() . 如果需要总像素数,请使用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 (ie greyscale) and four (ie BGRA) elements per pixel. 每个像素最通常在每个像素之间具有一个(即灰度)和四个(即BGRA)元素。

Use this 用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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