[Scipy-svn] r2743 - trunk/Lib/sandbox/image

scipy-svn@scip... scipy-svn@scip...
Thu Feb 22 02:46:23 CST 2007


Author: rkern
Date: 2007-02-22 02:46:08 -0600 (Thu, 22 Feb 2007)
New Revision: 2743

Added:
   trunk/Lib/sandbox/image/setup.py
Removed:
   trunk/Lib/sandbox/image/setup_image.py
Modified:
   trunk/Lib/sandbox/image/__init__.py
   trunk/Lib/sandbox/image/color.py
   trunk/Lib/sandbox/image/transforms.py
Log:
Update the image package in the sandbox so it builds and runs. Also added CIE L*u*v* colorspace transformation functions.

Modified: trunk/Lib/sandbox/image/__init__.py
===================================================================
--- trunk/Lib/sandbox/image/__init__.py	2007-02-22 01:58:34 UTC (rev 2742)
+++ trunk/Lib/sandbox/image/__init__.py	2007-02-22 08:46:08 UTC (rev 2743)
@@ -2,7 +2,5 @@
 # image - Image Processing Tools
 #
 
-from info_image import __doc__
-
 from color import *
 from transforms import *

Modified: trunk/Lib/sandbox/image/color.py
===================================================================
--- trunk/Lib/sandbox/image/color.py	2007-02-22 01:58:34 UTC (rev 2742)
+++ trunk/Lib/sandbox/image/color.py	2007-02-22 08:46:08 UTC (rev 2743)
@@ -1,6 +1,6 @@
 
-import numpy as sb
-import scipy
+import numpy as np
+from scipy import linalg
 import os
 
 # Various utilities and data for color processing
@@ -25,7 +25,7 @@
                    [0.177, 0.813, 0.011],
                    [0.000, 0.010, 0.990]]
 
-rgbcie_from_xyz = scipy.linalg.inv(xyz_from_rgbcie)
+rgbcie_from_xyz = linalg.inv(xyz_from_rgbcie)
 
 rgbntsc_from_xyz = [[1.910, -0.533, -0.288],
                     [-0.985, 2.000, -0.028],
@@ -45,7 +45,7 @@
                  [0.212671, 0.715160, 0.072169],
                  [0.019334, 0.119193, 0.950227]]
 
-rgb_from_xyz = scipy.linalg.inv(xyz_from_rgb)
+rgb_from_xyz = linalg.inv(xyz_from_rgb)
 
 # From http://www.mir.com/DMG/ycbcr.html
 
@@ -53,7 +53,7 @@
                    [-0.168736, -0.331264, 0.5],
                    [0.5, -0.418688, -0.081312]]
 
-rgbp_from_ycbcr = scipy.linalg.inv(ycbcr_from_rgbp)
+rgbp_from_ycbcr = linalg.inv(ycbcr_from_rgbp)
 
 
 # LMS color space spectral matching curves provide the
@@ -94,7 +94,7 @@
 for name in ['ciexyz31_1.txt','ciexyz64_1.txt','ciexyzjv.txt',
              'sbrgb2.txt','linss2_10e_1.txt']:
     k = k + 1
-    name = os.path.join(os.path.split(__file__)[0],'colordata',name)
+    name = os.path.join(os.path.dirname(__file__),name)
     afile = open(name)
     lines = afile.readlines()
     afile.close()
@@ -152,15 +152,15 @@
 
 def tri2chr(tri,axis=None):
     """Convert tristimulus values to chromoticity values"""
-    tri = sb.asarray(tri)
+    tri = np.asarray(tri)
     n = len(tri.shape)
     if axis is None:
         axis = coloraxis(tri.shape)
     slices = []
     for k in range(n):
         slices.append(slice(None))
-    slices[axis] = sb.newaxis
-    norm = sb.sum(tri,axis=axis)[slices]
+    slices[axis] = np.newaxis
+    norm = np.sum(tri,axis=axis)[slices]
     slices[axis] = slice(None,2)
     out = tri[slices]/norm
     return out
@@ -174,17 +174,17 @@
     raise ValueError, "No Color axis found."
 
 def convert(matrix,TTT,axis=None):
-    TTT = sb.asarray(TTT)
+    TTT = np.asarray(TTT)
     if axis is None:
         axis = coloraxis(TTT.shape)
     if (axis != 0):
