def equalizacao_histograma(imagem): print("Executando a thread: {}".format(threading.current_thread().name)) h = __calcular_histograma(imagem) p = __calcular_probabilidade_ocorrencia(h) q = __calcular_probabilidade_acumulada(p) nova_imagem = np.zeros((480, 640)) for x in range(len(imagem[0])): for y in range(len(imagem[0][x])): nova_imagem[x][y] = round(255 * q[imagem[0][x][y].astype(np.int)]) pgmwrite(nova_imagem, 'equalizacao_histograma.pgm')
def f1(imgs, sigmaD, sigmaR, filtType='bilateral'): import pgm imgt = imgs.bilateral_filter(sigmaD, sigmaR, filtType) plotTitle = caps(filtType) + ' filter with sigmaD=' + str(sigmaD) +\ ' and sigmaR=' + str(sigmaR) plot_fig(imgt.pix, plotTitle) # plot_hist(imgt, plotTitle) # print np.sum((imgt.pix - imgs.pix)**2) filename = 'academy_' + filtType + '_' + str(sigmaD) + '_' +\ str(sigmaR) + '.pgm' pgm.pgmwrite(np.int32(imgt.pix), filename) print '.'
def f1(imgs, sigmaD, sigmaR, filtType='bilateral'): import pgm imgt = imgs.bilateral_filter(sigmaD, sigmaR, filtType) plotTitle = caps(filtType) + ' filter with sigmaD=' + str(sigmaD) +\ ' and sigmaR=' + str(sigmaR) plot_fig(imgt.pix, plotTitle) # plot_hist(imgt, plotTitle) # print np.sum((imgt.pix - imgs.pix)**2) filename = 'academy_' + filtType + '_' + str(sigmaD) + '_' +\ str(sigmaR) + '.pgm' pgm.pgmwrite(np.int32(imgt.pix), filename) print '.'
def alargamento_contraste(imagem): print("Executando a thread: {}".format(threading.current_thread().name)) nova_imagem = np.zeros((480, 640)) imax = __extrair_imax(imagem) imin = __extrair_imin(imagem) for x in range(len(imagem[0])): for y in range(len(imagem[0][x])): processamento = (255 / (imax - imin)) * (imagem[0][x][y].astype(np.int) - imin) nova_imagem[x][y] = round(processamento) pgmwrite(nova_imagem, 'alargamento_final.pgm')
def f2(sigma): import pgm (imgarray,w,h) = pgm.pgmread('cameraman.pgm') imgs = Image.Image(imgarray) suppMat, imgt = imgs.lsv_blur(sigma) if show_plot == True: plt.figure() plt.imshow(suppMat) plt.title('Kernel size variation') plt.colorbar() plt.figure() plt.imshow(imgs.pix, cmap=cm.gray) plt.title('Original image') plt.figure() plt.imshow(imgt.pix, cmap=cm.gray) plt.title('Space variant blur') pgm.pgmwrite(imgt.pix, 'a3_lsv_blur.pgm') # imgt1 = f3(imgs) print 'Kernel size: ', suppMat[0][0] print 'Error : ', np.sum((imgt1.pix - imgt.pix)**2) / 256
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')
outImage[i:i + Lw, j:j + Lw] = (flatwindow[0:Lw, 0:Lw] + outImage[i:i + Lw, j - 1:j + Lw - 1]) / 2.0 #outImage[i:i+Lw,j:j+Lw] = (flatwindow[0:Lw,0:Lw] + np.mean(inImage[i:i+Lw,j+Lw]) + outImage[i:i+Lw,j-1:j+Lw-1]) /2.0 outImage[i:i + Lw, j + Lw] = (flatwindow[:, Lw - 1]) if j == 0: outImage[i:i + Lw, j:j + Lw] = flatwindow[ 0:Lw, 0:Lw] ##+ np.mean(inImage[i:i+Lw,j+Lw]) #outImage[i:i+Lw,j+Lw] = outImage[i:i+Lw,j+Lw] + np.mean(inImage[i:i+Lw,j+Lw]) #outImage[i:i+Lw,j+Lw] = (outImage[i:i+Lw,j+Lw] + inImage[i:i+Lw,j+Lw])/2.0 #outImage[i:i+Lw,j:j+Lw] = np.reshape(flatwindow+np.mean(flatwindow),(Lw, Lw)) eprint(i) #outImage = (outImage + inImage)/2.0 #outImage = (255*preprocessing.normalize(outImage)).astype(np.int); print(outImage) outImage = 255 * (outImage - np.min(outImage)) / (np.max(outImage) - np.min(outImage)) return outImage #inImage = pgmread("mri_NOISE.pgm") #inImage = pgmread("glassware_NOISE.pgm") inImage = pgmread("ardilla_NOISE.pgm") inImagef = inImage[0].astype(np.float) #inImagef = inImagef[100:200,100:200] Nw = 5 #Dictionary size.. Lw = 2 #width size of each window #D = buildDictionary(inImagef, Nw, Lw) outImage = processImage(inImagef, Nw, Lw) pgmwrite(outImage, "out.pgm")
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) f1(imgs, 3, 50) # f1(imgs, 3, 100) # f1(imgs, 3, 300) ## # f1(imgs, 5, 1) # f1(imgs, 5, 10)
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) f1(imgs, 3, 50) # f1(imgs, 3, 100) # f1(imgs, 3, 300) ## # f1(imgs, 5, 1) # f1(imgs, 5, 10)