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

有帮助吗?

解决方案

如果你想找到图像中不是白色像素的百分比,你为什么不计算所有不是白色的像素并将它除以图像中的像素总数?

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

其他提示

您可以使用 cv :: countNonZero ,如果您的图片只是黑白,则减去。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top