def whiteninggray(images,dc=True): """ This function automatically whites gray images (Image only, data range 0-255, precalculated filter). Images is a 3d-ndarray with dtype=float32, with dimension ordering: Number_Of_Data, YSize, XSize Return 3d-ndarray with dtype=float32, with dimension ordering: Number_Of_Data, YSize-6, XSize-6 """ global _WHITE_COE _l.ASSERTTYPE(images) if len(images.shape)!=3: raise Exception, "3d-ndarray needed." #Remove DC Coefficient if dc: if images.shape[0]==1: tmp=images else: tmp=_l.empty(images.shape) _l.removeDC(images.reshape(images.shape[0],images.shape[1]*images.shape[2]),tmp.reshape(images.shape[0],images.shape[1]*images.shape[2])) else: tmp=images #Convolution to get result out=_l.empty((images.shape[0],1,images.shape[1]-6,images.shape[2]-6)) _l.convolution4D(tmp.reshape(images.shape[0],images.shape[1],images.shape[2],1),_WHITE_COE,out) return out.reshape(images.shape[0],images.shape[1]-6,images.shape[2]-6)
def rgb2cielab(image): """ This function translates a RGB image (0~255 for each pixel) to a CIEL*A*B* image (0~100 for L*, -127~127 for A*, -127~127 for B*) Image is a 3d-ndarray with dtype=uint8, with dimension ordering: YSize, XSize, [R,G,B] Returns 3d-ndarray with dtype=float32, with dimension ordering: YSize, XSize, [L,A,B] """ global _xyz,_lfunc,_ffunc ishape=image.shape dmat=numpy.array(image.reshape((-1,3)),'f') dmat/=255 dmat=dmat.dot(_xyz) x=numpy.array(dmat[:,0],'f') y=numpy.array(dmat[:,1],'f') z=numpy.array(dmat[:,2],'f') x/=0.950456 z/=1.088754 l=_l.empty((dmat.shape[0],)) _l.transform(_lfunc,l,y) fx=_l.empty((dmat.shape[0],)) fy=_l.empty((dmat.shape[0],)) fz=_l.empty((dmat.shape[0],)) _l.transform(_ffunc,fx,x) _l.transform(_ffunc,fy,y) _l.transform(_ffunc,fz,z) a=500*(fx-fy) b=500*(fy-fz) dmat[:,0]=l dmat[:,1]=a dmat[:,2]=b return dmat.reshape(ishape)
def whiteningcielab(image): """ This function does whitening on a CIEL*A*B* colorspace color image. It uses predefined filter to do convolution on image. Image is a 3d-ndarray with dtype=float32, with dimension ordering: YSize, XSize, [L,A,B] Returns 3d-ndarray with dtype=float32, with dimension ordering: YSize-2, XSize-2, [w_L,w_A,w_B] """ global _WHITE_CIELAB_COE _l.ASSERTTYPE(image) out=_l.empty((1,3,image.shape[0]-2,image.shape[1]-2)) ireshape=image.reshape((1,image.shape[0],image.shape[1],image.shape[2])) _l.convolution4D(ireshape,_WHITE_CIELAB_COE,out) return numpy.array(out.reshape((3,image.shape[0]-2,image.shape[1]-2)).transpose((1,2,0)),'f')