def plot_matches(im1, im2, matches): """ Displays two images side by side with matches highlighted Args: im1, im2: paths to the two input images matches: 2D numpy array of size 4xN containing a list of matches (a list of pairs of points, each pair being represented by x1, y1, x2, y2) Returns: path to the resulting image, to be displayed """ # load images img1 = piio.read(im1).astype(np.uint8) img2 = piio.read(im2).astype(np.uint8) # if images have more than 3 channels, keep only the first 3 if img1.shape[2] > 3: img1 = img1[:, :, 0:3] if img2.shape[2] > 3: img2 = img2[:, :, 0:3] # build the output image h1, w1 = img1.shape[:2] h2, w2 = img2.shape[:2] w = w1 + w2 h = max(h1, h2) out = np.zeros((h, w, 3), np.uint8) out[:h1, :w1] = img1 out[:h2, w1:w] = img2 # define colors, according to min/max intensity values out_min = min(np.nanmin(img1), np.nanmin(img2)) out_max = max(np.nanmax(img1), np.nanmax(img2)) green = [out_min, out_max, out_min] blue = [out_min, out_min, out_max] # plot the matches for i in range(len(matches)): x1 = matches[i, 0] y1 = matches[i, 1] x2 = matches[i, 2] + w1 y2 = matches[i, 3] # convert endpoints to int (nn interpolation) x1, y1, x2, y2 = np.round([x1, y1, x2, y2]) plot_line(out, x1, y1, x2, y2, blue) out[y1, x1] = green out[y2, x2] = green # save the output image, and return its path outfile = common.tmpfile('.png') piio.write(outfile, out) return outfile
def compute_height_map(P1, P2, H1, H2, f_disp, f_mask, fout_height, fout_proj_err): import piio import common P1 = common.matrix_read(P1,3,4) P2 = common.matrix_read(P2,3,4) H1 = common.matrix_read(H1,3,3) H2 = common.matrix_read(H2,3,3) disp = piio.read(f_disp)[:,:,0] mask = piio.read(f_mask)[:,:,0] height,proj_err = disp_to_h_projective(P1,P2,H1,H2,disp,mask) piio.write(fout_height ,height) piio.write(fout_proj_err,proj_err)
def register_heights(im1, im2): """ Affine registration of heights. Args: im1: first height map im2: second height map, to be registered on the first one Returns path to the registered second height map """ # remove high frequencies with a morphological zoom out im1_low_freq = common.image_zoom_out_morpho(im1, 4) im2_low_freq = common.image_zoom_out_morpho(im2, 4) # first read the images and store them as numpy 1D arrays, removing all the # nans and inf i1 = piio.read(im1_low_freq).ravel() #np.ravel() gives a 1D view i2 = piio.read(im2_low_freq).ravel() ind = np.logical_and(np.isfinite(i1), np.isfinite(i2)) h1 = i1[ind] h2 = i2[ind] # for debug print np.shape(i1) print np.shape(h1) # # 1st option: affine # # we search the (u, v) vector that minimizes the following sum (over # # all the pixels): # #\sum (im1[i] - (u*im2[i]+v))^2 # # it is a least squares minimization problem # A = np.vstack((h2, h2*0+1)).T # b = h1 # z = np.linalg.lstsq(A, b)[0] # u = z[0] # v = z[1] # # # apply the affine transform and return the modified im2 # out = common.tmpfile('.tif') # common.run('plambda %s "x %f * %f +" > %s' % (im2, u, v, out)) # 2nd option: translation only v = np.mean(h1 - h2) out = common.tmpfile('.tif') common.run('plambda %s "x %f +" -o %s' % (im2, v, out)) return out
def recompose(prefix, levels, suffix, wtype, cur=0): print prefix+str(cur)+suffix image = piio.read(prefix+str(cur)+suffix) print image.shape if levels==1: # if last level return image LL = 2 * recompose(prefix,levels-1,suffix, wtype, cur+1) ## 2 compensates the factor used in decompose coeffs2 = pywt.dwt2(image,wtype,axes=(0,1)) if coeffs2[0].shape != LL.shape: print "ERROR: Are you sure that \'%s\' is the right wavelet? Sizes don't match!"%wtype exit() ncoeffs2 = (LL, coeffs2[1]) ret = pywt.idwt2(ncoeffs2,wtype, axes=(0,1)) # adjust size of the upsampled image ret = ret[:image.shape[0],:image.shape[1],:] print ret.shape return ret
def test_one(self, height, width, input, output): H, W = height, width inp_chns = 3 if self.args.model == 'color' else 1 self.batch_size = 1 if self.args.model == 'color' else 3 inputs = tf.placeholder(shape=[self.batch_size, H, W, inp_chns], dtype=tf.float32) outputs = self.generator(inputs, reuse=False) sess = tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions( allow_growth=True))) self.saver = tf.train.Saver() self.load(sess, self.train_dir, step=523000) blur = iio.read(input) h, w, c = blur.shape # make sure the width is larger than the height rot = False if h > w: blur = np.transpose(blur, [1, 0, 2]) rot = True h = int(blur.shape[0]) w = int(blur.shape[1]) resize = False if h > H or w > W: scale = min(1.0 * H / h, 1.0 * W / w) new_h = int(h * scale) new_w = int(w * scale) blur = scipy.misc.imresize(blur, [new_h, new_w], 'bicubic') resize = True blurPad = np.pad(blur, ((0, H - new_h), (0, W - new_w), (0, 0)), 'edge') else: blurPad = np.pad(blur, ((0, H - h), (0, W - w), (0, 0)), 'edge') blurPad = np.expand_dims(blurPad, 0) if self.args.model != 'color': blurPad = np.transpose(blurPad, (3, 1, 2, 0)) start = time.time() deblur = sess.run(outputs, feed_dict={inputs: blurPad / 255.0}) duration = time.time() - start res = deblur[-1] if self.args.model != 'color': res = np.transpose(res, (3, 1, 2, 0)) res = im2uint8(res[0, :, :, :]) # crop the image into original size if resize: res = res[:new_h, :new_w, :] res = scipy.misc.imresize(res, [h, w], 'bicubic') else: res = res[:h, :w, :] if rot: res = np.transpose(res, [1, 0, 2]) iio.write(output, res)
def main(): # verify input wtype = pick_option(sys.argv, 't', 'db7') if len(sys.argv) > 3: image = piio.read(sys.argv[1]) coarse = piio.read(sys.argv[2]) oname = sys.argv[3] else: print("Incorrect syntax, use:") print(" > " + sys.argv[0] + " image coarse result [-t type]") print(" 2 scale recomposition using wavelets") print(" available types:") for family in pywt.families(): print "%s family:" % family, ', '.join(pywt.wavelist(family)) sys.exit(1) if pywt.Wavelet(wtype).orthogonal == False: print "Warning \'%s\' is not orthogonal"%wtype res = recompose(image, coarse, wtype) piio.write(oname, res);
def main(): # verify input wtype = pick_option(sys.argv, 't', 'db7') if len(sys.argv) > 4: image = piio.read(sys.argv[1]) prefix = sys.argv[2] levels = int(sys.argv[3]) suffix = sys.argv[4] else: print("Incorrect syntax, use:") print(" > " + sys.argv[0] + " input prefix levels suffix [-t type]") print(" multiscale decomposition using wavelets") print(" available types:") for family in pywt.families(): print "%s family:" % family, ', '.join(pywt.wavelist(family)) sys.exit(1) if pywt.Wavelet(wtype).orthogonal == False: print "Warning \'%s\' is not orthogonal"%wtype decompose(image, prefix, levels, suffix, wtype)
def main(): # verify input wtype = pick_option(sys.argv, 't', 'db7') if len(sys.argv) > 4: image = piio.read(sys.argv[1]) prefix = sys.argv[2] levels = int(sys.argv[3]) suffix = sys.argv[4] else: print("Incorrect syntax, use:") print(" > " + sys.argv[0] + " input prefix levels suffix [-t type]") print(" multiscale decomposition using wavelets") print(" available types:") for family in pywt.families(): print "%s family:" % family, ', '.join(pywt.wavelist(family)) sys.exit(1) if pywt.Wavelet(wtype).orthogonal == False: print "Warning \'%s\' is not orthogonal" % wtype decompose(image, prefix, levels, suffix, wtype)
def mosaic(fout, w, h, list_tiles, tw, th, ov): """ Compose several tiles of the same size into a bigger image. Args: fout: path to the output image w, h: output image dimensions list_tiles: list containing paths to the input tiles tw, th: dimensions of a tile (they must all have the same dimensions) ov: overlap between tiles (in pixels) Returns: nothing """ N = len(list_tiles) ntx = np.ceil(float(w - ov) / (tw - ov)).astype(int) nty = np.ceil(float(h - ov) / (th - ov)).astype(int) assert (ntx * nty == N) # default numpy datatype is float64, useless as the ouput file will be # stored with float32 out = np.zeros([h, w], dtype=np.float32) count = np.zeros([h, w], dtype=np.uint8) # loop over all the tiles for j in range(nty): for i in range(ntx): sys.stdout.write("\tPasting tile %02d %02d\r" % (j, i)) sys.stdout.flush() # top-left and bottom-right corners of the tile in the output full # image x0 = i * (tw - ov) y0 = j * (th - ov) x1 = min(x0 + tw, w) y1 = min(y0 + th, h) # read the tile with piio. If the tile has not been produced, # nothing needs to be done. The corresponding pixels will get the # value 'nan' in the output full image. tile_fname = list_tiles[j * ntx + i] if os.path.isfile(tile_fname): tile = piio.read(tile_fname).astype(np.float32)[:, :, 0] assert (np.shape(tile) == (th, tw)) # count the pixels different from nan and inf ind = np.isfinite(tile) count[y0:y1, x0:x1] += ind[:y1 - y0, :x1 - x0] # replace nan and inf with zeros, then add the tile to the # output. ~ind is the negation of ind tile[~ind] = 0 out[y0:y1, x0:x1] += tile[:y1 - y0, :x1 - x0] # free mem if 'tile' in locals(): del tile if 'ind' in locals(): del ind gc.collect() sys.stdout.write('\n') # put nan where count is zero, and take the average where count is nonzero. sys.stdout.write('\tCounting...\n') sys.stdout.flush() ind = (count > 0) sys.stdout.write('\tAveraging...\n') sys.stdout.flush() out[ind] /= count[ind] sys.stdout.write('\tPutting nans on empty pixels...\n') sys.stdout.flush() out[~ind] = np.nan del count # saving the 'out' numpy array in TIFF with piio requires to much memory # (something like twice de file size) because tiled tiff image writing is # not implemented yet in iio. # As an alternative, the numpy array is stored in raw and the libtiff util # 'raw2tiff' is used to produce a tiff file from it. sys.stdout.write('\twriting raw data to disk...\n') raw_file = common.tmpfile('') out.tofile(raw_file) common.run('raw2tiff -w %d -l %d -d float -c zip %s %s' % (w, h, raw_file, fout))
def mosaic(fout, w, h, list_tiles, tw, th, ov): """ Compose several tiles of the same size into a bigger image. Args: fout: path to the output image w, h: output image dimensions list_tiles: list containing paths to the input tiles tw, th: dimensions of a tile (they must all have the same dimensions) ov: overlap between tiles (in pixels) Returns: nothing """ N = len(list_tiles) ntx = np.ceil(float(w - ov) / (tw - ov)).astype(int) nty = np.ceil(float(h - ov) / (th - ov)).astype(int) assert(ntx * nty == N) # default numpy datatype is float64, useless as the ouput file will be # stored with float32 out = np.zeros([h, w], dtype=np.float32) count = np.zeros([h, w], dtype=np.uint8) # loop over all the tiles for j in range(nty): for i in range(ntx): sys.stdout.write("\tPasting tile %02d %02d\r" % (j, i)) sys.stdout.flush() # top-left and bottom-right corners of the tile in the output full # image x0 = i * (tw - ov) y0 = j * (th - ov) x1 = min(x0 + tw, w) y1 = min(y0 + th, h) # read the tile with piio. If the tile has not been produced, # nothing needs to be done. The corresponding pixels will get the # value 'nan' in the output full image. tile_fname = list_tiles[j * ntx + i] if os.path.isfile(tile_fname): tile = piio.read(tile_fname).astype(np.float32)[:, :, 0] assert(np.shape(tile) == (th, tw)) # count the pixels different from nan and inf ind = np.isfinite(tile) count[y0:y1, x0:x1] += ind[:y1 - y0, :x1 - x0] # replace nan and inf with zeros, then add the tile to the # output. ~ind is the negation of ind tile[~ind] = 0 out[y0:y1, x0:x1] += tile[:y1 - y0, :x1 - x0] # free mem if 'tile' in locals(): del tile if 'ind' in locals(): del ind gc.collect() sys.stdout.write('\n') # put nan where count is zero, and take the average where count is nonzero. sys.stdout.write('\tCounting...\n') sys.stdout.flush() ind = (count > 0) sys.stdout.write('\tAveraging...\n') sys.stdout.flush() out[ind] /= count[ind] sys.stdout.write('\tPutting nans on empty pixels...\n') sys.stdout.flush() out[~ind] = np.nan del count # saving the 'out' numpy array in TIFF with piio requires to much memory # (something like twice de file size) because tiled tiff image writing is # not implemented yet in iio. # As an alternative, the numpy array is stored in raw and the libtiff util # 'raw2tiff' is used to produce a tiff file from it. sys.stdout.write('\twriting raw data to disk...\n') raw_file = common.tmpfile('') out.tofile(raw_file) common.run('raw2tiff -w %d -l %d -d float -c zip %s %s' % (w, h, raw_file, fout))
#!/usr/bin/env python from __future__ import division import piio import sys import os.path from matplotlib import cm if __name__ == '__main__': if '-h' in sys.argv: print "Usage: {} input=- output=- map=magma min=input.min() max=input.max()".format(os.path.basename(sys.argv[0])) sys.exit(0) in_image = piio.read(sys.argv[1] if len(sys.argv) > 1 else '-')[:,:,0] vmin = float(sys.argv[4]) if len(sys.argv) > 4 else in_image.min() vmax = float(sys.argv[5]) if len(sys.argv) > 5 else in_image.max() in_image = (in_image - vmin) / (vmax - vmin) cmap = cm.get_cmap(sys.argv[3] if len(sys.argv) > 3 else 'magma') out_image = cmap(in_image)[:,:,:3] piio.write(sys.argv[2] if len(sys.argv) > 2 else 'TIFF:-', out_image)
import sys, piio, demtk x = piio.read(sys.argv[1]).squeeze() piio.write(sys.argv[2], demtk.render(r))
[2, 3]])] rgb = np.array([1, 0, 2, 1]) def expand_GrRBGb(in_image, phase): assert(len(in_image.shape) == 3 and in_image.shape[2] == 4) w, h = in_image.shape[:2] out_image = np.zeros((2 * w, 2 * h, 3), dtype = in_image.dtype) for i in xrange(2): for j in xrange(2): chan = pattern[phase][i, j] out_image[i::2, j::2, rgb[chan]] = in_image[:, :, chan] return out_image if __name__ == '__main__': import piio import sys import os.path if '-h' in sys.argv: print "Usage: {} [phase [input [output]]]".format(os.path.basename(sys.argv[0])) sys.exit(1) phase = int(sys.argv[1]) if len(sys.argv) > 1 else 2 in_image = piio.read(sys.argv[2] if len(sys.argv) > 2 else '-') out_image = expand_GrRBGb(in_image, phase) piio.write(sys.argv[3] if len(sys.argv) > 3 else '-', out_image)
#!/usr/bin/env python import piio d = piio.read('testimg.tif') print d.shape print d[:, :, 0] piio.write('testimg2.png', d)
#!/usr/bin/env python import piio d = piio.read('testimg.tif') print(d.shape) print(d[:,:,0] ) piio.write('testimg2.png',d)
#!/usr/bin/env python3 import numpy import tkinter import piio x = piio.read("x.png").squeeze() x = numpy.uint8(x) x_bytes = x.data.tobytes() WIDTH = x.shape[1] HEIGHT = x.shape[0] window = tkinter.Tk() canvas = tkinter.Canvas(window, width=WIDTH, height=HEIGHT) canvas.pack() xdata = b"P5 %d %d 255\n" % (WIDTH, HEIGHT) + x_bytes + x_bytes + x_bytes image = tkinter.PhotoImage(data=xdata, format="PPM") canvas.create_image(1, 1, image=image, anchor=tkinter.NW) posx = 100 posy = 100 def helloworld(x): global posx global posy image.put("#ff0000", (posx, posy)) posx = posx + 1 print("hello world! \"%s\"" % x)