-        TTT = sb.swapaxes(TTT,0,axis)
+        TTT = np.swapaxes(TTT,0,axis)
     oldshape = TTT.shape
-    TTT = sb.reshape(TTT,(3,-1))
-    OUT = sb.dot(matrix, TTT)
+    TTT = np.reshape(TTT,(3,-1))
+    OUT = np.dot(matrix, TTT)
     OUT.shape = oldshape
     if (axis != 0):
-        OUT = sb.swapaxes(OUT,axis,0)
+        OUT = np.swapaxes(OUT,axis,0)
     return OUT
 
 def xyz2rgbcie(xyz,axis=None):
@@ -216,12 +216,12 @@
     return x, y, z, axis
 
 def join_colors(c1,c2,c3,axis):
-    c1,c2,c3 = sb.asarray(c1),sb.asarray(c2),sb.asarray(c3)
+    c1,c2,c3 = np.asarray(c1),np.asarray(c2),np.asarray(c3)
     newshape = c1.shape[:axis] + (1,) + c1.shape[axis:]
     c1.shape = newshape
     c2.shape = newshape
     c3.shape = newshape
-    return sb.concatenate((c1,c2,c3),axis=axis)
+    return np.concatenate((c1,c2,c3),axis=axis)
 
 def xyz2lab(xyz, axis=None, wp=whitepoints['D65'][-1], doclip=1):
     x,y,z,axis = separate_colors(xyz,axis)
@@ -229,21 +229,21 @@
     def f(t):
         eps = 216/24389.
         kap = 24389/27.
