문제

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