def cpu(x): import tempfile, piio, os f = f"{tempfile.NamedTemporaryFile().name}.tiff" c = f"cpu {f} 2>/dev/null ; rm {f}" piio.write(f, x) os.system(c)
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 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 decompose(image, prefix, levels, suffix, wtype, cur=0): print prefix + str(cur) + suffix piio.write(prefix + str(cur) + suffix, image) print image.shape if levels == 1: return ret, _ = pywt.dwt2(image, wtype, axes=(0, 1)) decompose(ret / 2.0, prefix, levels - 1, suffix, wtype, cur + 1)
def decompose(image, prefix, levels, suffix, wtype, cur=0): print prefix+str(cur)+suffix piio.write(prefix+str(cur)+suffix, image) print image.shape if levels==1: return ret, _ = pywt.dwt2(image,wtype,axes=(0,1)) decompose(ret/2.0,prefix,levels-1,suffix, wtype, cur+1)
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 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: prefix = sys.argv[1] levels = int(sys.argv[2]) suffix = sys.argv[3] oname = sys.argv[4] else: print("Incorrect syntax, use:") print(" > " + sys.argv[0] + " prefix levels suffix output [-t type]") print(" multiscale 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 image = recompose(prefix, levels, suffix, wtype) piio.write(oname, image);
#!/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)
[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)
import sys, piio, demtk x = piio.read(sys.argv[1]).squeeze() piio.write(sys.argv[2], demtk.render(r))
def keyboard_callback(window, key, scancode, action, mods): global V,D if V.mute_keyboard: # only mute the spacebar event return #print key # navigate winx, winy= glfw.glfwGetFramebufferSize(window) if key==glfw.GLFW_KEY_RIGHT and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.translation_update(winx/4/V.zoom_param,0) elif key==glfw.GLFW_KEY_UP and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.translation_update(0,-winy/4/V.zoom_param) elif key==glfw.GLFW_KEY_LEFT and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.translation_update(-winx/4/V.zoom_param,0) elif key==glfw.GLFW_KEY_DOWN and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.translation_update(0,winy/4/V.zoom_param) #contrast change if key==glfw.GLFW_KEY_E and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.radius_update(1) if key==glfw.GLFW_KEY_D and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.radius_update(-1) if key==glfw.GLFW_KEY_C and action==glfw.GLFW_PRESS: V.reset_scale_bias() if V.shift_is_pressed: V.TOGGLE_AUTOMATIC_RANGE = (V.TOGGLE_AUTOMATIC_RANGE + 1) % 2 if V.TOGGLE_AUTOMATIC_RANGE: print("automatic range enabled") else: print("automatic range disabled") if key==glfw.GLFW_KEY_B and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.reset_range_to_8bits() V.TOGGLE_AUTOMATIC_RANGE = 0 print("range set to [0,255]") # (SAVE) write current buffer if key==glfw.GLFW_KEY_S and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): import numpy as np import piio w,h=V.winx,V.winy data = glReadPixels (0,0,w,h, GL_RGB, GL_UNSIGNED_BYTE) iimage = np.fromstring(data, dtype=np.uint8, count=w*h*3).reshape((h,w,3)) n=0 # TODO determine next snapshot print('saving' + 'snap%02d.png'%n) piio.write('snap%02d.png'%n, iimage[::-1,:,0:3]) ### from http://nullege.com/codes/show/src@g@[email protected]@[email protected]/313/OpenGL.GL.glReadPixels #from PIL import Image #image = Image.fromstring('RGBA', (w,h), data) #image = image.transpose(Image.FLIP_TOP_BOTTOM) #image.save ('save.png') # zoom if key==glfw.GLFW_KEY_P and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.zoom_update(+1) if key==glfw.GLFW_KEY_M and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): V.zoom_update(-1) # fit image to window if key==glfw.GLFW_KEY_F and action==glfw.GLFW_PRESS: V.TOGGLE_FIT_TO_WINDOW_SIZE = (V.TOGGLE_FIT_TO_WINDOW_SIZE + 1) % 2 if V.TOGGLE_FIT_TO_WINDOW_SIZE: print("ENABLE: fit image to window") V.update_zoom_position_to_fit_window() V.redisp = 1 else: print("DISABLE: fit image to window") #V.reset_zoom() V.redisp = 1 # reset visualization if key==glfw.GLFW_KEY_R and action==glfw.GLFW_PRESS: V.reset_zoom() V.reset_scale_bias() # reset visualization if key==glfw.GLFW_KEY_1 and action==glfw.GLFW_PRESS: V.TOGGLE_FLOW_COLORS = (V.TOGGLE_FLOW_COLORS + 1) % 5 V.redisp = 1 # modifier keys if key==glfw.GLFW_KEY_LEFT_SHIFT and action==glfw.GLFW_PRESS: V.shift_is_pressed=1 if key==glfw.GLFW_KEY_LEFT_SHIFT and action==glfw.GLFW_RELEASE: V.shift_is_pressed=0 if key==glfw.GLFW_KEY_Z and action==glfw.GLFW_PRESS: V.alt_is_pressed=1 if key==glfw.GLFW_KEY_Z and action==glfw.GLFW_RELEASE: V.alt_is_pressed=0 # CHANGE IMAGE TODO: use DataBackend DD global current_image_idx new_current_image_idx = current_image_idx if key==glfw.GLFW_KEY_SPACE and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): new_current_image_idx = change_image(current_image_idx+1) if V.TOGGLE_AUTOMATIC_RANGE: V.reset_scale_bias() V.mute_keyboard=1 if key==glfw.GLFW_KEY_BACKSPACE and (action==glfw.GLFW_PRESS or action==glfw.GLFW_REPEAT): new_current_image_idx = change_image(current_image_idx-1) if V.TOGGLE_AUTOMATIC_RANGE: V.reset_scale_bias() V.mute_keyboard=1 if not new_current_image_idx == current_image_idx: current_image_idx = new_current_image_idx V.redisp=1 V.resize=1 # display hud if key==glfw.GLFW_KEY_U and action==glfw.GLFW_PRESS: V.display_hud=(V.display_hud+1)%2 V.redisp=1 # help if key==glfw.GLFW_KEY_H and action==glfw.GLFW_PRESS: print("Q : quit") print("U : show/hide HUD") print("arrows: pan image") print("P,M : zoom image in/out") print("Z : zoom modifier for the mouse wheel") print("F : fit image to window size") print("C : reset intensity range") print("shiftC: automatically reset range") print("B : set range to [0:255]") print("D,E : range scale up/down") print("R : reset visualization: zoom,pan,range") print("1 : toggle optic flow coloring") print("S : capture a snap##.png of the current view") print("mouse wheel: contrast center") print("mouse wheel+shift: contrast scale") print("mouse motion+shift: contrast center") print("space/backspace : next/prev image") # exit if (key==glfw.GLFW_KEY_Q or key==glfw.GLFW_KEY_ESCAPE ) and action ==glfw.GLFW_PRESS: glfw.glfwSetWindowShouldClose(window,1) global x0,y0,w0,h0 print(x0,y0,w0,h0) sys.exit(0)
#!/usr/bin/env python import piio d = piio.read('testimg.tif') print d.shape print d[:, :, 0] piio.write('testimg2.png', d)