-        return sb.where(t > eps,
-                        sb.power(t, 1.0/3),
+        return np.where(t > eps,
+                        np.power(t, 1.0/3),
                         (kap*t + 16.0)/116)
     fx,fy,fz = f(xn), f(yn), f(zn)
     L = 116*fy - 16
     a = 500*(fx - fy)
     b = 200*(fy - fz)
     if doclip:
-        L = sb.clip(L, 0.0, 100.0)
-        a = sb.clip(a, -500.0, 500.0)
-        b = sb.clip(b, -200.0, 200.0)
+        L = np.clip(L, 0.0, 100.0)
+        a = np.clip(a, -500.0, 500.0)
+        b = np.clip(b, -200.0, 200.0)
     return join_colors(L,a,b,axis)
 
 def lab2xyz(lab, axis=None, wp=whitepoints['D65'][-1]):
-    lab = sb.asarray(lab)
+    lab = np.asarray(lab)
     L,a,b,axis = separate_colors(lab,axis)
     fy = (L+16)/116.0
     fz = fy - b / 200.
@@ -251,8 +251,8 @@
     def finv(y):
         eps3 = (216/24389.)**3
         kap = 24389/27.
-        return sb.where(y > eps3,
-                        sb.power(y,3),
+        return np.where(y > eps3,
+                        np.power(y,3),
                         (116*y-16)/kap)
     xr, yr, zr = finv(fx), finv(fy), finv(fz)
     return join_colors(xr*wp[0],yr*wp[1],zr*wp[2],axis)
@@ -263,6 +263,51 @@
 def lab2rgb(lab):
     return xyz2rgb(lab2xyz(lab))
 
+def _uv(x, y, z):
+    """ The u, v formulae for CIE 1976 L*u*v* computations.
+    """
+    denominator = (x + 15*y + 3*z)
+    zeros = (denominator == 0.0)
+    denominator[zeros] = 1.0
+    u_numerator = 4 * x
+    u_numerator[zeros] = 4.0
+    v_numerator = 9 * y
+    v_numerator[zeros] = 9.0 / 15.0
+
+    return u_numerator/denominator, v_numerator/denominator
+
+def xyz2luv(xyz, axis=None, wp=whitepoints['D65'][-1]):
+    x, y, z, axis = separate_colors(xyz, axis)
+    xn, yn, zn = x/wp[0], y/wp[1], z/wp[2]
+    Ls = 116.0 * np.power(yn, 1./3) - 16.0
+    small_mask = (y <= 0.008856*wp[1])
+    Ls[small_mask] = 903.0 * y[small_mask] / wp[1]
+    unp, vnp = _uv(*wp)
+    up, vp = _uv(x, y, z)
+    us = 13 * Ls * (up - unp)
+    vs = 13 * Ls * (vp - vnp)
+
+    return join_colors(Ls, us, vs, axis)
+
+def luv2xyz(luv, axis=None, wp=whitepoints['D65'][-1]):
+    Ls, us, vs, axis = separate_colors(luv, axis)
+    unp, vnp = _uv(*wp)
+    small_mask = (Ls <= 903.3 * 0.008856)
+    y = wp[1] * ((Ls + 16.0) / 116.0) ** 3
+    y[small_mask] = Ls * wp[1] / 903.0
+    up = us / (13*Ls) + us
+    vp = vs / (13*Ls) + vs
+    x = 9.0 * y * up / (4.0 * vp)
+    z = -x / 3.0 - 5.0 * y + 3.0 * y/vp
+
+    return join_colors(x, y, z, axis)
+
+def rgb2luv(rgb):
+    return xyz2luv(rgb2xyz(rgb))
+
+def luv2rgb(luv):
+    return xyz2rgb(luv2xyz(luv))
+
 #  RGB values that will be displayed on a screen are always
 #  R'G'B' values.  To get the XYZ value of the color that will be
 #  displayed you need a calibrated monitor with a profile
@@ -291,10 +336,10 @@
 #    rgbp = rgb**(1.0/gamma)
 
 def rgb2rgbp(rgb,gamma=None):
-    rgb = sb.asarray(rgb)
+    rgb = np.asarray(rgb)
     if gamma is None:
         eps = 0.0031308
-        return sb.where(rgb < eps, 12.92*rgb,
+        return np.where(rgb < eps, 12.92*rgb,
                      1.055*rgb**(1.0/2.4) - 0.055)
     else:
         return rgb**(1.0/gamma)
@@ -306,11 +351,11 @@
 #     rgb = rgbp**gamma
 #
 def rgbp2rgb(rgbp,gamma=None):
-    rgbp = sb.asarray(rgbp)
+    rgbp = np.asarray(rgbp)
     if gamma is None:
         eps = 0.04045
-        return sb.where(rgbp <= eps, rgbp / 12.92,
-                     sb.power((rgbp + 0.055)/1.055,2.4))
+        return np.where(rgbp <= eps, rgbp / 12.92,
+                     np.power((rgbp + 0.055)/1.055,2.4))
     else:
         return rgbp**gamma
 
@@ -340,9 +385,9 @@
 
 def ycbcr_8bit(ycbcr,axis=None):
     y,cb,cr,axis = separate_colors(ycbcr,axis)
-    Y = sb.asarray((y*219 + 16),sb.UInt8)
-    Cb = sb.asarray((cb*224 + 128),sb.UInt8)
-    Cr = sb.asarray((cr*224 + 128),sb.UInt8)
+    Y = np.asarray((y*219 + 16),np.uint8)
+    Cb = np.asarray((cb*224 + 128),np.uint8)
+    Cr = np.asarray((cr*224 + 128),np.uint8)
     return join_colors(Y,Cb,Cr,axis)
 
 def ycbcr_norm(YCbCr,axis=None):

Copied: trunk/Lib/sandbox/image/setup.py (from rev 2742, trunk/Lib/sandbox/image/setup_image.py)
===================================================================
--- trunk/Lib/sandbox/image/setup_image.py	2007-02-22 01:58:34 UTC (rev 2742)
+++ trunk/Lib/sandbox/image/setup.py	2007-02-22 08:46:08 UTC (rev 2743)
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+
+def configuration(parent_package='', top_path=None):
+    from numpy.distutils.misc_util import Configuration
+    config = Configuration('image', parent_package, top_path)
+    config.add_data_files("""ciexyz31_1.txt ciexyz64_1.txt ciexyzjv.txt
+        linss2_10e_1.txt sbrgb2.txt""".split())
+
+    return config
+
+if __name__ == '__main__':
+    from numpy.distutils.core import setup
+    setup(**configuration())

Deleted: trunk/Lib/sandbox/image/setup_image.py
===================================================================
--- trunk/Lib/sandbox/image/setup_image.py	2007-02-22 01:58:34 UTC (rev 2742)
+++ trunk/Lib/sandbox/image/setup_image.py	2007-02-22 08:46:08 UTC (rev 2743)
@@ -1,25 +0,0 @@
-#!/usr/bin/env python
-
-from glob import glob
-import os
-
-def configuration(parent_package='', parent_path=None):
-    from numpy.distutils.system_info import get_info, dict_append
-    from numpy.distutils.misc_util import default_config_dict, \
-         dot_join, get_path
-
-    package = 'image'
-    config = default_config_dict(package,parent_package)
-
-    local_path = get_path(__name__, parent_path)
-    image_path = os.path.join(parent_package,'image')
-
-    color_files = glob(os.path.join(local_path, '*.txt'))
-    data_path = os.path.join(image_path, 'colordata')
-    config['data_files'].extend([(data_path, color_files)])
-
-    return config
-
-if __name__ == '__main__':
-    from numpy.distutils.core import setup
-    setup(**configuration())

Modified: trunk/Lib/sandbox/image/transforms.py
===================================================================
--- trunk/Lib/sandbox/image/transforms.py	2007-02-22 01:58:34 UTC (rev 2742)
+++ trunk/Lib/sandbox/image/transforms.py	2007-02-22 08:46:08 UTC (rev 2743)
@@ -1,6 +1,13 @@
-import numpy as sb
-pi = sb.pi
+import numpy as np
+from numpy import conj, dot, ogrid, pi, r_, sin, transpose, zeros
+from scipy import linalg
 
+
+__all__ = """dct idct dct2 idct2 dctn idctn dct2raw idct2raw
+dst idst dst2 idst2 dstn idstn 
+digitrevorder bitrevorder wht
+""".split()
+
 # fast discrete cosine transforms of real sequences (using the fft)
 #  These implement the DCT-II and inverse DCT-II (DCT-III)
 #  described at http://en.wikipedia.org/wiki/Discrete_cosine_transform
@@ -28,7 +35,7 @@
     else:
         newshape = list(x.shape)
         newshape[axis] = 2*N
-        xtilde = sb.empty(newshape,sb.float64)
+        xtilde = np.empty(newshape,np.float64)
         slices[0][axis] = slice(None,N)
         slices[2][axis] = slice(N,None)
         slices[3][axis] = slice(None,None,-1)
@@ -37,9 +44,9 @@
         slices[k] = tuple(slices[k])
     xtilde[slices[0]] = x[slices[1]]
     xtilde[slices[2]] = x[slices[3]]
-    Xt = sb.fft(xtilde,axis=axis)
-    pk = sb.exp(-1j*pi*sb.arange(N)/(2*N))
-    newshape = sb.ones(n)
+    Xt = np.fft(xtilde,axis=axis)
+    pk = np.exp(-1j*pi*np.arange(N)/(2*N))
+    newshape = np.ones(n)
     newshape[axis] = N
     pk.shape = newshape
 
@@ -47,7 +54,7 @@
         pk /= 2;
         Xt = Xt[slices[0]]
 
-    return sb.real(Xt*pk)
+    return np.real(Xt*pk)
 
 
 def idct(v,axis=-1):
@@ -59,13 +66,13 @@
         slices[k] = []
         for j in range(n):
             slices[k].append(slice(None))
-    k = sb.arange(N)
+    k = np.arange(N)
     if even:
-        ak = sb.r_[1.0,[2]*(N-1)]*sb.exp(1j*pi*k/(2*N))
-        newshape = sb.ones(n)
+        ak = np.r_[1.0,[2]*(N-1)]*np.exp(1j*pi*k/(2*N))
+        newshape = np.ones(n)
         newshape[axis] = N
         ak.shape = newshape
-        xhat = sb.real(sb.ifft(v*ak,axis=axis))
+        xhat = np.real(np.ifft(v*ak,axis=axis))
         x = 0.0*v
         slices[0][axis] = slice(None,None,2)
         slices[1][axis] = slice(None,N/2)
@@ -77,13 +84,13 @@
         x[slices[2]] = xhat[slices[3]]
         return x
     else:
-        ak = 2*sb.exp(1j*pi*k/(2*N))
-        newshape = sb.ones(n)
+        ak = 2*np.exp(1j*pi*k/(2*N))
+        newshape = np.ones(n)
         newshape[axis] = N
         ak.shape = newshape
         newshape = list(v.shape)
         newshape[axis] = 2*N
-        Y = zeros(newshape,sb.Complex)
+        Y = zeros(newshape,np.complex128)
         #Y[:N] = ak*v
         #Y[(N+1):] = conj(Y[N:0:-1])
         slices[0][axis] = slice(None,N)
@@ -92,7 +99,7 @@
         slices[3][axis] = slice((N-1),0,-1)
         Y[slices[0]] = ak*v
         Y[slices[2]] = conj(Y[slices[3]])
-        x = sb.real(sb.ifft(Y,axis=axis))[slices[0]]
+        x = np.real(np.ifft(Y,axis=axis))[slices[0]]
         return x
 
 def dct2(x,axes=(-1,-2)):
@@ -103,7 +110,7 @@
 
 def dctn(x,axes=None):
     if axes is None:
-        axes = sb.arange(len(x.shape))
+        axes = np.arange(len(x.shape))
     res = x
     for k in axes:
         res = dct(res,axis=k)
@@ -111,7 +118,7 @@
 
 def idctn(v,axes=None):
     if axes is None:
-        axes = sb.arange(len(v.shape))
+        axes = np.arange(len(v.shape))
     res = v
     for k in axes:
         res = idct(res,axis=k)
@@ -120,7 +127,7 @@
 
 def makeC(N):
     n,l = ogrid[:N,:N]
-    C = sb.cos(pi*(2*n+1)*l/(2*N))
+    C = np.cos(pi*(2*n+1)*l/(2*N))
     return C
 
 def dct2raw(x):
@@ -159,7 +166,7 @@
             slices[k].append(slice(None))
     newshape = list(x.shape)
     newshape[axis] = 2*(N+1)
-    xtilde = sb.zeros(newshape,sb.float64)
+    xtilde = np.zeros(newshape,np.float64)
     slices[0][axis] = slice(1,N+1)
     slices[1][axis] = slice(N+2,None)
     slices[2][axis] = slice(None,None,-1)
@@ -167,8 +174,8 @@
         slices[k] = tuple(slices[k])
     xtilde[slices[0]] = x
     xtilde[slices[1]] = -x[slices[2]]
-    Xt = sb.fft(xtilde,axis=axis)
-    return (-sb.imag(Xt)/2)[slices[0]]
+    Xt = np.fft(xtilde,axis=axis)
+    return (-np.imag(Xt)/2)[slices[0]]
 
 def idst(v,axis=-1):
     n = len(v.shape)
@@ -180,7 +187,7 @@
             slices[k].append(slice(None))
     newshape = list(v.shape)
     newshape[axis] = 2*(N+1)
-    Xt = sb.zeros(newshape,sb.Complex)
+    Xt = np.zeros(newshape,np.complex128)
     slices[0][axis] = slice(1,N+1)
     slices[1][axis] = slice(N+2,None)
     slices[2][axis] = slice(None,None,-1)
@@ -189,7 +196,7 @@
         slices[k] = tuple(slices[k])
     Xt[slices[0]] = -val
     Xt[slices[1]] = val[slices[2]]
-    xhat = sb.real(sb.ifft(Xt,axis=axis))
+    xhat = np.real(np.ifft(Xt,axis=axis))
     return xhat[slices[0]]
 
 def dst2(x,axes=(-1,-2)):
@@ -200,7 +207,7 @@
 
 def dstn(x,axes=None):
     if axes is None:
-        axes = sb.arange(len(x.shape))
+        axes = np.arange(len(x.shape))
     res = x
     for k in axes:
         res = dst(res,axis=k)
@@ -208,14 +215,14 @@
 
 def idstn(v,axes=None):
     if axes is None:
-        axes = sb.arange(len(v.shape))
+        axes = np.arange(len(v.shape))
     res = v
     for k in axes:
         res = idst(res,axis=k)
     return res
 
 def digitrevorder(x,base):
-    x = sb.asarray(x)
+    x = np.asarray(x)
     rem = N = len(x)
     L = 0
     while 1:
@@ -227,7 +234,7 @@
         rem = intd
         L += 1
     vec = r_[[base**n for n in range(L)]]
-    newx = x[sb.newaxis,:]*vec[:,sb.newaxis]
+    newx = x[np.newaxis,:]*vec[:,np.newaxis]
     # compute digits
     for k in range(L-1,-1,-1):
         newx[k] = x // vec[k]
@@ -260,8 +267,8 @@
     Digital Signal Processing" Spring Verlag, New York 1975. page-111.
     """
     N = len(data)
-    L = sb.log2(N);
-    if ((L-sb.floor(L)) > 0.0):
+    L = np.log2(N);
+    if ((L-np.floor(L)) > 0.0):
         raise ValueError, "Length must be power of 2"
     x=bitrevorder(data);
 



More information about the Scipy-svn mailing list