Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV网格区域_C++_C_Image Manipulation_Opencv_Area - Fatal编程技术网

C++ OpenCV网格区域

C++ OpenCV网格区域,c++,c,image-manipulation,opencv,area,C++,C,Image Manipulation,Opencv,Area,我想使用OpenCV从相机中找到图像的非白色区域。我已经可以使用我的网络摄像头中的图像找到圆圈。我想做一个网格或其他东西,这样我就可以确定图像的百分比不是白色的。有什么想法吗?如果你想找出图像中非白色像素的百分比,为什么不直接计算所有非白色像素,然后除以图像中的像素总数 用C编写代码 #include <stdio.h> #include <cv.h> #include <cxcore.h> #include <highgui.h> int ma

我想使用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;
}
#包括
#包括
#包括
#包括
int main()
{
//获取图像(我正在从文件中读取);
IplImage*img=cvLoadImage(“image.bmp”,1);
int i,j,k;
//用于存储图像属性的变量
整数高度、宽度、步长、通道;
uchar*数据;
//用于存储白色像素数和标志的变量
int白计数,bWhite;
//获取图像unfo
高度=img->height;
宽度=img->width;
步长=img->widthStep;
通道=img->n通道;
数据=(uchar*)img->imageData;
//开始
白细胞计数=0;

对于(i=0;i您可以使用
cv::countNonZero
并在图像只有黑白时进行减法。

这样做的方法是什么?您使用的是OpenCV2.0(即
Mat
类)还是旧版本(
IplImage*
CvArray*
,…)?