[Image-SIG] Re: Histogram
Fredrik Lundh
fredrik at pythonware.com
Wed Apr 21 09:36:40 EDT 2004
Manik Bali wrote:
> I have a tiff image file of 16353 rows and 40320 which are uniformly
> gridded.
> I want to count the pixels of each type in a gaussian rectangle with
> pixels which lie on the border be counted as the fraction that is inside
> the Gaussian grid.
> Is there a builtin function to do this in pil. Is the histogram overloaded
> to count fraction of grid bos inside a given rectangle and so on....
do you want to generate histograms for parts of the image, and
treat edge pixels as "fractions"? that is, if an edge pixel covers 50%
of an image pixel with the value 86, treat it as if it had value 43?
one way to do this is to use "crop" to cut out a rectangle, use
ImageChops.multiply() to multiply the rectangle with a
precalculated mask (where 1.0 is mapped to 255), and then
calculate a histogram of the result:
width, height = size of region
mask = Image.new("L", (width, height))
... initialize mask ...
for x, y in list of positions:
region = im.crop((x, y, x+width, y+height))
region = ImageChops.multiply(region, mask)
histogram = region.histogram()
...
hope this helps!
</F>
More information about the Image-SIG
mailing list