def lab(filename,extra): I = Image.open(filename) L = ct.rgb_to_cielab_i_X(I,0) L = mfs.mfs(L,extra) #L = singularityCL.spec(L,extra) if(extra[4]): a = ct.rgb_to_cielab_i_X(I,1) b = ct.rgb_to_cielab_i_X(I,2) a = mfs.mfs(a,extra) b = mfs.mfs(b,extra) #a = singularityCL.spec(a,extra) #b = singularityCL.spec(b,extra) return np.hstack((L,a,b)) return np.array(L)
def inner_localMF(I, N, extra): if(N==0): return mfs.mfs(I,extra) w,h = I.size #print w, h # crop? return np.hstack(( inner_localMF(I.crop((0,0,w/2,h/2)), N-1,extra), \ inner_localMF(I.crop((0,h/2,w/2,h)), N-1,extra), \ inner_localMF(I.crop((w/2,0,w,h/2)), N-1,extra), \ inner_localMF(I.crop((w/2,h/2,w,h)), N-1,extra) ))
def laplacian(filename,extra): #Compute Gradient # :s, conv2d matlab function... #f1 = fspecial('gaussian',5,1); #f2 = [-1 -1 -1;-1 8 -1;-1 -1 -1]; #f = conv2(f1,f2); f = np.array([[-0.0029690, -0.0162752, -0.0382135, -0.0485507, -0.0382135, -0.0162752, -0.0029690],[-0.0162752, -0.0624946, -0.0897184, -0.0686955, -0.0897184, -0.0624946, -0.0162752], [-0.0382135, -0.0897184, 0.0448730, 0.2599999, 0.0448730, -0.0897184, -0.0382135], [-0.0485507, -0.0686955, 0.2599999, 0.6650041, 0.2599999, -0.0686955, -0.0485507], [-0.0382135, -0.0897184, 0.0448730, 0.2599999, 0.0448730, -0.0897184, -0.0382135], [-0.0162752, -0.0624946, -0.0897184, -0.0686955, -0.0897184, -0.0624946, -0.0162752], [-0.0029690, -0.0162752, -0.0382135, -0.0485507, -0.0382135, -0.0162752, -0.0029690]]) IM = Image.open(filename) IM = IM.convert("L") a = scipy.signal.convolve2d(IM, f,mode="full") Nx, Ny = a.shape a = a[3:Nx-3,3:Ny-3] a = np.floor((a<0).choose(a,0)) extra[3] = False return mfs.mfs(a,extra)
def main(filename,extra): #Compute Gradient #filename = '../images/nonbread/brodatz/D1.gif' IM = Image.open(filename) IM = IM.convert("L") Nx, Ny = IM.size IM = np.array(IM.getdata()).reshape(IM.size) fx = np.float32(0.5)*np.array([[-1, 0, 1],[0, 0, 0],[0, 0, 0]]) fy = fx.T fxy = np.float32(0.5)*np.array([[-1, 0, 0],[0, 0, 0],[0, 0, 1]]) fyx = np.float32(0.5)*np.array([[0, 0, -1],[0, 0, 0],[1, 0, 0]]) IMG = IM a = scipy.signal.convolve2d(IMG, fx,mode="full") Nx, Ny = a.shape a = a[0:Nx-2,1:Ny-1] b = scipy.signal.convolve2d(IMG, fy,mode="full") Nx, Ny = b.shape b = b[1:Nx-1,0:Ny-2] c = scipy.signal.convolve2d(IMG, fxy,mode="full") Nx, Ny = c.shape c = c[1:Nx-1,1:Ny-1] d = scipy.signal.convolve2d(IMG, fyx,mode="full") Nx, Ny = d.shape d = d[1:Nx-1,1:Ny-1] IMG = a**2 + b**2+c**2 +d**2 IMG = np.sqrt(IMG) IMG = np.floor(IMG) #WG = sum(double(IMG(:))); #WG = np.sum(IMG) #print WG extra[3] = False return mfs.mfs(IMG,extra)
def efd(filename,extra): cantFD = extra[1] a = Image.open(filename) Nx, Ny = a.size L = Nx*Ny gray = a.convert('L') # rgb 2 gray arr = np.array(gray.getdata()).astype(np.int32) alphaIm = np.zeros((Nx,Ny), dtype=np.float32 ) # Nx rows x Ny columns prg = cl.Program(ctx, """ __kernel void measure(__global float *alphaIm, __global int *img, const int Nx, const int Ny, const int size) { int i = get_global_id(0); int j = get_global_id(1); // make histogram of region int hist[256]; int t; for(t = 0; t < 256; t++) hist[t] = 0; int xi = max(i-size,0); int yi = max(j-size,0); int xf = min(i+size,Nx-1); int yf = min(j+size,Ny-1); int u , v; for(int u = xi; u <= xf; u++) for(int v = yi; v <= yf; v++) hist[img[u*Ny+v]]++; float res = 0; int s; float total = (yf-yi)*(xf-xi); // size of region for(s = 0; s <= 255; s++) { float v = hist[s]/total+0.0000001; // probability res += v*log2(v); } alphaIm[i*Ny+j] = -res; } """).build() img_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=arr) alphaIm_buf = cl.Buffer(ctx, mf.WRITE_ONLY, alphaIm.nbytes) sh = alphaIm.shape size = 8 # Window size prg.measure(queue, sh, None, alphaIm_buf, img_buf, np.int32(Nx), np.int32(Ny), np.int32(size)) cl.enqueue_read_buffer(queue,alphaIm_buf,alphaIm).wait() min_Im = np.min(alphaIm) max_Im = np.max(alphaIm) alphaIm = 255*(alphaIm-min_Im)/(max_Im - min_Im) extra[3] = False #import matplotlib #from matplotlib import pyplot as plt #print np.floor(alphaIm) #plt.imshow(np.floor(alphaIm), cmap=matplotlib.cm.gray) #plt.show() res = mfs.mfs(np.floor(alphaIm),extra) return res
def spec(filename, extra): #cuantas = extra[0] #OPEN_IMAGE = extra[1] #if(OPEN_IMAGE==True): a = Image.open(filename) gray = a.convert('L') # rgb 2 gray Nx, Ny = a.size #else: # np array # a = filename #Nx, Ny = a.shape # Nx, Ny = a.size L = Nx*Ny arr = np.array(gray.getdata()).astype(np.int32) alphaIm = np.zeros((Nx,Ny), dtype=np.double ) # Nx rows x Ny columns l = 4 # (maximum window size-1) / 2 temp = map(lambda i: 2*i+1, range(l)) temp = np.log(temp) measure = np.zeros(l*Ny).astype(np.int32) b = np.vstack((temp,np.ones((1,l)))).T AA=coo_matrix(np.kron(np.identity(Ny), b)) # which: which measure to take which = 4 prg = cl.Program(ctx, """ int maxx(__global int *img, int x1, int y1, int x2, int y2, const int Ny) { int i, j; int maxim = 0; for(i = x1; i < x2; i++) for(j = y1; j < y2; j++) if(img[i*Ny + j] > maxim) maxim = img[i*Ny + j]; return maxim; } int minn(__global int *img, int x1, int y1, int x2, int y2, const int Ny) { int i, j; int minim = 255; for(i = x1; i < x2; i++) for(j = y1; j < y2; j++) if(img[i*Ny + j] < minim) minim = img[i*Ny + j]; return minim; } int summ(__global int *img, int x1, int y1, int x2, int y2, const int Ny) { int i, j; int summ = 0; for(i = x1; i < x2; i++) for(j = y1; j < y2; j++) summ += img[i*Ny + j]; return summ; } int iso(__global int *img, int x1, int y1, int x2, int y2, const int Ny, const int x, const int y) { int i, j; int cant = 0; for(i = x1; i < x2; i++) for(j = y1; j < y2; j++) if(img[i*Ny + j] == img[x*Ny + y]) cant++; return cant; } int difAbsCentral(__global int *img, int x1, int y1, int x2, int y2, const int Ny, const int x, const int y) { int i, j; int maxim = 0; for(i = x1; i < x2; i++) for(j = y1; j < y2; j++) { int dif = abs(img[i*Ny + j]-img[x*Ny + y]); if(dif > maxim) maxim = dif; } return maxim; } __kernel void measure(__global int *dest, __global int *img, const int Nx, const int Ny, const int l, int i, const int d, const int which) { int j = get_global_id(0); int jim = (int)(j/l)+d; if(which == 0) dest[j] = maxx(img,max(i-((j%l)+1),0),max(jim-((j%l)+1),0), min(i+(j%l)+1,Nx-1),min(jim+(j%l)+1,Ny-1), Ny) + 1; if(which == 1) dest[j] = minn(img,max(i-((j%l)+1),0),max(jim-((j%l)+1),0), min(i+(j%l)+1,Nx-1),min(jim+(j%l)+1,Ny-1), Ny) + 1; if(which == 2) dest[j] = summ(img,max(i-((j%l)+1),0),max(jim-((j%l)+1),0), min(i+(j%l)+1,Nx-1),min(jim+(j%l)+1,Ny-1), Ny) + 1; if(which == 3) dest[j] = iso(img,max(i-((j%l)+1),0),max(jim-((j%l)+1),0), min(i+(j%l)+1,Nx-1),min(jim+(j%l)+1,Ny-1), Ny, i, j) + 1; if(which == 4) dest[j] = difAbsCentral(img,max(i-((j%l)+1),0),max(jim-((j%l)+1),0), min(i+(j%l)+1,Nx-1),min(jim+(j%l)+1,Ny-1), Ny, i, j) + 1; } """).build() d = measure.shape[0]/2 ms = measure[0:l*d] img_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=arr) dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, ms.nbytes) sh = ms.shape for i in range(Nx): prg.measure(queue, sh, None, dest_buf, img_buf, np.int32(Nx), np.int32(Ny), np.int32(l), np.int32(i), np.int32(0), np.int32(which)) cl.enqueue_read_buffer(queue, dest_buf, measure[0:l*d]).wait() prg.measure(queue, sh, None, dest_buf, img_buf, np.int32(Nx), np.int32(Ny), np.int32(l), np.int32(i), np.int32(d), np.int32(which)) cl.enqueue_read_buffer(queue, dest_buf, measure[l*d:]).wait() # Instead of doing polyfits, a sparse linear system is constructed and solved bb=np.log(measure) z = linsolve.lsqr(AA,bb)[0] z = z.reshape(2,Ny,order = 'F') alphaIm[i] = z[0] maxim = np.max(alphaIm) minim = np.min(alphaIm) alphaIm = np.floor(255*(alphaIm-minim)/(maxim-minim)) #import matplotlib #from matplotlib import pyplot as plt # Alpha image #plt.imshow(alphaIm, cmap=matplotlib.cm.gray) #plt.show() #return extra[3] = False return mfs.mfs(alphaIm,extra)
################ x = np.arange(FD) plt.ylabel(r'$f(\alpha)$',fontsize=12) plt.xlabel('FD',fontsize=12) filen = '../images/camera/sandwich/s5.tif' san = mfs.mfs(filen,[1,FD,3,True]) plt.plot(x, san, 'b*--', linewidth=2.0) plt.show() exit() sal = np.zeros((cant,FD)) san = np.zeros((cant,FD)) l = np.zeros((cant,FD)) b = np.zeros((cant,FD)) salC = np.zeros((cant,FD)) sanC = np.zeros((cant,FD)) lC = np.zeros((cant,FD)) bC = np.zeros((cant,FD)) for i in range(1,cant):