3
$\begingroup$

I have black-and white picture like this enter image description here Can anyone tell me is it possible to calculate surface that black color occupies in this picture, so I know what's the percentage that black color has in whole picture.

$\endgroup$
  • $\begingroup$ What have you tried? $\endgroup$ – C. E. Dec 8 '18 at 16:50
  • $\begingroup$ Maybe ImageMeasurements[] and if not then ImageData[] $\endgroup$ – Michael E2 Dec 8 '18 at 17:09
  • 1
    $\begingroup$ Nitpick: you don't seek a surface (like a hyperboloid of one sheet), but an area (like 1.2 square millimetres). $\endgroup$ – Andreas Rejbrand Dec 8 '18 at 23:41
7
$\begingroup$

After reading Michael E2's answer, I realized that one can simply do

1 - Mean[img]

0.106198

There are several other solutions as well. There is a function called ImageLevels that counts the channels:

img = Import["https://i.stack.imgur.com/PdMDk.png"];
levels = ImageLevels[img]

{{0, 521982}, {1, 4393218}}

levels[[1, 2]]/(levels[[1, 2]] + levels[[2, 2]]) // N

0.106198

One could also use

neg = ColorNegate[img];
Total[neg, 2]/(Total[img, 2] + Total[neg, 2])

0.106198

or

{w, h} = ImageDimensions[img];
1 - Total[img, 2]/(w h)

0.106198

One could also explicitly get the matrix of image pixels:

pixels = Flatten@ImageData[img];
1 - Total[pixels]/Length[pixels] // N

0.106198

$\endgroup$
5
$\begingroup$

Just use ImageHistogram with two levels:

ImageHistogram[myImage,2, FrameTicks->True]

enter image description here

or

Dimensions[SplitBy[ImageData[myImage], First]][[2;;3]]

(*

{886, 1342}

*)

$\endgroup$
  • $\begingroup$ So the percentage of black color is (886/1342)*100% $\endgroup$ – Cro Simpson2.0 Dec 8 '18 at 17:19
5
$\begingroup$

For a binary image:

img = Import["https://i.stack.imgur.com/PdMDk.png"];
1 - ImageMeasurements[img, "MeanIntensity"]
(*  0.106198  *)
$\endgroup$
  • 1
    $\begingroup$ Thanks to this I realized that 1 - Mean[img] works... $\endgroup$ – C. E. Dec 9 '18 at 13:09
  • $\begingroup$ @C.E. Cool. I didn't know that. $\endgroup$ – Michael E2 Dec 9 '18 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.