def show_progress(x, net, title=None, handle=False, dname='', CLC=True, IF_SAVE=False): ''' Helper function to show intermediate results during the gradient descent. :param x: vectorised image on which the gradient descent is performed :param net: caffe.Classifier object defining the network :param title: optional title of figuer :param handle: obtional return of figure handle :return: figure handle (optional) ''' global index disp_image = (x.reshape(*net.blobs['data'].data.shape)[0].transpose(1,2,0)[:,:,::-1]-x.min())/(x.max()-x.min()) if CLC: clear_output() if disp_image.shape[2] == 1: plt.imshow(disp_image[:,:,0], cmap='gray') if IF_SAVE: pimg.imsave(dname+str(index)+'.png',disp_image[:,:,0], cmap='gray') index += 1 else: plt.imshow(disp_image) if title != None: ax = plt.gca() ax.set_title(title) f = plt.gcf() display() plt.show() if handle: return f
def match_score2prim(score,primlist,stafflist,barlist,myimg): notelist = score.getElementsByClass('Part')[0].getElementsByClass('Measure')[0].getElementsByClass('Note') #read all stem primitives stemlist = [] for i in primlist: if i.name == "stem": stemlist.append(i) stemlist.sort(key=lambda x:x.locbegin[0],reverse=False) #for i in stemlist: # print i.locbegin[0] note_range = [] cols = range(barlist[1]-barlist[0])#10 pixels per column, overlap by 5 column_labels = [0 for i in range(len(cols))] for i in range(len(notelist)): dur_label = int(4*notelist[i].duration.quarterLength) pitch_label = notelist[i].pitch left,right = get_stem_box(stemlist[i]) #note_range.append([x,x2]) up = stafflist[0]-20 down = stafflist[4]+20 #print dur_label,left,right cur = myimg[up:down,int(left):int(right)] debugdir = "./debug/" if not os.path.exists(debugdir): os.makedirs(debugdir) pngname = "./debug/"+str(dur_label)+"_"+str(int(left))+".png" mpimg.imsave(pngname,cur) for j in range(int(left)-barlist[0],int(right)-barlist[0]): column_labels[j] = dur_label reply = "" for i in column_labels: reply += str(i) + " " reply += "\n" return reply
def main(): # parse command line arguments parser = argparse.ArgumentParser(description='Colorize pictures') parser.add_argument('greyImage', help='png image to be coloured') parser.add_argument('markedImage', help='png image with colour hints') parser.add_argument('output', help='png output file') parser.add_argument('-v', '--view', help='display image', action='store_true') args = parser.parse_args() # Note: when reading .png, division by 255. is not required # Note: when reading .bmp, division by 255. is required # TODO: make this more universal, i.e., support various image formats # read images greyImage = mpimg.imread(args.greyImage, format='png') markedImage = mpimg.imread(args.markedImage, format='png') # colorize colouredImage = colorize(greyImage, markedImage) # save output mpimg.imsave(args.output,colouredImage, format='png') # display output, if requested if args.view: plt.imshow(colouredImage) plt.show()
def save_image(self, filename, mode='rgb', antialiased=False): """Save view from all panels to disk Parameters ---------- filename: string Path to new image file. mode : string Either 'rgb' (default) to render solid background, or 'rgba' to include alpha channel for transparent background. antialiased : bool Antialias the image (see :func:`mayavi.mlab.screenshot` for details; default False). .. warning:: Antialiasing can interfere with ``rgba`` mode, leading to opaque background. Notes ----- Due to limitations in TraitsUI, if multiple views or hemi='split' is used, there is no guarantee painting of the windows will complete before control is returned to the command line. Thus we strongly recommend using only one figure window (which uses a Mayavi figure to plot instead of TraitsUI) if you intend to script plotting commands. """ im = self.screenshot(mode, antialiased) imsave(filename, im)
def saveVideo(I, IDims, filename, FrameRate = 30, YCbCr = False, Normalize = False): #Overwrite by default if os.path.exists(filename): os.remove(filename) N = I.shape[0] if YCbCr: for i in range(N): frame = np.reshape(I[i, :], IDims) I[i, :] = ntsc2rgb(frame).flatten() if Normalize: I = I-np.min(I) I = I/np.max(I) for i in range(N): frame = np.reshape(I[i, :], IDims) frame[frame < 0] = 0 frame[frame > 1] = 1 mpimage.imsave("%s%i.png"%(TEMP_STR, i+1), frame) if os.path.exists(filename): os.remove(filename) #Convert to video using avconv command = [AVCONV_BIN, '-r', "%i"%FrameRate, '-i', TEMP_STR + '%d.png', '-r', "%i"%FrameRate, '-b', '30000k', filename] subprocess.call(command) #Clean up for i in range(N): os.remove("%s%i.png"%(TEMP_STR, i+1))
def main(): # Check and parse arguments if len(sys.argv) < 2: print("Not enough arguments given. Need full path for the image file.") sys.exit() source = sys.argv[1] im_colors = { 'r':0, 'g':1, 'b':2} for filename in os.listdir(source): filepath = source + filename if os.path.splitext(filepath)[1] == '.jpg': # Load image im = mpim.imread(filepath) im_color = im_colors[filename[-5]] # Switch color channel rows, cols = im.shape im_colored = np.zeros((rows, cols, 3)) im_colored[:,:,im_color] = im / 255. # Save colored image mpim.imsave(os.path.splitext(filepath)[0] + '.png', im_colored, format='PNG')
def _fetch_captchas_to_dir(directory, num=1): plt.ion() plt.show() for i in range(num): img = _captcha_provider.fetch() plt.clf() plt.axis('off') plt.imshow(img) # http://stackoverflow.com/questions/12670101/matplotlib-ion-function # -fails-to-be-interactive # https://github.com/matplotlib/matplotlib/issues/1646/ plt.show() plt.pause(1e-2) while True: seq = input('[{}] Enter the char sequence: '.format(i + 1)) # To skip a CAPTCHA. # Warning: skipping may reduce the quality of the training set. if seq == _SEQ_SKIP: break seq = _captcha_provider.canonicalize_seq(seq) if not _captcha_provider.is_valid_seq(seq): print('Invalid sequence!') else: break if seq == _SEQ_SKIP: print('Skipped manually') continue path = os.path.join(directory, _add_suffix(seq)) if os.path.isfile(path): print('Warning: char sequence already exists in dataset! Skipping') else: mpimg.imsave(path, img) plt.ioff()
def display_image(image): import io from matplotlib.image import imsave from IPython import display buf = io.BytesIO() imsave(buf, image) return display.display_png(display.Image(buf.getvalue()))
def generate_label(num): for i in range(num): pagenum = i print "Label page num",pagenum infile = "./meas/"+"poly_44_"+str(pagenum) #read svg file and assemble svg_file = infile + ".svg" primlist,stafflist,barlist= svg2primlist(svg_file) primitive_assemble(primlist) #read xml file xml_file = infile + ".xml" score = converter.parse(xml_file) #score.show('text') #cut out png, and write out label for each column myimg = mpimg.imread(infile+'.png') extraspace = 30 cur = myimg[stafflist[0]-extraspace:stafflist[4]+extraspace,barlist[0]:barlist[1]] pngname = infile+"_s"+".png" mpimg.imsave(pngname,cur) column_labels,range_labels = match_score2prim(score,primlist,stafflist,barlist,myimg,extraspace) labelfile = infile+"_col.txt" f = open(labelfile,"w") f.write(column_labels) f.close() rangefile = infile+"_s.txt" f2 = open(rangefile,"w") f2.write(range_labels) f2.close()
def averager(imgpaths, width=500, height=600, alpha=False, blur_edges=False, out_filename='result.png', plot=False): size = (height, width) images = [] point_set = [] for path in imgpaths: img, points = load_image_points(path, size) if img is not None: images.append(img) point_set.append(points) ave_points = locator.average_points(point_set) num_images = len(images) result_images = np.zeros(images[0].shape, np.float32) for i in xrange(num_images): result_images += warper.warp_image(images[i], point_set[i], ave_points, size, np.float32) result_image = np.uint8(result_images / num_images) mask = blender.mask_from_points(size, ave_points) if blur_edges: blur_radius = 10 mask = cv2.blur(mask, (blur_radius, blur_radius)) if alpha: result_image = np.dstack((result_image, mask)) mpimg.imsave(out_filename, result_image) if plot: plt.axis('off') plt.imshow(result_image) plt.show()
def warp_report(in_file): """Plot the registration summary using nipy contours""" mni_file = op.join(os.environ["FSL_DIR"], "data/standard/MNI152_T1_1mm_brain.nii.gz") mni_img = nib.load(mni_file) mni_data, mni_aff = mni_img.get_data(), mni_img.get_affine() sub_img = nib.load(in_file) sub_data, sub_aff = sub_img.get_data(), sub_img.get_affine() sub_data[sub_data < 1] = 0 kwargs = dict(draw_cross=False, annotate=False) cut_coords = dict(x=(-45, -12, 12, 45), y=(-55, -25, 5, 45), z=(-30, -5, 20, 40)) colors = sns.color_palette("bright") im_data = dict() for axis in ["x", "y", "z"]: f = plt.figure(figsize=(10, 2.5)) coords = cut_coords[axis] slicer = viz.plot_anat(sub_data, sub_aff, slicer=axis, cut_coords=coords, figure=f, **kwargs) slicer.contour_map(mni_data, mni_aff, colors=colors) fname = "slices_%s.png" % axis f.savefig(fname, facecolor="k", edgecolor="k") im_data[axis] = mplimg.imread(fname) concat_data = [im_data[axis] for axis in ["x", "y", "z"]] concat_data = np.concatenate(concat_data, axis=0) mplimg.imsave("warp_report.png", concat_data) return op.abspath("warp_report.png")
def imgHibrida(): raiz = os.getcwd() filtro = gaussiana(9) alinear(Image.open(raiz + "\human.png"), Image.open(raiz + "\cat.png")) gato = mpimg.imread(raiz + "\cat.png") print gato.shape humano = mpimg.imread(raiz + "\humanAlign.png") gatoConv = lowFilter(gato, filtro) humanoConv = lowFilter(humano, filtro) gatoHighConv = highFilter(gato, gatoConv) plt.show() plt.imshow(gatoHighConv) plt.colorbar() plt.title("Gat(Convolution with hp)") plt.show() plt.imshow(humanoConv) plt.colorbar() plt.title("Human(Convolution with lp)") finalImage = gatoHighConv + humanoConv normalizar(finalImage) plt.show() plt.imshow(finalImage) plt.colorbar() plt.title("Hybrid Image") mpimg.imsave("HybridImage1.png", finalImage)
def post_process(self, sequence): # postprocess a sequence in some way sequence = np.array(sequence[self.params['n_steps']:]) save_generated_example('generated.wav',np.array(sequence)) mpimg.imsave('sequence.png',sequence)
def custom_imsave(path, image): imsave(path, image, cmap=cm.Greys_r) return ret = image.copy() ret = ret / (ret.max() / 255.0) imsave(path, ret.astype(int))
def generate_tweet(): """Generate a tweet and jpg. Returns ------- (status, jpg) : (str, str) """ imageid, fitsurl = select_random_image() log.info('Opening {0}'.format(fitsurl)) fts = fits.open(fitsurl) # Create the status message imgtime = fts[0].header['IMG-TIME'] instrument = fts[0].header['INSTRUME'][8:] exptime = fts[0].header['EXPTIME'] timestr = Time(imgtime).datetime.strftime('%d %b %Y, %H:%M UT').lstrip("0") url = 'http://imagearchives.esac.esa.int/picture.php?/{0}'.format(imageid) status = ('A new #Rosetta image was recently released!\n' '⌚ {}.\n' '📷 #{}.\n' '⌛ {:.2f}s.\n' '🔗 {}'.format(timestr, instrument, exptime, url)) # Create the cropped and scaled image image_cropped = entropy_crop(fts[0].data, width=600, height=300) image_scaled = scale_image(image_cropped, scale='linear', min_percent=0.05, max_percent=99.95) # Save the result as an image image_fn = '/tmp/rosettabot.png' log.info('Writing {0}'.format(image_fn)) imsave(image_fn, image_scaled, cmap=cm.gray) return (status, image_fn)
def savePic2(fname, matrix, mode='normal'): if mode == 'normal': pimg.imsave(fname, matrix) elif mode == 'gray': pimg.imsave(fname, matrix, cmap=mlib.cm.gray) else: pass
def getImagePath(self, img_obj): buffer = io.BytesIO() mpimg.imsave(buffer,img_obj) try: mpimg.imsave(buffer,img_obj) except: print("Error: getImage method must return an image object") return(buffer)
def _save_cm(output_cmap, cmap, format='png', n_colors=256): """ Save the colormap of an image as an image file. """ # save the colormap data = np.arange(0., n_colors) / (n_colors - 1.) data = data.reshape([1, n_colors]) imsave(output_cmap, data, cmap=cmap, format=format)
def extract_hue(infile): """ Returns a hue greyscale image from an rgb image. """ rgb_image = imread(infile) hsv_image = rgb_to_hsv(rgb_image) hue_image = -hsv_image[:,:,0] imsave(fname='%s-hue.png' %(infile.split('.')[0]),arr=hue_image,cmap='gray')
def render_offroad_tile(tile): mask = vecttile_mask(tile) heatmap = heatmap_array(tile_path(os.path.join(tiledir, 'strava'), tile, 'png')) mod = adjust_alpha(heatmap.copy(), mask) path = tile_path(os.path.join(tiledir, 'offroad'), tile, 'png') prep_dirs(path) image.imsave(path, mod) return path
def preview(shape, solutions): import os remove("preview", "*.png") for i, solution in enumerate(solutions): image = matrix_to_image(shape, solution) imsave(os.path.join("preview", "%d.png" % i), image) if is_windows: os.system("preview\\0.png")
def compare_image_lists(new_result, old_result, decimals): fns = ['old.png', 'new.png'] num_images = len(old_result) assert(num_images > 0) for i in range(num_images): mpimg.imsave(fns[0], np.loads(zlib.decompress(old_result[i]))) mpimg.imsave(fns[1], np.loads(zlib.decompress(new_result[i]))) assert compare_images(fns[0], fns[1], 10**(-decimals)) == None for fn in fns: os.remove(fn)
def convert(image_file, outfile): original_image = img.imread(image_file) image = rgb_to_grayscale(original_image) height, width = image.shape print "Converting image ({0}x{1}) to graph...".format(width, height) img.imsave("tmp/grayscale.png", image, cmap="gray") graph = {} # graph has structure vertex_id -> edges counter = 0 for i in xrange(height): for j in xrange(width): my_id = str(i * width + j) edges = [] # create edge between adjaceny pixels top_left = get_edge(i, j, i-1, j-1, height, width, image) top = get_edge(i, j, i-1, j, height, width, image) top_right = get_edge(i, j, i-1, j+1, height, width, image) left = get_edge(i, j, i, j-1, height, width, image) right = get_edge(i, j, i, j+1, height, width, image) bottom_left = get_edge(i, j, i+1, j-1, height, width, image) bottom = get_edge(i, j, i+1, j, height, width, image) bottom_right = get_edge(i, j, i+1, j+1, height, width, image) edges.append(top_left) edges.append(top) edges.append(top_right) edges.append(left) edges.append(right) edges.append(bottom_left) edges.append(bottom) edges.append(bottom_right) # remove out of range edges lst = range(len(edges)) lst.reverse() for k in lst: if edges[k] == None: edges.pop(k) graph[my_id] = edges counter +=1 # super source and super sink source_edges = [] for i in xrange(height): for j in xrange(width): v_id = str(i * width + j) source_edges.append([v_id, source_weight(i, j, image)]) graph[v_id].append(["t", sink_weight(i, j, image)]) graph["s"] = source_edges graph["t"] = [] # write the outfile outfile = open(outfile, "w") for u, edges in graph.iteritems(): outfile.write(json.dumps(u) + "\t" + json.dumps(edges) + "\n")
def write_image(arr, name, suffix='.png', mode=None): from ..base.plots import colormap_for_mode if mode==None: cmap = colormap_for_mode(res['mode']) else: cmap = colormap_for_mode(mode) from ..base import make_filename imagefile = make_filename(args_file, name, suffix, work_dir) import matplotlib.image as pltimg pltimg.imsave(imagefile, arr, dpi=144, cmap=cmap)
def inner(self, *args, **kwargs): if not self.do_plot or self.filepath is None: pass if len(args) > 1 and args[1] is 'save' and self.filepath is not None: filename = self.filepath.format(self.counter - 1) mpimg.imsave(filename, args[0]) print(filename) if self.do_plot: func(self, *args, **kwargs) self.counter += 1
def post_process(self, sequence): # postprocess a sequence in some way sequence = np.array(sequence[self.params['n_steps']:]) mpimg.imsave('sequence.png', sequence, cmap=plt.cm.gray) # ex. convert each vec to binary and print fig = plt.figure(figsize=(10,10)) sub = fig.add_subplot(111) sub.matshow(sequence, cmap=plt.cm.gray) plt.show()
def save_w_as_image( X, in_w, in_h, out_w, out_h, outfile, mode, dpi=288, verbose=True, lms=False, ): assert len(X.shape) == 2, 'imagetiles: size error' if mode=='rgb': vis_ch = 3 elif mode=='rg' or mode=='rg_vs_b': vis_ch = 2 else: vis_ch = 1 x = out_h y = out_w _h = in_h _w = in_w xy, hw = X.shape assert (xy == x*y) and (hw == _h*_w*vis_ch), 'imagetiles: size error' if mode=='rgb': X = stack_matrix(X, _w, y) elif mode=='rg' or mode=='rg_vs_b': X = stack_matrix(X, _w, y, mode=mode) frame = 1 if mode=='lum': Y = N.ones((frame+x*(_h+frame), frame+y*(_w+frame))) * .05 else: Y = N.ones((frame+x*(_h+frame), frame+y*(_w+frame), 3)) * .05 image_id = 0 for xx in range(x): for yy in range(y): if image_id >= xy: break beginH, beginW = frame+xx*(_h+frame), frame+yy*(_w+frame) if mode=='rgb': tile = N.array([N.reshape(X[image_id].T[0], (_h, _w)), N.reshape(X[image_id].T[1], (_h, _w)), N.reshape(X[image_id].T[2], (_h, _w))]).T elif mode=='rg': tile = N.array([N.reshape(X[image_id].T[0], (_h, _w)), N.reshape(X[image_id].T[1], (_h, _w)), N.zeros((_h, _w))]).T elif mode=='rg_vs_b': tile = N.array([N.reshape(X[image_id].T[0], (_h, _w)), N.reshape(X[image_id].T[0], (_h, _w)), N.reshape(X[image_id].T[1], (_h, _w))]).T else: tile = N.reshape(X[image_id], (_h, _w)) Y[beginH : beginH+_h, beginW : beginW+_w] = tile image_id += 1 cmap = colormap_for_mode(mode) Y = normalize_color(Y) pltimg.imsave(outfile, Y, dpi=dpi, cmap=cmap) if (verbose): print 'file:', outfile, 'written.'
def write(self, imagearr, file_name=None): if file_name != None: self.file_name = file_name elif self.file_name != None: pass else: raise Exception(" %s , Undefined file name: " % sys._getframe().f_code.co_name) imagearr.un_normalize() imsave(self.file_name, imagearr.get_uint_array(tdepth=8))
def slice_save(self, outputFile): ''' Processes/saves a single slice. ARGS o outputFile The output filename to save the slice to. ''' if self._verbose: self._log('Output file: %s\n' % outputFile) image.imsave(outputFile, self._2Dslice, cmap = cm.Greys_r)
def save_masks_to_dir(dataset, all_masks): os.mkdir("output/%s" % dataset.name) for t in range(len(dataset.time)): os.mkdir("output/%s/time%02d" % (dataset.name, t)) for s in range(len(dataset.slices)): mask = all_masks[t][s] image.imsave("output/%s/time%02d/slice%02d_mask.png" % (dataset.name, t, s), mask) eroded = binary_erosion(mask) hollow_mask = np.where(eroded, 0, mask) colorimg = cv2.cvtColor(dataset.images[s][t], cv2.COLOR_GRAY2RGB) colorimg = colorimg.astype(np.uint8) colorimg[hollow_mask != 0] = [255, 0, 255] image.imsave("output/%s/time%02d/slice%02d_color.png" % (dataset.name, t, s), colorimg)
def process_image(img): global counter mplimg.imsave( os.path.join(output_dir, "output_{:05d}.jpg".format(counter)), img) counter += 1 return img
keypoint_coord3d_v = sess.run(keypoint_coord3d_tf, feed_dict={image_tf: image_v}) # Classifying based on Geometry if args.solve_by == 0: score_label = predict_by_geometry(keypoint_coord3d_v, known_finger_poses, args.threshold) # Classifying based on Neural networks elif args.solve_by == 1: score_label = predict_by_neural_network(keypoint_coord3d_v, known_finger_poses, args.pb_file, args.threshold) # Classifying based on SVM elif args.solve_by == 2: score_label = predict_by_svm(keypoint_coord3d_v, known_finger_poses, args.svc_file) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(image_raw, score_label, (10, 200), font, 1.0, (255, 0, 0), 2, cv2.LINE_AA) file_name = os.path.basename(img_name) file_name_comp = file_name.split('.') file_save_path = os.path.join(output_path, "{}_out.png".format(file_name_comp[0])) mpimg.imsave(file_save_path, image_raw) print('{} --> {}\n\n'.format(file_name, score_label))
def execute_image_pipeline(self, visualize=False): images = glob.glob(self.output_images_path + 'undist_*.jpg') for idx, fname in enumerate(images): image_fname = ntpath.basename(fname) print("Processing", fname) output_image = os.path.join(self.output_images_path, image_fname) image = mpimg.imread(output_image) self.image_shape = image.shape thresholded_binary, warped, out_img, ploty, left_fitx, right_fitx = \ self.pipeline( image, sobel_kernel_size=self.sobel_kernel_size, sx_thresh=self.sx_thresh, sy_thresh=self.sy_thresh, s_thresh=self.s_thresh, mag_thresh=self.mag_thresh, dir_thresh=self.dir_thresh, test_image_pipeline=True, visualize=True) result, newwarp = self.project_back(image, thresholded_binary, warped, ploty, left_fitx, right_fitx) result = self.plot_dashboard(result, ploty, left_fitx, right_fitx, newwarp) output_image_thres = os.path.join(self.output_images_path, 'thres_' + image_fname) mpimg.imsave(output_image_thres, thresholded_binary, cmap=cm.gray) output_image_warp = os.path.join(self.output_images_path, 'warp_' + 'thres_' + image_fname) mpimg.imsave(output_image_warp, warped, cmap=cm.gray) output_image_out = os.path.join(self.output_images_path, 'out_' + 'warp_' + 'thres_' + image_fname) mpimg.imsave(output_image_out, out_img) output_image_result = os.path.join(self.output_images_path, 'result_' + 'out_' + 'warp_' + 'thres_' + image_fname) mpimg.imsave(output_image_result, result) if visualize: # Plot the result f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9)) f.tight_layout() ax1.imshow(image) ax1.set_title('Undistorted Image', fontsize=30) ax2.imshow(thresholded_binary, cmap='gray') ax2.set_title('Thresholded Result', fontsize=30) plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.) ax2.imshow(warped, cmap='gray') ax2.set_title('Warped Result', fontsize=30) plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.) ax2.imshow(out_img, cmap='gray') ax2.set_title('Line fit', fontsize=30) ax2.plot(left_fitx, ploty, color='yellow') ax2.plot(right_fitx, ploty, color='yellow') plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.) ax1.imshow(result) ax1.set_title('Output Image', fontsize=30)
def find_lines_sliding_window(binary_warped, window_search, display): histogram = np.sum(binary_warped[int(binary_warped.shape[0] / 2):, :], axis=0) out_img = np.dstack((binary_warped, binary_warped, binary_warped)) * 255 # we need max for each half of the histogram. the example above shows how # things could be complicated if didn't split the image in half # before taking the top 2 maxes midpoint = np.int(histogram.shape[0] / 2) leftx_base = np.argmax(histogram[:midpoint]) rightx_base = np.argmax(histogram[midpoint:]) + midpoint # Choose the number of sliding windows # this will throw an error in the height if it doesn't evenly divide the img height nwindows = 9 # Set height of windows window_height = np.int(binary_warped.shape[0] / nwindows) # Identify the x and y positions of all nonzero pixels in the image nonzero = binary_warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) # Current positions to be updated for each window leftx_current = leftx_base rightx_current = rightx_base # Set the width of the windows +/- margin margin = 100 # Set minimum number of pixels found to recenter window minpix = 50 # Create empty lists to receive left and right lane pixel indices left_lane_inds = [] right_lane_inds = [] # Step through the windows one by one for window in range(nwindows): # Identify window boundaries in x and y (and right and left) win_y_low = int(binary_warped.shape[0] - (window + 1) * window_height) win_y_high = int(binary_warped.shape[0] - window * window_height) win_xleft_low = leftx_current - margin win_xleft_high = leftx_current + margin win_xright_low = rightx_current - margin win_xright_high = rightx_current + margin # Draw the windows on the visualization image cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 3) cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 3) # Identify the nonzero pixels in x and y within the window good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0] good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0] # Append these indices to the lists left_lane_inds.append(good_left_inds) right_lane_inds.append(good_right_inds) # If you found > minpix pixels, recenter next window on their mean position if len(good_left_inds) > minpix: leftx_current = np.int(np.mean(nonzerox[good_left_inds])) if len(good_right_inds) > minpix: rightx_current = np.int(np.mean(nonzerox[good_right_inds])) # Concatenate the arrays of indices left_lane_inds = np.concatenate(left_lane_inds) right_lane_inds = np.concatenate(right_lane_inds) # Extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] # Fit a second order polynomial to each left_fit = np.polyfit(lefty, leftx, 2) right_fit = np.polyfit(righty, rightx, 2) # Generate x and y values for plotting ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0]) left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2] right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2] nonzero = binary_warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) margin = 100 left_lane_inds = ( (nonzerox > (left_fit[0] * (nonzeroy**2) + left_fit[1] * nonzeroy + left_fit[2] - margin)) & (nonzerox < (left_fit[0] * (nonzeroy**2) + left_fit[1] * nonzeroy + left_fit[2] + margin))) right_lane_inds = ( (nonzerox > (right_fit[0] * (nonzeroy**2) + right_fit[1] * nonzeroy + right_fit[2] - margin)) & (nonzerox < (right_fit[0] * (nonzeroy**2) + right_fit[1] * nonzeroy + right_fit[2] + margin))) # Again, extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] # Fit a second order polynomial to each left_fit = np.polyfit(lefty, leftx, 2) right_fit = np.polyfit(righty, rightx, 2) if display: out_img[lefty, leftx] = [255, 0, 0] out_img[righty, rightx] = [0, 0, 255] # Plots the left and right polynomials on the lane lines plt.plot(left_fitx, ploty, color='yellow') plt.plot(right_fitx, ploty, color='yellow') plt.imshow(out_img) mpimg.imsave('./output_images/10-window_search.jpg', out_img) return left_fit, right_fit, leftx, lefty, rightx, righty, window_search
figure.append(temp) print("figure:", figure) # print(img.shape) counter = img[position[1]:position[1] + position[3] + 1, position[0]:position[0] + position[2] + 1] counter = rgb2gray(counter) #plt.imshow(counter, cmap='gray', vmin=0, vmax=255) # plt.show() for i in range(5): img_figure = img[figure[i][1]:figure[i][1] + figure[i][3] + 1, figure[i][0]:figure[i][0] + figure[i][2] + 1] img_figure = rgb2gray(img_figure) mpimg.imsave(output_path + str(figure_reading[i]) + '/' + str(index) + '.jpg', img_figure, cmap='gray') index += 1 #plt.imshow(img_figure, cmap='gray', vmin=0, vmax=255) # plt.show() """ subprocess.call("rm -rf " + data_folder + output_all, shell=True) f = open(data_folder + output_all,'w') for path,dir_list,file_list in g: for file_name in file_list: f.write(os.path.join(path, file_name)) f.write('\n') image_path = "../Linux/data/11_3.jpg" img = rgb2gray(mpimg.imread(image_path))
def uploader(): def load(fname): import numpy as np from PIL import Image import matplotlib.pyplot as plt import matplotlib.image as img import math as m import copy dic={} M = img.imread(fname) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+fname, M) dic['M']=M W, H = M.shape[:2] dic['W']=W dic['H']=H NEW = np.random.rand(W,H) dic['NEW']=NEW IMAG= np.random.rand(W,H) dic['IMAG']=IMAG FB=(1.0/273)*np.array([ [1,4,7,4,1], [4,16,26,16,4], [7,26,41,26,7], [4,16,26,16,4], [1,4,7,4,1] ]) FS=(1.0)*np.array([ [0,-1,0], [-1,5,-1], [0,-1,0], ]) dic['FB']=FB dic['FS']=FS return dic def convertGray(fname): import copy import numpy as np d=load(fname) new=copy.deepcopy(d['NEW']) w=copy.deepcopy(d['W']) h=copy.deepcopy(d['H']) m=copy.deepcopy(d['M']) for i in range(w): for j in range(h): avg =float((m[i][j][0]*0.299)+(m[i][j][1]*0.587)+(m[i][j][2]*0.114)) #avg =(m[i][j][0]+m[i][j][1]+m[i][j][2])/3 new[i][j] = avg return copy.deepcopy(new) def blur(fname): import numpy as np import copy d=load(fname) m=copy.deepcopy(d['M']) Xw=Xw=m.shape[1] Xh=m.shape[0] newss=np.zeros((Xh,Xw,3)) F=copy.deepcopy(d['FB']) g=sum(sum(F)) Fh=F.shape[0] Fw=F.shape[1] H=(Fh-1)//2 W=(Fw-1)//2 for i in range(H,Xh-H): for j in range(W,Xw-W): s1=0 s2=0 s3=0 for k in range(-H,H+1): for l in range(-W,W+1): a1=m[i+k,j+l,0] a2=m[i+k,j+l,1] a3=m[i+k,j+l,2] p1=F[H+k,W+l] p2=F[H+k,W+l] p3=F[H+k,W+l] s1+=(p1*a1) s2+=(p2*a2) s3+=(p3*a3) newss[i,j,0]=s1 newss[i,j,1]=s2 newss[i,j,2]=s3 return copy.deepcopy(newss/255) def sharpen(fname): import numpy as np import copy d=load(fname) m=copy.deepcopy(d['M']) Xw=Xw=m.shape[1] Xh=m.shape[0] newsss=np.zeros((Xh,Xw,3)) F=copy.deepcopy(d['FS']) g=sum(sum(F)) Fh=F.shape[0] Fw=F.shape[1] H=(Fh-1)//2 W=(Fw-1)//2 for i in range(H,Xh-H): for j in range(W,Xw-W): s1=0 s2=0 s3=0 for k in range(-H,H+1): for l in range(-W,W+1): a1=m[i+k,j+l,0] a2=m[i+k,j+l,1] a3=m[i+k,j+l,2] p1=F[H+k,W+l] p2=F[H+k,W+l] p3=F[H+k,W+l] s1+=(p1*a1) s2+=(p2*a2) s3+=(p3*a3) newsss[i,j,0]=s1 newsss[i,j,1]=s2 newsss[i,j,2]=s3 return copy.deepcopy(newsss) def histogramequalisation(fname): import copy import numpy as np new=convertGray(fname) pmf={} cmf={} d=load(fname) w=copy.deepcopy(d['W']) h=copy.deepcopy(d['H']) for i in range(w): for j in range(h): if new[i][j] in pmf: pmf[new[i][j]]+=1 else: pmf[new[i][j]]=1 for i in pmf: pmf[i]=pmf[i]/(w*h) s=0 for i in pmf: if s==0: s+=pmf[i] cmf[i]=pmf[i] else: s+=pmf[i] cmf[i]=s for i in cmf: cmf[i]=int(cmf[i]*255) newpmf={} for i in pmf: newpmf[cmf[i]]=pmf[i]*(w*h) pix={} for i in pmf: pix[i]=cmf[i] chan = np.random.rand(w,h) for i in range(w): for j in range(h): chan[i][j]=pix[new[i][j]] return copy.deepcopy(chan) def negative(fname): import numpy as np import copy from copy import deepcopy d=load(fname) w=copy.deepcopy(d['W']) h=copy.deepcopy(d['H']) neg=np.zeros((w,h)) new=convertGray(fname) for i in range(w): for j in range(h): neg[i][j]=255-new[i][j] return deepcopy(neg) def logtransform(fname): import numpy as np from copy import deepcopy import math as m import copy d=load(fname) w=copy.deepcopy(d['W']) h=copy.deepcopy(d['H']) log=np.zeros((w,h)) new=convertGray(fname) for i in range(w): for j in range(h): log[i][j]=0.004*m.log(new[i][j]+1) return deepcopy(log) def gamma(fname): import numpy as np import copy from copy import deepcopy d=load(fname) w=copy.deepcopy(d['W']) h=copy.deepcopy(d['H']) gama=np.zeros((w,h)) new=convertGray(fname) for i in range(w): for j in range(h): gama[i][j]=new[i][j]**1.5 return deepcopy(gama) def linear(fname): import numpy as np import copy from copy import deepcopy d=load(fname) w=copy.deepcopy(d['W']) h=copy.deepcopy(d['H']) lin=np.zeros((w,h)) new=convertGray(fname) for i in range(w): for j in range(h): lin[i][j]=new[i][j]*25+1 return deepcopy(lin) def MedianFilter(image, filter_size): import numpy result = numpy.zeros((len(image),len(image[0]))) filter_range = filter_size // 2 garbage = [] for i in range(len(image)): for j in range(len(image[0])): for z in range(filter_size): if i + z - filter_range < 0 or i + z +filter_range > len(image) - 1: for c in range(filter_size): garbage.append(0) else: if j + z - filter_range < 0 or j + filter_range > len(image[0]) - 1: garbage.append(0) else: for k in range(filter_size): garbage.append(image[i + z - filter_range][j + k - filter_range]) garbage.sort() result[i][j] = garbage[len(garbage) // 2] garbage = [] return result def Mean(image, filter_size): import numpy result = numpy.zeros((len(image),len(image[0]))) filter_range = filter_size // 2 garbage = [] for i in range(len(image)): for j in range(len(image[0])): for z in range(filter_size): if i + z - filter_range < 0 or i + z +filter_range > len(image) - 1: for c in range(filter_size): garbage.append(0) else: if j + z - filter_range < 0 or j + filter_range > len(image[0]) - 1: garbage.append(0) else: for k in range(filter_size): garbage.append(image[i + z - filter_range][j + k - filter_range]) m=sum(garbage)//(len(garbage)) result[i][j] = m garbage = [] return result def rgb2gray(rgb): return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140]) def BandB(new): s=0 w, h = new.shape[:2] for i in range(w): for j in range(h): s+=new[i][j] threshold=s//(new.shape[0]*new.shape[1]) for i in range(w): for j in range(h): if new[i][j]>=threshold: new[i][j]=1 else: new[i][j]=0 return new if request.method == 'POST': import copy import numpy as np from PIL import Image import matplotlib.pyplot as plt import matplotlib.image as img import math as m import copy import cv2 as cv import io f = request.files['file'] f.save(secure_filename(f.filename)) if request.form['submit_button'] == 'Gray': new=convertGray(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'Gray'+str(f.filename), new,cmap='gray') full_filename='Gray'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Blur': new=blur(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'Blur'+str(f.filename), new) full_filename='Blur'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Sharpen Kernel': new=sharpen(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'S'+str(f.filename), new) full_filename='S'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Histogram Equalisation': new=histogramequalisation(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'Histo'+str(f.filename), new,cmap='gray') full_filename='Histo'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Linear Transformation': new=linear(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'linear'+str(f.filename), new,cmap='gray') full_filename='linear'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Gamma Transformation': new=gamma(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'gamma'+str(f.filename), new,cmap='gray') full_filename='gamma'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Negative Transformation': new=negative(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'negative'+str(f.filename), new,cmap='gray') full_filename='negative'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Log Transformation': new=logtransform(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'log'+str(f.filename), new,cmap='gray') full_filename='log'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Median Filtering': import numpy imgi = img.imread(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+str(f.filename), imgi) gray = rgb2gray(imgi) arr = numpy.array(gray) imgi = MedianFilter(arr, 3) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'Med'+str(f.filename), imgi,cmap='gray') full_filename='Med'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Mean Filtering': import numpy imgi = img.imread(str(f.filename)) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+str(f.filename), imgi) gray = rgb2gray(imgi) arr = numpy.array(gray) imgi = Mean(arr, 3) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'Mean'+str(f.filename), imgi,cmap='gray') full_filename='Mean'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) elif request.form['submit_button'] == 'Black & White': new=convertGray(str(f.filename)) new=BandB(new) img.imsave('C:/Users/aditya/Desktop/cs300/static/'+'B&W'+str(f.filename), new,cmap='gray') full_filename='B&W'+str(f.filename) return render_template('image.html',name=full_filename,input=str(f.filename)) return 'file uploaded successfully'
sxbinary = np.zeros_like(scaled_sobel) sxbinary[(scaled_sobel >= thresh_min) & (scaled_sobel <= thresh_max)] = 1 # Threshold color channel s_thresh_min = 170 s_thresh_max = 255 s_binary = np.zeros_like(S) s_binary[(S >= s_thresh_min) & (S <= s_thresh_max)] = 1 combined_binary = np.zeros_like(sxbinary) combined_binary[(s_binary == 1) | (sxbinary == 1)] = 1 plt.figure(c) plt.imshow(combined_binary, cmap='gray') mpimg.imsave("output_images/" + fname + "_combBin.jpg", combined_binary, cmap='gray') #trap = np.array([[[240,686],[1055,675],[690,450],[587,450]]], np.int32) #wwww = np.array([[[100,img.shape[1]],[1100,img.shape[1]],[1100,0],[100,0]]], np.int32) trap = np.array([[[568, 468], [715, 468], [1040, 680], [270, 680]]], np.int32) wwww = np.array([[[200, 0], [1000, 0], [1000, 680], [200, 680]]], np.int32) src = np.float32(trap) dst = np.float32(wwww) M = cv2.getPerspectiveTransform(src, dst) warped = cv2.warpPerspective( combined_binary, M, (combined_binary.shape[1], combined_binary.shape[0]), flags=cv2.INTER_LINEAR)
for epoch in range(total_epochs): for batch in range(total_batchs): batch_x = train_x[batch * batch_size:(batch + 1) * batch_size] noise = random_noise(batch_size) sess.run(g_train, feed_dict={Z: noise}) sess.run(d_train, feed_dict={X: batch_x, Z: noise}) gl, dl = sess.run([g_loss, d_loss], feed_dict={ X: batch_x, Z: noise }) if epoch % 2 == 0: print("======= Epoch : ", epoch, " =======") print("generator: ", -gl) print("discriminator: ", -dl) samples = 20 if epoch % 2 == 0: sample_noise = random_noise(samples) gen = sess.run(fake_x, feed_dict={Z: sample_noise}) for i in range(samples): img = gen[i].reshape([256, 256, 3]) mpimg.imsave('./epoch/epoch' + str(epoch) + '_' + str(i) + '.png', img, format='png')
% Remove the low frequencies from image2. The easiest way to do this is to % subtract a blurred version of image2 from the original version of image2. % This will give you an image centered at zero with negative values. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ lf2 = my_imfilter(image2, filter) lf2 = my_imfilter(lf2, filter_transpose) high_frequencies = image2 - lf2 """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Combine the high frequencies and low frequencies %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ hybrid_image = low_frequencies + high_frequencies #%% Visualize and save outputs plt.figure(1) plt.imshow(low_frequencies) plt.figure(2) plt.imshow(high_frequencies + 0.5) vis = vis_hybrid_image(hybrid_image) #see function script vis_hybrid_image.py plt.figure(3) plt.imshow(vis) mpimg.imsave('Results/low_frequencies.jpg', np.clip(low_frequencies, 0, 1)) mpimg.imsave('Results/high_frequencies.jpg', np.clip(high_frequencies + 0.5, 0, 1.0)) mpimg.imsave('Results/em_hybrid_image.jpg', np.clip(hybrid_image, 0, 1)) mpimg.imsave('Results/em_hybrid_image_scales.jpg', np.clip(vis, 0, 1))
cv2.fillPoly(mask, vertices, ignore_mask_color) masked_edges = cv2.bitwise_and(edges, mask) # Define the Hough transform parameters # Make a blank the same size as our image to draw on rho = 2 # distance resolution in pixels of the Hough grid theta = np.pi / 180 # angular resolution in radians of the Hough grid threshold = 15 # minimum number of votes (intersections in Hough grid cell) min_line_length = 40 #minimum number of pixels making up a line max_line_gap = 20 # maximum gap in pixels between connectable line segments line_image = np.copy(image) * 0 # creating a blank to draw lines on # Run Hough on edge detected image # Output "lines" is an array containing endpoints of detected line segments lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap) # Iterate over the output "lines" and draw lines on a blank image for line in lines: for x1, y1, x2, y2 in line: cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10) # Create a "color" binary image to combine with line image color_edges = np.dstack((edges, edges, edges)) # Draw the lines on the edge image lines_edges = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0) mpimg.imsave('output.jpg', line_image) plt.imshow(lines_edges) plt.show()
import numpy as np import matplotlib.image as img import statistics as stits a = img.imread("Sample.png") h, w = a.shape[:2] k = np.zeros([h, w, 3]) for i in range(0, h): for j in range(0, w): n = [a[i][j][0], a[i][j][1], a[i][j][2]] m = int(stits.mean(n)) k[i][j][0] = int(m) k[i][j][1] = int(m) k[i][j][2] = int(m) img.imsave('test.png', k)
signal = np.clip(signal, -500, 500) # Calculate activation signal return expit(signal) def ReLU_function(signal): # Return the activation signal return np.maximum(0, signal) ################################################################################ # Load Input Image ################################################################################ img = Image.open(os.path.join(PATH_TO_TEST_IMAGES_DIR, PATH_TO_TEST_IMAGE)) # Save original image to the visualization folder image.imsave(os.path.join(PATH_TO_INPUT_IMAGE, '{}.png'.format(0)), img) input_image = np.array(img.getdata(), dtype=np.uint8) input_image = np.resize(input_image, (img.size[1], img.size[0], 4)) input_image = input_image[:, :, 1] ################################################################################ # Convolution 1 ################################################################################ conv1_output = np.empty((20, 20, 256)) for i in range(conv1_weights.shape[3]): # Get the 9x9 kernel extracted_filter = conv1_weights[:, :, :, i] extracted_filter = np.squeeze(extracted_filter) # Save image of the kernel image.imsave(os.path.join(PATH_TO_CONV1_KERNEL, '{}.png'.format(i)),
if __name__ == "__main__": labels = defaultdict(list) with open('training_data/object-detection-crowdai/labels.csv', 'r') as csvfile: label_data = csv.DictReader(csvfile) for row in label_data: if row['Label'] == 'Car': x0, x1, y0, y1 = int(row['xmin']), int(row['ymin']), int( row['xmax']), int(row['ymax']) value = (min(x0, x1), max(x0, x1), min(y0, y1), max(y0, y1)) labels[row['Frame']].append(value) filenames = glob.glob('training_data/object-detection-crowdai/*.jpg') # out_path = 'training_data/udacity/cars/' #to void triggering by mistake and blowing away folder!! if os.path.exists(out_path): shutil.rmtree(out_path) os.makedirs(out_path) i = 0 for fname in filenames: frame_name = os.path.basename(fname) frame_labels = labels.get(frame_name) if frame_labels is not None: image = mpimg.imread(fname) for label in frame_labels: sub_image = image[label[2]:label[3], label[0]:label[1], :] if sub_image.any(): outname = "{}/{}.png".format(out_path, i) mpimg.imsave(outname, sub_image) i += 1
#gray scale import matplotlib.image as img a=img.imread("sample.jpg") w,h=a.shape[:2] import numpy as np b=np.zeros([w,h,3]) import statistics as stits for i in range(0,w): for j in range(0,h): n=[a[i][j][0],a[i][j][1],a[i][j][2]] k=int(stits.mean(n)) b[i][j][0]=int(k) b[i][j][1]=int(k) b[i][j][2]=int(k) img.imsave('shine1.jpg',b)
img = mpimg.imread(img_path) img = np.array(img) mask = mpimg.imread(mask_path) mask = np.array(mask) #converting to grayscale mask_gray = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY) img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) block_sizes = [5, 10, 15, 20, 25, 30] SD_THs = [None, 1, 2, 3, 4] pms = [] counter = 1 for size in block_sizes: for SD_TH in SD_THs: print("size :", size, " SD_TH:", SD_TH) pm = CPF(img_gray, mask_gray, size, mean_threshold, SD_threshold, min_pixel_distance, check_offset, SD_TH) output = str(img_no) + "_" + str(counter) + ".png" mpimg.imsave(output, pm, cmap="gray") counter += 1 plt.show()
# -*- coding: utf-8 -*- """ Generates templates with dots for each domino """ import numpy as np import matplotlib as mpl from matplotlib.image import imsave import cv2 as cv import os folder = "domino_templ" base_images = [] final_images = [] for i in range(0, 7): img = cv.imread(os.path.join(folder, "base/%i.png" % i)) if img is not None: img = img[:, :, 1] base_images.append(img) #imsave(folder + '/0%i.png' %i, img) for i in range(7): for j in range(i + 1): upper_half = base_images[i][:, :] lower_half = base_images[j][::-1, ::-1] img = np.vstack((upper_half[:100, :], lower_half[100:, :])) imsave(folder + '/%i%i.png' % (j, i), 255 - img, cmap='binary')
def simulateByteErrors(I, IDims, fracbyte, doPlot=False): """ Create a version of the video which would occur from bit errors :param I: NFrames x NPixels video array :param IDims: Tuple of the dimensions of the pixels :param fracbyte: The fraction of the bytes that are randomly changed """ TEMPAVI = "temp.avi" LFAC = 400 #How many bytes to corrupt in a row FLIPPROB = 0.5 FrameRate = 30 N = I.shape[0] #Step 1: Output video as temporary files and convert to AVI for i in range(N): frame = np.reshape(I[i, :], IDims) mpimage.imsave("%s%i.png" % (TEMP_STR, i + 1), frame) #Convert to video using avconv if os.path.exists(TEMPAVI): os.remove(TEMPAVI) command = [ AVCONV_BIN, '-r', "%i" % FrameRate, '-i', TEMP_STR + '%d.png', '-r', "%i" % FrameRate, '-b', '30000k', TEMPAVI ] subprocess.call(command) #Step 2: Read in AVI file as a binary file and corrupt it fin = open(TEMPAVI, "rb") b = fin.read() fin.close() #Cleanup leftover files for i in range(N): os.remove("%s%i.png" % (TEMP_STR, i + 1)) os.remove(TEMPAVI) b = bytearray(b) i = 15000 #Make sure the header is skipped lam = LFAC / fracbyte ilocs = [] while i < len(b): i += int(np.random.exponential(lam)) ilocs.append(i) NumFlipped = 0 for i in ilocs: for k in range(i, min(i + LFAC, len(b))): if np.random.rand() < FLIPPROB: b[k] = np.random.randint(255) NumFlipped += 1 if doPlot: #For debugging plt.stem(np.array(ilocs), np.ones(len(ilocs))) plt.title("fracbyte = %g, %g flipped" % (fracbyte, float(NumFlipped) / len(b))) plt.show() fout = open(TEMPAVI, "wb") fout.write(b) fout.close() #Step 3: Use avconv to extract the corrupted frames command = [AVCONV_BIN, '-i', TEMPAVI, '-f', 'image2', TEMP_STR + '%d.png'] subprocess.call(command) os.remove(TEMPAVI) i = 1 frames = [] IDimsRet = IDims while os.path.exists("%s%d.png" % (TEMP_STR, i)): filename = "%s%d.png" % (TEMP_STR, i) F = scipy.misc.imread(filename) IDimsRet = F.shape #os.remove(filename) frames.append(F.flatten()) i += 1 ret = np.array(frames) / 255.0 return (ret, IDimsRet)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ @author: Wenxuan Liu 805152602 """ import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np if __name__ == "__main__": img1 = mpimg.imread('g.jpg').copy() img2 = mpimg.imread('h.jpg').copy() img1 = img1 / 255.0 img2 = img2 / 255.0 a, b, c = img1.shape img3 = np.empty([a, b, c]) for i in range(a): for j in range(b): for k in range(c): img3[i, j, k] = abs(img2[i, j, k]-img1[i, j, k]) #find the difference plt.axis('off') plt.imshow(img3) mpimg.imsave('i.jpg', img3) plt.show()
for idx, fname in enumerate(images): test_img = mpimg.imread(fname) # Find_cars on test images box_list, find_cars_img = find_cars(test_img, ystart=ystart, ystop=ystop, color_space=color_space, scale=scale, svc=svc, X_scaler=X_scaler, orient=orient, pix_per_cell=pix_per_cell, cell_per_block=cell_per_block, spatial_size=spatial_size, hist_bins=hist_bins) mpimg.imsave('./output_images/find_cars_' + str(idx) + '.jpg', find_cars_img) # Add heat to each box in box list heat = np.zeros_like(test_img[:, :, 0]).astype(np.float) heat = add_heat(heat, box_list) # Visualize the heatmap when saving heatmap = np.clip(heat, 0, 255) mpimg.imsave('./output_images/add_heat_' + str(idx) + '.jpg', heatmap, cmap='hot') # Apply threshold to help remove false positives heat = apply_threshold(heat, threshold=threshold) # Visualize the heatmap when saving heatmap = np.clip(heat, 0, 255) mpimg.imsave('./output_images/apply_threshold_' + str(idx) + '.jpg', heatmap, cmap='hot')
""" The image "img" will be repeated n times in vertical and m times in horizontal direction. """ if n == 1: tiled_img = img else: lst_imgs = [] for i in range(n): lst_imgs.append(img) tiled_img = np.concatenate(lst_imgs, axis=1) if m > 1: lst_imgs = [] for i in range(m): lst_imgs.append(tiled_img) tiled_img = np.concatenate(lst_imgs, axis=0) return tiled_img basic_pattern = mpimg.imread('imag_tile_explanation.png') decorators_img = imag_tile(basic_pattern, 3, 3) print decorators_img plt.axis("off") plt.imshow(decorators_img) cropped = basic_pattern[90:150, 50:120] # print(at_img.shape) mpimg.imsave('/home/halk/Pictures/decorators_with_at.png', cropped) mpimg.imsave('/home/halk/Pictures/decorators_with_at1.png', decorators_img)
if i < 2: continue fname = x.split(' ')[0] landmarks = x.split(' ')[1:] landmarks = [int(a) for a in landmarks if len(a) > 0] if len(landmarks) == 10: nose_to_left_eye = landmarks[0] - landmarks[4] eye_distance = landmarks[0] - landmarks[2] rotation = nose_to_left_eye / float(eye_distance) # print('%s %.04f' % (fname, rotation)) if (rotation > 0.35) and (rotation < 0.65): eye_height = (landmarks[1] + landmarks[3]) // 2 mouth_height = (landmarks[7] + landmarks[9]) // 2 nose_height = landmarks[5] # origin is top left thirds_height = mouth_height - eye_height top = eye_height - thirds_height bottom = nose_height + thirds_height left = landmarks[4] - thirds_height right = landmarks[4] + thirds_height # nose_to_right_eye = landmarks[2] - landmarks[4] img = imread('%s/%s' % (src_folder, fname)) crop = img[top:bottom, left:right] imsave('%s/%s' % (dst_folder, fname), crop) # img.close()
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sun Sep 15 15:07:42 2019 @author: nguyent """ import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpg largeur = 600 longueur = int(largeur * 2 / 3) c = int(largeur * 1 / 3) im = np.zeros((longueur, largeur, 3), dtype=np.uint8) im[:, 0:c:1] = [5, 20, 64] im[:, c:2 * c:1] = [255, 255, 255] im[:, 2 * c:3 * c:1] = [236, 25, 32] plt.imshow(im) mpg.imsave('Flag/DrapeauFrancais.png', im)
x0 = start.real - (radius / 2) + (j * radius / nb_pxls) y0 = start.imag - (radius / 2) + (i * radius / nb_pxls) z0 = complex(x0, y0) # gray = (MAX - how_long_within(z0, MAX))/MAX # color = (gray, gray, gray) index = MAX - how_long_within(z0, MAX) color = COLORS[index] mtrx[i, j] = color return mtrx # mtrx[i,j] = if __name__ == "__main__": parser = argparse.ArgumentParser(description='Draw mandelbrot set') parser.add_argument('nb_pixels', type=int, help='size in pixels of the square image') parser.add_argument('xc', type=float, help='real part of the start') parser.add_argument('yc', type=float, help='imaginary part of the start') parser.add_argument('radius', type=float, help='enter smaller numbers to zoom near') args = parser.parse_args() nb_pixels = args.nb_pixels xc = args.xc yc = args.yc radius = args.radius res = mandelbrot_image_matrix(nb_pixels, complex(xc, yc), radius) mpimg.imsave('tes.png', res)
def save_images(model_dict, data_dict, X_test, adv_x, dev_list, mean, rd=None, dr_alg=None): """Save <no_of_img> samples as image files in visual_data folder""" from lib.utils.dr_utils import invert_dr no_of_img = 1 indices = range(no_of_img) X_curr = X_test[indices] channels = data_dict['channels'] atk = model_dict['attack'] dataset = model_dict['dataset'] DR = model_dict['dim_red'] rev = model_dict['rev'] abs_path_v = resolve_path_v(model_dict) if (rd is not None) and (rev is None): X_curr = invert_dr(X_curr, dr_alg, DR) features_per_c = X_curr.shape[-1] height = int(np.sqrt(features_per_c)) width = height X_curr_rev = X_curr.reshape((no_of_img, channels, height, width)) elif (rd is None) or ((rd is not None) and (rev is not None)): height = data_dict['height'] width = data_dict['width'] if channels == 1: dev_count = 0 for dev_mag in dev_list: adv_curr = adv_x[indices, :, dev_count] if (rd is not None) and (rev is None): adv_x_rev = invert_dr(adv_curr, dr_alg, DR) adv_x_rev = adv_x_rev.reshape( (no_of_img, channels, height, width)) for i in indices: adv = adv_x_rev[i].reshape((height, width)) orig = X_curr_rev[i].reshape((height, width)) adv += mean orig += mean img.imsave( abs_path_v + '{}_{}_{}_{}_mag{}.png'.format(atk, i, DR, d, dev_mag), adv * 255, vmin=0, vmax=255, cmap='gray') img.imsave(abs_path_v + '{}_{}_{}_orig.png'.format(i, DR, rd), orig * 255, vmin=0, vmax=255, cmap='gray') elif (rd is None) or (rev is not None): adv_x_curr = adv_x[indices, :, dev_count] for i in indices: adv = adv_x_curr[i].reshape((height, width)) orig = (X_curr[i] + mean[0]).reshape((height, width)) mean_arr = mean[0].reshape((height, width)) adv += mean[0] if rd is not None: fname = abs_path_v + ' {}_{}_{}_rev_{}'.format( atk, i, DR, rd) elif rd is None: fname = abs_path_v + '{}_{}'.format(atk, i) img.imsave(fname + '_mag{}.png'.format(dev_mag), adv * 255, vmin=0, vmax=255, cmap='gray') img.imsave(fname + '_orig.png', orig * 255, vmin=0, vmax=255, cmap='gray') dev_count += 1
def main(data_set, model_type, latent_dim, shortcut='True', num_epoch=100, log_gamma_decay=0.0, beta_policy_type='constant'): data_dir = data_folder(data_set) if data_dir == '': print('No such data set named {0}.'.format(data_set)) return x = load_img_from_folder(data_dir, num_img=50) if model_type == 'VAE': variational = True elif model_type == 'AE': variational = False else: print('No such model type named {0}.'.format(model_type)) return if shortcut == 'True': resnet = True elif shortcut == 'False': resnet = False else: print('Shortcut setting unclear: {0}.'.format(shortcut)) return if beta_policy_type == 'constant': beta_policy = constant_beta elif beta_policy_type == 'linear': beta_policy = linear_beta else: print('No beta policy named {0}.'.format(beta_policy_type)) return # model = VaeNet(variational, latent_dim=latent_dim, shortcut=resnet, init_log_gamma=-4.0, log_gamma_decay=log_gamma_decay) # if not os.path.exists('model'): # os.mkdir('model') # with tf.Session() as sess: # sess.run(tf.global_variables_initializer()) # saver = tf.train.Saver() # writer = tf.summary.FileWriter('graph', sess.graph) # train_model(model, sess, writer, x, num_epoch, 0.002, 32, beta_policy) # saver.save(sess, 'model/model.ckpt') # tf.reset_default_graph() model = VaeNet(variational, latent_dim=latent_dim, shortcut=resnet, is_train=False) with tf.Session() as sess: saver = tf.train.Saver() saver.restore( sess, './experiment/HYPER_GammaDecay_Policy/Flower_000_constant/model/model.ckpt' ) samples = generate_sample(model, sess, 500) if not os.path.exists('samples'): os.mkdir('samples') for i in range(500): mpimg.imsave('samples/' + str(i) + '.jpg', samples[i, :, :, :], 0.0, 1.0)
raw_imgs = draw_boxes(raw_imgs, boxes) # save found boxes for box in boxes: box_list.append(box) # print(box_list) # Add heat to each box in box list heat = add_heat(heat, box_list) # filter out multiple detections and false positives tresh_heat = np.copy(heat) tresh_heat = apply_threshold(tresh_heat, 3) # visualize the heatmap when displaying heatmap = np.clip(tresh_heat, 0, 255) # process labels labels = label(heatmap) draw_img = draw_labeled_bboxes(np.copy(img), labels) print(labels[1], 'cars found in file', fname) # plot images mpimg.imsave('./output_images/' + fname[-9:-4] + '_cars_found.jpg', raw_imgs) mpimg.imsave('./output_images/' + fname[-9:-4] + '_heat.jpg', heat) mpimg.imsave('./output_images/' + fname[-9:-4] + '_tresh_heat.jpg', tresh_heat) mpimg.imsave('./output_images/' + fname[-9:-4] + '_processed.jpg', draw_img) # mpimg.imsave('./output_images/'+fname[-9:-4]+'_windows.jpg', window_img)
def draw_lines(img, lines, color=[255, 0, 0], thickness=6, output_path="test_images/annotated/"): """workflow: 1) examine each individual line returned by hough & determine if it's in left or right lane by its slope because we are working "upside down" with the array, the left lane will have a negative slope and right positive 2) track extrema 3) compute averages 4) solve for b intercept 5) use extrema to solve for points 6) smooth frames and cache """ y_global_min = img.shape[ 0] #min will be the "highest" y value, or point down the road away from car y_max = img.shape[0] l_slope, r_slope = [], [] l_lane, r_lane = [], [] det_slope = 0.4 hough_lines_img = copy.deepcopy(img) print "Found {0} lines in the image.".format(len(lines)) for line in lines: #1 for x1, y1, x2, y2 in line: cv2.line(hough_lines_img, (x1, y1), (x2, y2), color, thickness) slope = get_slope(x1, y1, x2, y2) if slope > det_slope: r_slope.append(slope) r_lane.append(line) elif slope < -det_slope: l_slope.append(slope) l_lane.append(line) #2 y_global_min = min(y1, y2, y_global_min) mpimg.imsave(output_path + "6a-hough_lines.jpg", hough_lines_img) # to prevent errors in challenge video from dividing by zero if ((len(l_lane) == 0) or (len(r_lane) == 0)): print('no lane detected') return 1 #3 l_slope_mean = np.mean(l_slope, axis=0) r_slope_mean = np.mean(r_slope, axis=0) l_mean = np.mean(np.array(l_lane), axis=0) r_mean = np.mean(np.array(r_lane), axis=0) if ((r_slope_mean == 0) or (l_slope_mean == 0)): print('dividing by zero') return 1 #4, y=mx+b -> b = y -mx l_b = l_mean[0][1] - (l_slope_mean * l_mean[0][0]) r_b = r_mean[0][1] - (r_slope_mean * r_mean[0][0]) #5, using y-extrema (#2), b intercept (#4), and slope (#3) solve for x using y=mx+b # x = (y-b)/m # these 4 points are our two lines that we will pass to the draw function l_x1 = int((y_global_min - l_b) / l_slope_mean) l_x2 = int((y_max - l_b) / l_slope_mean) r_x1 = int((y_global_min - r_b) / r_slope_mean) r_x2 = int((y_max - r_b) / r_slope_mean) #6 if l_x1 > r_x1: l_x1 = int((l_x1 + r_x1) / 2) r_x1 = l_x1 l_y1 = int((l_slope_mean * l_x1) + l_b) r_y1 = int((r_slope_mean * r_x1) + r_b) l_y2 = int((l_slope_mean * l_x2) + l_b) r_y2 = int((r_slope_mean * r_x2) + r_b) else: l_y1 = y_global_min l_y2 = y_max r_y1 = y_global_min r_y2 = y_max vertices = np.array([l_x1, l_y1, l_x2, l_y2, r_x1, r_y1, r_x2, r_y2], dtype=np.int32) cv2.line(img, (vertices[0], vertices[1]), (vertices[2], vertices[3]), color, thickness) cv2.line(img, (vertices[4], vertices[5]), (vertices[6], vertices[7]), color, thickness)
def process_frame(image, output_path="test_images/annotated/"): imshape = image.shape print "imshape =", imshape # KITCHEN # lower_left = [0, imshape[0]] # lower_right = [imshape[1]*9/10, imshape[0]] # top_left = [0, imshape[0]/4] # top_right = [imshape[1]*7/8, imshape[0]/4] # HIGHWAY # lower_left = [0, imshape[0]] # lower_right = [imshape[1]*7/9, imshape[0]] # top_left = [imshape[1]*3/8, imshape[0]*4/10] # top_right = [imshape[1]*6/9, imshape[0]*4/10] # BALCANI # lower_left = [imshape[1]/8, imshape[0]] # lower_right = [imshape[1]*7/8, imshape[0]] # top_left = [imshape[1]*3/7, imshape[0]*5/8] # top_right = [imshape[1]*4/7, imshape[0]*5/8] # DEFAULT lower_left = [imshape[1] / 9, imshape[0]] lower_right = [imshape[1] * 8 / 9, imshape[0]] top_left = [imshape[1] * 3 / 8, imshape[0] * 6 / 10] top_right = [imshape[1] * 5 / 8, imshape[0] * 6 / 10] roi_lines_image = copy.deepcopy(image) color = [0, 255, 0] thickness = 3 cv2.line(roi_lines_image, (lower_left[0], lower_left[1]), (top_left[0], top_left[1]), color, thickness) cv2.line(roi_lines_image, (top_left[0], top_left[1]), (top_right[0], top_right[1]), color, thickness) cv2.line(roi_lines_image, (top_right[0], top_right[1]), (lower_right[0], lower_right[1]), color, thickness) cv2.line(roi_lines_image, (lower_right[0], lower_right[1]), (lower_left[0], lower_left[1]), color, thickness) mpimg.imsave(output_path + "1a-roi_lines_image.jpg", roi_lines_image) vertices = [ np.array([lower_left, top_left, top_right, lower_right], dtype=np.int32) ] roi_image = region_of_interest(image, vertices) mpimg.imsave(output_path + "1b-roi_image.jpg", roi_image) gray_image = grayscale(roi_image) mpimg.imsave(output_path + "1-grayscale.jpg", gray_image) img_hsv = cv2.cvtColor(roi_image, cv2.COLOR_RGB2HSV) mpimg.imsave(output_path + "2-hsv.jpg", img_hsv) #hsv = [hue, saturation, value] #more accurate range for yellow since it is not strictly black, white, r, g, or b lower_yellow = np.array([20, 100, 100], dtype="uint8") upper_yellow = np.array([30, 255, 255], dtype="uint8") mask_yellow = cv2.inRange(img_hsv, lower_yellow, upper_yellow) mpimg.imsave(output_path + "3a-mask_yelow.jpg", mask_yellow) mask_white = cv2.inRange(gray_image, 180, 255) mpimg.imsave(output_path + "3b-mask_white.jpg", mask_white) mask_yw = cv2.bitwise_or(mask_white, mask_yellow) mask_yw_image = cv2.bitwise_and(gray_image, mask_yw) mpimg.imsave(output_path + "3c-mask_yellow_white.jpg", mask_yw_image) kernel_size = 5 gauss_gray = gaussian_blur(mask_yw_image, kernel_size) mpimg.imsave(output_path + "4-gauss_gray.jpg", gauss_gray) #same as quiz values low_threshold = 50 high_threshold = 150 canny_edges = canny(gauss_gray, low_threshold, high_threshold) mpimg.imsave(output_path + "5-canny_edges.jpg", canny_edges) #rho and theta are the distance and angular resolution of the grid in Hough space #same values as quiz rho = 2 theta = np.pi / 180 #threshold is minimum number of intersections in a grid for candidate line to go to output threshold = 30 min_line_len = 50 max_line_gap = 100 line_image = hough_lines(canny_edges, rho, theta, threshold, min_line_len, max_line_gap, output_path=output_path) mpimg.imsave(output_path + "6b-hough_lines.jpg", line_image) result = weighted_img(line_image, image, alpha=0.8, beta=1., gamma=0.) return result
return samples if __name__ == "__main__": dbTrain = MyDatabase("database/train", "database/train/data_train.csv") print("Train db length: ", len(dbTrain)) edge = Edge() dbTest = MyDatabase("database/test", "database/test/data_test.csv") print("Test db length: ", len(dbTest)) # check shape assert edge_kernels.shape == (5, 2, 2) # evaluate database APs, res = myevaluate(dbTrain, dbTest, edge.make_samples, depth=depth, d_type="d1") # add pictures in prediction folder under the predicted class folder path = "/Users/johanncarfantan/Documents/ENSSAT/IMR3/AnalyseDimages/Partie 1/predictions/" for i in range(len(dbTest)): saveName = path + res[i] + "/" + dbTest.data.img[i].split('/')[-1] bid = imageio.imread(dbTest.data.img[i]) if not os.path.exists(path + res[i]): os.makedirs(path + res[i]) mpimg.imsave(saveName, bid / 255.)
im = mplimg.imread(imgPath) # Do rectification in two stages imA, HA = rectifyAffineF(im, nLinePairs) plt.close('all') imM, HM = rectifyMetricF(imA, nLinePairs) # Translate and scale HM * HA to the image H = translateAndScaleHToImage(np.dot(HM, HA), imA.shape) # Apply translated and scaled version of HM * HA imM = myApplyH(im, H) # Chain the two transforms plt.close('all') # Show result imshow(np.concatenate((im, imA, imM), axis=1)) axis('off') plt.suptitle( 'Original (left), Affine rectified (middle), Metric rectification (right)') margin = 0.05 plt.subplots_adjust(left=margin, right=1.0 - margin, top=1.0 - margin, bottom=margin) show() # Save output imgPathRect = fileparts[0] + "_rect" + fileparts[1] print("Saving output to %s..." % imgPathRect) mplimg.imsave(imgPathRect, cropOuterRegion(imM))