【问题标题】:how to count image pixel [closed]如何计算图像像素[关闭]
【发布时间】:2012-06-07 15:59:47
【问题描述】:

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

使用opencv代码

任何帮助

【问题讨论】:

  • 我觉得你的问题不够清楚。
  • 不太明白您要交付什么。
  • @MizukiKai 我想知道如何计算图像中的 x 像素图像中的所有点都有 P(x,y) 如何计算 x 像素

标签: c++ image image-processing opencv


【解决方案1】:

我不确定我是否理解正确,但这是我对图像中的像素进行计数的建议。

我下面使用的Mat图像是灰度的,如果你想在一张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.*/

【讨论】:

  • 感谢重播,但是我如何定义 mat 图像,请详细说明以了解如何。
  • 我按照你的要求编辑了答案
  • 谢谢谢谢,但我需要知道像素坐标 x 和 y 并对 x 坐标和 y 坐标求和
  • 哪些像素?您应该从代码中理解它,它已经在上面的代码中。 i 和 j 变量用于坐标。也许你应该从头开始 OpenCV。
  • 我尝试你的代码并得到错误是 Mat undefined ,什么是类类型或标题库来定义 Mat 和 cvtColor
【解决方案2】:

此代码尝试通过对每一列的所有白色像素求和来找到每一行的白色像素。

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

干杯。

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多