[iastat] [Up] [ialabel] Measurements

iacolorhist
Color-image histogram.

Synopsis

hc = iacolorhist( f, mask = None )

Implemented in Python.

Input

f Image. Gray-scale (uint8 or uint16) or binary image (logical).
mask Image. Binary image (logical).

Default: None

Output

hc Image. Gray-scale (uint8 or uint16) or binary image (logical).

Description

Compute the histogram of a color image and return a graphical image suitable for visualization with the 3 marginal histograms: red-green at top-left, blue-green at top-right and red-blue at bottom-left. If the optional mask image is available, the histogram is computed only for those pixels under the mask.

Examples

>>> f = iaread('boat.ppm')

              
>>> iashow(f)
(3, 257, 256) Min= 0 Max= 218 Mean=94.533 Std=57.35
>>> hc = iacolorhist(f)

              
>>> iashow(hc)
(527, 527) Min= 0 Max= 598 Mean=175.379 Std=271.53
>>> iashow(Numeric.log(hc+1))
(527, 527) Min= 0.0 Max= 6.39526159812 Mean=2.023 Std=2.86
f hc
Numeric.log(hc+1)

Source Code

def iacolorhist(f, mask=None):
    from Numeric import asarray, nonzero, ravel, zeros, Int32, ones, transpose, put, NewAxis
    WFRAME=5
    f = asarray(f)
    if len(f.shape) == 1: f = f[NewAxis,:]
    if not f.typecode() == 'b':
      iaerror('error, can only process uint8 images')
      return
    if not f.shape[0] == 3:
      iaerror('error, can only process 3-band images')
      return
    r,g,b = 1.*f[0,:,:], 1.*f[1,:,:], 1.*f[2,:,:]
    n_zeros = 0
    if mask:
      n_zeros = f.shape[0]*f.shape[1]-len(nonzero(ravel(mask)))
      r,g,b = mask*r, mask*g, mask*b
    hrg = zeros((256,256), Int32); hbg=hrg+0; hrb=hrg+0
    img = 256*r + g; m1 = max(ravel(img))
    aux = iahistogram(img.astype(Int32)); aux[0] = aux[0] - n_zeros
    put(ravel(hrg), range(m1+1), aux)
    img = 256*b + g; m2 = max(ravel(img))
    aux = iahistogram(img.astype(Int32)); aux[0] = aux[0] - n_zeros
    put(ravel(hbg), range(m2+1), aux)
    img = 256*r + b; m3 = max(ravel(img))
    aux = iahistogram(img.astype(Int32)); aux[0] = aux[0] - n_zeros
    put(ravel(hrb), range(m3+1), aux)
    m=max(max(ravel(hrg)),max(ravel(hbg)),max(ravel(hrb)))
    hc=m*ones((3*WFRAME+2*256,3*WFRAME+2*256))
    hc[WFRAME:WFRAME+256,WFRAME:WFRAME+256] = transpose(hrg)
    hc[WFRAME:WFRAME+256,2*WFRAME+256:2*WFRAME+512] = transpose(hbg)
    hc[2*WFRAME+256:2*WFRAME+512,WFRAME:WFRAME+256] = transpose(hrb)
    return hc
    
[iastat] [Up] [ialabel] http://www.python.org