def f2(): import pgm # # imgs is the image to be modified # imgt is the reference image # imgarray,w,h = pgm.pgmread('histo_inp_image.pgm') #imgarray,w,h = pgm.pgmread('histo_ref_image.pgm') #imgarray,w,h = pgm.pgmread('histo_inp_image2.pgm') #imgarray,w,h = pgm.pgmread('histo_ref_image2.pgm') #imgarray,w,h = pgm.pgmread('lena.pgm') #imgarray,w,h = pgm.pgmread('fourier_transform.pgm') #imgarray,w,h = pgm.pgmread('Fourier.pgm') imgs = Img.Img(imgarray) # #imgarray,w,h = pgm.pgmread('histo_inp_image.pgm') imgarray,w,h = pgm.pgmread('histo_ref_image.pgm') #imgarray,w,h = pgm.pgmread('histo_inp_image2.pgm') #imgarray,w,h = pgm.pgmread('histo_ref_image2.pgm') #imgarray,w,h = pgm.pgmread('lena.pgm') #imgarray,w,h = pgm.pgmread('fourier_transform.pgm') #imgarray,w,h = pgm.pgmread('Fourier.pgm') imgt = Img.Img(imgarray) # imgt1 = imgs.modify_hist(imgt) plot_fig(imgs.pix, 'Original image') plot_fig(imgt.pix, 'Reference image') plot_fig(imgt1.pix, 'Modified original image') stem_hist(imgs, 'Original image') stem_hist(imgt, 'Reference image') stem_hist(imgt1, 'Modified original image') print np.sum((imgs.pix - imgt1.pix)**2)
def f1(): """ Convolves image and kernel """ (imgarr, w, h) = pgm.pgmread('cameraman.pgm') imgs = Img.Img(imgarr) k1 = Img.create_gauss_kernel(Img.find_gauss_support(0.8)) k2 = Img.create_gauss_kernel(Img.find_gauss_support(1.2)) k3 = Img.create_gauss_kernel(Img.find_gauss_support(1.6)) imgt1 = imgs.conv(k1) imgt2 = imgs.conv(k2) imgt3 = imgs.conv(k3) # print imgt1.col, imgt1.row # print imgt2.col, imgt2.row # print imgt3.col, imgt3.row plt.figure(1) plt.imshow(imgs.pix, cmap=cm.gray) plt.title('Original image') plt.figure(2) plt.imshow(imgt1.pix, cmap=cm.gray) plt.title('Img convolved with Gaussian kernel' + str(shape(k1))) plt.figure(3) plt.imshow(imgt2.pix, cmap=cm.gray) plt.title('Img convolved with Gaussian kernel' + str(shape(k2))) plt.figure(4) plt.imshow(imgt3.pix, cmap=cm.gray) plt.title('Img convolved with Gaussian kernel' + str(shape(k3))) plt.show()
def f1(): import pgm #imgarray,w,h = pgm.pgmread('histo_inp_image.pgm') #imgarray,w,h = pgm.pgmread('histo_ref_image.pgm') #imgarray,w,h = pgm.pgmread('histo_inp_image2.pgm') #imgarray,w,h = pgm.pgmread('histo_ref_image2.pgm') imgarray,w,h = pgm.pgmread('lena.pgm') #imgarray,w,h = pgm.pgmread('fourier_transform.pgm') imgs = Img.Img(imgarray) imgt = imgs.add_salt_pepper(0.65) imgt1 = imgt.median_filter(np.ones((3,3))) imgt1 = imgt1.median_filter(np.ones((5,5))) #imgt1 = imgt.median_filter(np.ones((7,7))) #imgt1 = imgt.median_filter(np.array([[2,2,2],[2,1,2],[2,2,2]])) k = Img.create_gauss_kernel(Img.find_gauss_support(0.75), 0.75) imgt2 = imgt.conv(k) plot_fig(imgs.pix, 'Original image') plot_fig(imgt.pix, 'Added salt and pepper noise') plot_fig(imgt1.pix, 'Median filtered image') plot_fig(imgt2.pix, 'Gaussian LP filtered image') #plot_hist(imgs, 'Original image') #plot_hist(imgt, 'Added salt and pepper noise') #plot_hist(imgt1, 'Median filtered image') #plot_hist(imgt2, 'Gaussian LP filtered image') print np.sum((imgs.pix - imgt1.pix)**2)
def calc_sml(file1, file2, q): """ Calculates SML for file1 frames with q(defined as in class), and store as mat file file2 """ stack = get_stack(file1) stackOut = dict() # Define output stack as dict for key in sorted(stack.keys()): print key imgOut = Img.Img(stack[key]).sum_mod_lap(q) # create a new Img class stackOut[key] = imgOut.pix # Add array to output stack io.savemat(file2, stackOut)
def Preprocessing(img, option='edge'): imdata = Img(img) if (option == 'raw' or option == 'RAW' or option == 'Raw'): imdata.setGrayscaleBuffer() imdata.znorm() imdata.normalize() elif (option == 'edge' or option == 'EDGE' or option == 'Edge'): imdata.setGrayscaleBuffer() imdata.znorm() imdata = ExtractEdge(imdata) #imdata.display() return imdata
def f3(): import pgm imgarray,w,h = pgm.pgmread('histo_inp_image.pgm') #imgarray,w,h = pgm.pgmread('histo_ref_image.pgm') #imgarray,w,h = pgm.pgmread('histo_inp_image2.pgm') #imgarray,w,h = pgm.pgmread('histo_ref_image2.pgm') #imgarray,w,h = pgm.pgmread('lena.pgm') #imgarray,w,h = pgm.pgmread('fourier_transform.pgm') #imgarray,w,h = pgm.pgmread('Fourier.pgm') #imgarray,w,h = pgm.pgmread('academy.pgm') imgs = Img.Img(imgarray) imgt = imgs.hist_eq() plot_fig(imgs.pix, 'Original image') plot_fig(imgt.pix, 'Histogram equalized image') stem_hist(imgs, 'Original image') stem_hist(imgt, 'Histogram equalized image') print np.sum((imgs.pix - imgt.pix)**2)
def f2(): """ Scale image """ factor = (0.8, 0.8) (imgarr, w, h) = pgm.pgmread('cameraman.pgm') imgs = Img.Img(imgarr) imgt1 = imgs.scale(factor) imgt2 = imgs.scale(factor, type='bilinear') plt.figure(1) plt.imshow(imgs.pix, cmap=cm.gray) plt.title('Original image') plt.figure(2) plt.imshow(imgt1.pix, cmap=cm.gray) plt.title('Scale factor = ' + str(factor)) plt.figure(3) plt.imshow(imgt2.pix, cmap=cm.gray) plt.title('Scale factor = ' + str(factor) + '\nwith bilinear interpolation') plt.show()
def __init__(self, bs, n=None): print('Building computational graph...') self.bs = bs self.path = filepath self.I = Img() self.n = n self.I.trainlist = self.I.trainlist[0:n] x = T.tensor4('inputs') y = T.tensor4('targets') loss = self.build(x, y) updat = Tool.adam(loss, self.params) self.train = theano.function([x, y], loss, updates=updat) self.predict = theano.function([x], self.Y) print('Computational graph built.')
def GetFaceChip(im, src_leye, src_reye, dst_leye, dst_reye, dst_size, channel='gray'): imdata = Img(im) if (channel=='gray' or channel=='Gray' or channel=='GRAY'): imdata.setGrayscaleBuffer() elif (channel=='RGB'): imdata.setRGBBuffer() elif (channel=='red' or channel=='Red' or channel=='RED'): imdata.setRedBuffer() elif (channel=='I'): imdata.setChrominanceBuffer() else: raise TypeError("Unsuppoted Color Channel: %s ..."%channel) ## smooth the image before applying affine transformation imdata = FaceSmoothing(imdata, src_leye, src_reye) imdata = AffineFromPoints(imdata, src_leye, src_reye, dst_leye, dst_reye, dst_size) return imdata
def sum_mod_lap_test(): imgs = Img.Img(np.array([[1, 2, 3], [0, 1, 5], [6, 3, 9]])) imgt = imgs.sum_mod_lap(1) print imgs.pix print imgt.pix
def g1(): import pgm imgarray,w,h = pgm.pgmread('test.pgm') imgs = Img.Img(imgarray) imgt1 = imgs.median_filter(np.ones((3,3))) pgm.pgmwrite(imgt1.pix, 'test2.pgm')
imgarray = img.calc_hist() plt.figure() plt.plot(imgarray) plt.title(plotTitle) def plot_fig(imgarr, plotTitle): plt.figure() plt.imshow(imgarr, cmap=cm.gray) plt.title(plotTitle) if __name__ == '__main__': import pgm (imgarray, w, h) = pgm.pgmread('academy.pgm') img = Img.Img(imgarray) # plot_fig(np.int32(img.pix), 'Original mage') # plot_hist(img, 'Original image') imgarray = add_noise(imgarray) imgs = Img.Img(imgarray) plot_fig(imgs.pix, 'Image with additive Gaussian with sigma=5') pgm.pgmwrite(np.int32(imgs.pix), 'academy_with_noise.pgm') # plot_hist(img, 'Noise image') ## # f1(imgs, 1, 1) # f1(imgs, 1, 10) # f1(imgs, 1, 100) # f1(imgs, 1, 300) ## # f1(imgs, 3, 1) # f1(imgs, 3, 10)
image_no = 6 #Insert the 5 epoc numbers you'd like to compare. model_trained = [5, 10, 15, 20, 25] for imgno in range(9): image_no = imgno reconstructions = [] for i in range(5): M = Model(batch_size) M.__load__(model_trained[i]) M.Generate(batch_type, batch_no) reconstructions.append(M.valid_recon[image_no]) I = Img() curbatch, curcrop = I.load_batch(batch_size, batch_no, batch_type) curcropped = np.copy(curbatch) curbatch[:, :, 16:48, 16:48] = curcrop #I.plotandcompare(M.valid_recon[image_no], curbatch[image_no]) for i in range(len(reconstructions)): reconstructions[i] = reconstructions[i].transpose(1, 2, 0) original = curbatch[image_no].transpose(1, 2, 0) challenge = curcropped[image_no].transpose(1, 2, 0) fig = plt.figure(figsize=(17, 3)) ax1 = fig.add_subplot(171) ax1.imshow(challenge)
def DisplayImg(img, m, n): pic = Img(img, m, n) pic.display()
def __init__(self,root): filename = filedialog.askopenfilename(initialdir="/", title="Selectionnez une image", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))) Im = Img(filename) trait = tk.Toplevel(root) trait.title("Traitements d'images") trait.geometry("%dx%d" % (trait.winfo_screenwidth(), trait.winfo_screenheight())) ## Déclaration des éléments graphique de type menu menubar = Menu(trait) trait['menu'] = menubar Fichier = Menu(menubar) Traitement = Menu(menubar) Couleur = Menu(menubar) Texture = Menu(menubar) Contour = Menu(menubar) Aide = Menu(menubar) Change_palette = Menu(Couleur) Change_color = Menu(Couleur) can2 = Canvas(trait, bg="white") can2.pack(side=tk.TOP, fill=BOTH, expand=True) can2.image = Im.getImagetk() can2.create_image(trait.winfo_screenwidth()/4, 0, anchor=NW, image=Im.getImagetk()) ## Ajout d'un onglet Fichier menubar.add_cascade(label="Fichier", menu=Fichier) Fichier.add_command(label="Enregistrer_sous", command=exit) Fichier.add_command(label="Quitter", command=exit) ## Ajout d'un onglet Traitement menubar.add_cascade(label="Traitement", menu=Traitement) Traitement.add_command(label="Egalisation d'histogramme", command=Im.egalisation) Traitement.add_command(label="Reconaissance de lettres", command=exit) Traitement.add_command(label="Segmentation", command=Im.segmentation) ## Ajout d'un onglet Couleur menubar.add_cascade(label="Couleur", menu=Couleur) Couleur.add_command(label="Negatif", command=Im.negatif) Couleur.add_command(label="Niveau de gris", command=Im.niveauGris) Couleur.add_cascade(label="Changement de palette", menu=Change_palette) Change_palette.add_command(label="Automne", command=Im.autumn) Change_palette.add_command(label="Arc en ciel", command=Im.rainbow) Change_palette.add_command(label="Bone", command=Im.bone) Change_palette.add_command(label="Cool", command=Im.cool) Change_palette.add_command(label="Hot", command=Im.hot) Change_palette.add_command(label="HSV", command=Im.hsv) Change_palette.add_command(label="Jet", command=Im.jet) Change_palette.add_command(label="Ocean", command=Im.ocean) Change_palette.add_command(label="Pink", command=Im.pink) Change_palette.add_command(label="Printemps", command=Im.spring) Change_palette.add_command(label="Ete", command=Im.summer) Change_palette.add_command(label="Hiver", command=Im.winter) ## Ajout d'un onglet Texture menubar.add_cascade(label="Texture", menu=Texture) Texture.add_command(label="Camouflage", command=Im.camo) ## Ajout d'un onglet Contours menubar.add_cascade(label="Contours", menu=Contour) Contour.add_command(label="Extraction", command=Im.extraction) Contour.add_command(label="Detection", command=Im.detection) ## Ajout d'un onglet A propos de menubar.add_cascade(label="?", menu=Aide) Aide.add_command(label="A propos de ...", command=exit) trait.transient(root) trait.mainloop()