Search code examples
visual-c++opencvpixel

how to calculate pixel density of a sampled binary image


This may be a very simple matter, but I have conflict with that. I have split a particular binary image (28 x 28) into (4 x 4) samples. Now I want to calculate the pixel densities of each sample (I use those density values as features in a OCR application). As I know density defines the number of pixels in a particular area, like 7 pixels per square inch. Is that the same in here? all of my samples have 4 pixels. Is there are relationship between Moment->m00 and pixel density? Can someone explain this? plz help


Solution

  • A "density" depicts how much of "a thing" corresponds to a "small fraction of space".

    In terms of images, that might be i.e. "amount of colour" that a "fragment of image" holds.

    For black and white or grayscale images that are held as pixel arrays that could simply mean an average pixel value.

    For example, if your image is black and white (that is, pixels have value either 0 or 1): If your sample is a 4x4 square, then its area is 16. In this area you can have from 0 to 16 pixels, what would be respectively 0.0 and 1.0. Here a 4 black pixels and 12 white could indicate a density of 4/16 = 0.25 (or 12/16 = 0.75, depending on which pixels you treat as "empty" (black or white?)).

    For example, if your image is grayscale (that is, pixels have values in range of 0..255 which describes how white they are): If your sample is a 4x4 square, then its area is 16. In this area you can have from 0 to 16 pixels, what would be respectively 0% and 100%. All but four pixels "empty", and those four have values 100,100,50,50 gives you density of (100+100+50+50)/255/16 = 0.073 . Mind that pixels have min=0 and max=255 values. If your pixels have different value ranges, adjust appropriately.

    In terms of OpenCV, I'd assume that moment->m00 is a "spacial image m=0,n=0 moment". So, you might want to review i.e. http://software.intel.com/sites/products/documentation/hpc/ipp/ippi/ippi_ch11/ch11_image_moments.html

    Looking at that document and the formulas, I think that you will find your density either in m00 or in m11 fields. I think that since m and n are 0, then m00 will be equal to 1(one), and the m11 will hold the average pixel value of the 2D image, but I've not tried/checked so I am not 100% sure.