# Object detection in image

I have the following image, with a "large" object and 3 visible bright pixels:

What I do not understand:

Why do I count in the this image more pixels with brightness $\ge0$ (= 191688) than pixels are available (326$\cdot$294 = 95844). Also is not clear to me why so many pixels have brightness = 1.

image = ColorConvert[image, "Grayscale"]

dim = ImageDimensions[image]
{326, 294}

dim[[1]]*dim[[2]]
95844

Count[Flatten[ImageData[image]], a_ /; a >= 0]
191688

Count[Flatten[ImageData[image]], a_ /; a == 1]
95843


Accordingly, the histogram of the image is surprising:

Histogram[Flatten@ImageData@image, {0, 1.01, 0.01}, ScalingFunctions -> "Log"]


As next I wanted to measure the mean brightness center of the large object. Here I do not understand why the resultingImage includes the bright pixels visible in the original image and also has pixels with brightness $\ge0$, although the binarized image binImage containes only pixels with brightness of = 0 and = 1.

What am I doing wrong?

binImage = Binarize[image, 0.4]


Histogram[Flatten@ImageData@binImage, {0, 1.01, 0.01}, ScalingFunctions -> "Log"]


resultingImage = ImageMultiply[image, binImage]


Histogram[Flatten@ImageData@resultingImage, {0, 1.01, 0.01}, ScalingFunctions -> "Log"]


The following code measured the center of the large object and the three bright pixels:

meanValue = ComponentMeasurements[resultingImage, "Centroid"]
{1 -> {170.5, 231.5}, 2 -> {125.372, 172.731}, 3 -> {94.5, 70.5}, 4 -> {175.5, 70.5}}

• Your image seems to have 2 channels, which is why Length@Flatten[ImageData[image]] is twice the value of dim[[1]]*dim[[2]] Mar 2, 2016 at 10:36
• How could I solve that and why do have half of the pixles in image brightness=1?
– mrz
Mar 2, 2016 at 10:44
• the second channel seems to be just flat white, so I say throw it out, see the answer below Mar 2, 2016 at 10:48

For some reason those 3 bright pixels are located in the second channel of your image, while the bright spot is in the first channel,

image = Import["http://i.stack.imgur.com/A4Gu8.png"];
{Image[ImageData[image][[All, All, 1]]],
Image[ImageData[image][[All, All, 2]]]}


So you could just throw out that second channel, which seems to have a flat brightness,

image = Image[ImageData[image][[All, All, 1]]]
ImageMultiply[image, Binarize[blackimage, 0.4]]


• Thank you ... now I have looked into the original 4K image. It has only one channel. But when I cropped the image with xnview and saved it this strange 2 channel image was produced. You solved my problem.
– mrz
Mar 2, 2016 at 11:11
• @mrz glad I could help Mar 2, 2016 at 11:11
• You mean ColorSeparate?
– yode
Mar 5, 2016 at 8:43