سؤال

أريد أن أجد غير المنطقة البيضاء من صورة من الكاميرا باستخدام بنسف.لا يمكن أن تجد بالفعل الدوائر باستخدام الصور من الويب كام.أنا تريد أن تجعل من الشبكة أو شيء لذلك أنا يمكن أن تحدد في المئة من الصورة ليست بيضاء.أي أفكار ؟

هل كانت مفيدة؟

المحلول

إذا كنت تريد أن تجد نسبة وحدات البكسل في الصورة التي ليست بيضاء ، لماذا لا يمكنك الاعتماد فقط كل بكسل التي لا أبيض وقسمته على إجمالي عدد وحدات البكسل في الصورة ؟

التعليمات البرمجية في 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