如何计算图像像素

how to count image pixel

本文关键字:图像 像素 计算 何计算      更新时间:2023-10-16

我想知道如何应用这个"水平位置,从图像左边缘开始计算像素,最小矩形框的中心,可以用框内所有"上"像素绘制。请注意,框==图像

使用 OpenCV 代码

任何帮助

我不确定我是否正确理解了你,但这是我计算图像像素的建议。

我在下面使用的垫子图像是灰度的,如果要计算 3 通道 RGB 图像上的像素,则需要拆分其通道并单独处理它们。

Mat image= imread( "C:\1.jpg" );// load your image by giving the path here.
Mat grayscale_image;
cvtColor(image,grayscale_image,CV_RGB2GRAY);/* this converts "image" to 
"grayscale_image" which is a one channel image.*/
//Now you can play with its pixels
 int sumPixels=0;
                     for(int i=0; i<image.rows; i++)
                     {
                         for(int j=0;j<image.cols;j++)
                                sumPixels+=image.at<uchar>(i,j);                            
                     }
/* value of a pixel on a gray scale image(if it is a 8 bit image) varies between 
   0 and 255. "sumPixels" variable will contain the total pixel values of image.*/

此代码尝试通过将每列的所有白色像素相加来查找每行的白色像素。

    int sum_of_x = 0;
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        { 
            if(data[i*step + j]==255)
                sum_of_x = sum_of_x + 1;
        }
        cout<<"Row No #"<<i<<", White Pixel at each ROW = "<<sum_of_x<<endl;
    }

干杯。