【问题标题】:OpenCV Grid AreaOpenCV 网格区域
【发布时间】:2010-12-09 13:14:19
【问题描述】:

我想使用 OpenCV 从相机中找到图像的非白色区域。我已经可以使用网络摄像头中的图像找到圈子了。我想做一个网格或其他东西,这样我就可以确定图像的百分比不是白色的。有什么想法吗?

【问题讨论】:

    标签: c++ c image-manipulation opencv area


    【解决方案1】:

    如果您想找出图像中非白色像素的百分比,为什么不计算所有非白色像素并将其除以图像中的总像素数?

    C 代码

    #include <stdio.h>
    #include <cv.h>
    #include <cxcore.h>
    #include <highgui.h>
    
    int main()
    {
        // Acquire the image (I'm reading it from a file);
        IplImage* img = cvLoadImage("image.bmp",1);
    
        int i,j,k;
        // Variables to store image properties
        int height,width,step,channels;
        uchar *data;
        // Variables to store the number of white pixels and a flag
        int WhiteCount,bWhite;
    
        // Acquire image unfo
        height    = img->height;
        width     = img->width;
        step      = img->widthStep;
        channels  = img->nChannels;
        data      = (uchar *)img->imageData;
    
        // Begin
        WhiteCount = 0;
        for(i=0;i<height;i++) 
        {
          for(j=0;j<width;j++) 
          { // Go through each channel of the image (R,G, and B) to see if it's equal to 255
            bWhite = 0;
            for(k=0;k<channels;k++)
            {   // This checks if the pixel's kth channel is 255 - it can be faster.
                if (data[i*step+j*channels+k]==255) bWhite = 1;
                else 
                {
                    bWhite = 0;
                    break;
                }
            }
            if(bWhite == 1) WhiteCount++;
          }
        }       
    
        printf("Percentage: %f%%",100.0*WhiteCount/(height*width));
    
        return 0;
    }
    

    【讨论】:

    • 这样做的方法是什么?
    • 您使用的是 OpenCV2.0(即Mat 类)还是旧版本(IplImage*,CvArray*,..)?
    【解决方案2】:

    如果您的图像只有黑白,您可以使用cv::countNonZero 并减去。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 2020-03-29
      • 2018-04-16
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多