Home c# How to check it is not empty picture?

How to check it is not empty picture?

Author

Date

Category

There is a list of images. There is a task to determine empty picture or there is an image on it. I found the code for my task http://www.chinhdo.com/20080910/detect- Blank-images / however, it works incorrectly.

public static bool isblank (String ImageFilename)
{
  double stddev = getstddev (imagefilename);
  RETURN STDDEV & LT; 100,000;
}
/// & LT; Summary & GT;
/// Get The Standard Deviation of Pixel Values.
/// & LT; / Summary & GT;
/// & lt; param name = "imagefilename" & gt; Name of the image file. & lt; / Param & gt;
/// & LT; Returns & gt; Standard Deviation. & Lt; / Returns & GT;
Public Static Double GetStddev (String ImageFilename)
{
  Double Total = 0, TotalVariance = 0;
  INT COUNT = 0;
  Double stddev = 0;
  // FIRST GET ALL THE Bytes
  Using (Bitmap B = New Bitmap (ImageFileName))
  {
    BitmapData BMDATA = B.LockBits (New Rectangle (0, 0, B.Width, B.Height), ImageLockMode.Readonly, B.Pixelformat);
    int stride = bmdata.stride;
    Intptr scan0 = bmdata.scan0;
    Unsafe
    {
      byte * p = (byte *) (void *) scan0;
      int noffset = stride - b.width * 3;
      for (int y = 0; y & lt; b.height; ++ y)
      {
        for (int x = 0; x & lt; b.width; ++ x)
        {
          COUNT ++;
          BYTE BLUE = P [0];
          BYTE GREEN = P [1];
          BYTE RED = P [2];
          int pixelvalue = color.fromargb (0, Red, Green, Blue) .toargb ();
          Total + = Pixelvalue;
          Double AVG = Total / Count;
          TotalVariance + = Math.pow (Pixelvalue - AVG, 2);
          STDDev = Math.SQRT (TotalVariance / Count);
          p + = 3;
        }
        p + = noffset;
      }
    }
    b.unlockbits (BMDATA);
  }
  Return stddev;
}

 Enter a description of the image


Answer 1, Authority 100%

One of the ways is to check the standard deviation to the output for some threshold. This threshold must be selected experimentally.

Calculate the standard deviation is the easiest way according to the

formula

{\ sqrt {e [x ^ {2}] - (E [x]) ^ {2} }}

Calculate it will be either separately for each color component – either if the task allows you to work with a black and white image. If it was decided to calculate separately – it will be necessary to come up with a way to combine the result together. For example, you can use the Euclidean norm …

It turns out about such a pseudocode:

float sumr = 0, sumg = 0, sumb = 0, sumr2 = 0, sumg2 = 0, sumb2 = 0 ;
INT COUNT = 0;
For all piszzles pictures
{
  Let R, G, B be - the values ​​of the color component of the current pussy
  SUMR + = R;
  SUMG + = G;
  SUmb + = B;
  Sumr2 + = R * R;
  SUMG2 + = G * G;
  SUmb2 + = b * b;
  COUNT ++;
}
Float Dev = SQRT (SUMR2 + SUMG2 + SUMB2
  - SUMR * SUMR - SUMG * SUMG - SUMB * SUMB) / COUNT;
if (dev & gt; sorts
  The picture is!
ELSE.
  No pictures!

However, this method has limitations. For example, such a simple check is not able to distinguish a small object in the corner (which is definitely in the picture) from a weak gradient through the entire picture (which is simply background).

If it is important for you not to work in pictures with gradients – you can use alternative ways. For example, you can nullly apply the Laplace operator to the picture (find the second spatial derivative), which is equivalent to a convolution of the image with the kernel like this:

\ begin {pmatrix} 1 & amp; -2 & amp; 1 \2 & amp; 4 & amp; -2 \ 1 & amp; -2 & amp; 1 \ end {pmatrix}

or you can use the first derivative – the Hamilton operator (more precisely, its numerical representation in the form of the Bobe operator) with the kernels

\ begin {pmatrix} -1 & amp; -2 & amp; -1 \ 0 & amp; 0 & amp; 0 \ 1 & amp; 2 & amp; 1 \ end {pmatrix} \ begin {pmatrix} -1 & amp; 0 & amp; 1 \ -2 & amp; 0 & amp; 2 \ -1 & amp; 0 & amp; 1 \ end {pmatrix}

You can also use any other boundaries method.


If suddenly it turns out that you get a picture from the camera – do not forget to make a correction to lighting. For example, you can share the standard deviation to the average value, and the threshold is installed for this relationship.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions