Problem by counting pixels in regions found by mh.label (regions)

Skip to first unread message

Eduardo Munizaga

Apr 8, 2016, 4:24:58 PM4/8/16
to pythonvision
I am using the algorithm attached and the intention is to measure the amount of pixels of the regions identified by the code "mahotas.label (image)".

The problem is that the greater the area of the background, the greater the number of pixels that detects, considering that all points have the same number of pixels in different images. This is due to the following line: "mh.gaussian_filter (image 4)"

However this should not happen because what I need is that the regions are exactly independent measurements of the area that has the background.

Attached the script and images of different sizes but with the same picture of 200 x 200 pixels as examples.

import mahotas as mh
import mahotas.demos
import numpy as np
from pylab import imshow, show

imagen = mahotas.imread('76x66.png')
imagen = imagen[:,:,0]
imagen = mh.gaussian_filter(imagen, 4)
imagen = (imagen> imagen.mean())

etiquetas, unidades = mahotas.label(imagen)
sizes = mahotas.labeled.labeled_size(etiquetas)

print sizes[0]


100x100.png             print sizes = 1012 pixeles
200x 200.png            print sizes = 1292 pixeles
76x66.png                 print sizes = 848 pixeles

How I can do that, regardless of the background area, the result will not have change?

Luis Pedro Coelho

Apr 10, 2016, 4:37:12 AM4/10/16
Hi Eduardo,

The issue is that when you are using the mean as the threshold. However,
when you increase the size of the background, you also increase change
the value of the mean. Thus, your thresholds are different. Because you
have smoothed the image, different thresholds result in different areas.


Luis Pedro Coelho | EMBL |
My blog:
> <>
> <>
> <>
> --
> You received this message because you are subscribed to the Google Groups
> "pythonvision" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
> For more options, visit

Eduardo Munizaga

Apr 10, 2016, 11:12:31 AM4/10/16
Hi Luis!
Surely there is some proportion related treaty between the point and the bottom of the image. Any idea to solve it?
Reply all
Reply to author
0 new messages