OpenCV を使用して、カメラからの画像の白以外の領域を見つけたいです。Web カメラからの画像を使用して、既に円を見つけることができます。画像のパーセントが白ではないことを確認できるように、グリッドなどを作成したいと考えています。何か案は?
2715 次
2 に答える
4
画像内の白ではないピクセルの割合を知りたい場合は、白ではないすべてのピクセルを数えて、画像内のピクセルの総数で割ってみませんか?
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;
}
于 2009-10-23T18:13:22.427 に答える
0
cv::countNonZero
画像が白黒のみの場合は、使用および減算できます。
于 2011-12-16T16:36:27.633 に答える