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 f2(file1, file2): from scipy.fftpack import fftshift (imgarr1,w,h) = pgm.pgmread(file1) (imgarr2,w,h) = pgm.pgmread(file2) imgarr1 = np.float32(imgarr1) imgarr2 = np.float32(imgarr2) print imgarr1.shape, imgarr2.shape # dftArr1 = dft_2d_from_1d(imgarr1) dftArr2 = dft_2d_from_1d(imgarr2) # absArr1 = np.abs(dftArr1) absArr2 = np.abs(dftArr2) phaseArr1 = np.angle(dftArr1) phaseArr2 = np.angle(dftArr2) # newArr1 = absArr1 * np.exp(1j * phaseArr2) newArr2 = absArr2 * np.exp(1j * phaseArr1) #print 'Error : ', np.sum((newArr1 - dftArr1)**2) #print 'Error : ', np.sum((newArr2 - dftArr2)**2) # idftArr1 = idft_2d_from_1d(newArr1) idftArr2 = idft_2d_from_1d(newArr2) idftArr1 = np.round(idftArr1.real) idftArr2 = np.round(idftArr2.real) # dftArrAbs1 = np.abs(fftshift(dftArr1)) dftArrAbs2 = np.abs(fftshift(dftArr2)) # plt.figure() plt.subplot(121) plt.title(file1) plt.imshow(imgarr1, cmap=cm.gray) #plt.colorbar() plt.subplot(122) plt.title(file2) plt.imshow(imgarr2, cmap=cm.gray) #plt.colorbar() # plt.figure() plt.subplot(121) plt.title('DFT of ' + file1) plt.imshow(np.int32(np.log(dftArrAbs1)), cmap=cm.gray) plt.colorbar() plt.subplot(122) plt.title('DFT of ' + file2) plt.imshow(np.int32(np.log(dftArrAbs2)), cmap=cm.gray) plt.colorbar() # plt.figure() plt.subplot(121) plt.title('IDFT1') plt.imshow(np.int32(idftArr1), cmap=cm.gray) #plt.colorbar() plt.subplot(122) plt.title('IDFT2') plt.imshow(np.int32(idftArr2), cmap=cm.gray)
def f2(file1, file2): from scipy.fftpack import fftshift (imgarr1, w, h) = pgm.pgmread(file1) (imgarr2, w, h) = pgm.pgmread(file2) imgarr1 = np.float32(imgarr1) imgarr2 = np.float32(imgarr2) print imgarr1.shape, imgarr2.shape # dftArr1 = dft_2d_from_1d(imgarr1) dftArr2 = dft_2d_from_1d(imgarr2) # absArr1 = np.abs(dftArr1) absArr2 = np.abs(dftArr2) phaseArr1 = np.angle(dftArr1) phaseArr2 = np.angle(dftArr2) # newArr1 = absArr1 * np.exp(1j * phaseArr2) newArr2 = absArr2 * np.exp(1j * phaseArr1) #print 'Error : ', np.sum((newArr1 - dftArr1)**2) #print 'Error : ', np.sum((newArr2 - dftArr2)**2) # idftArr1 = idft_2d_from_1d(newArr1) idftArr2 = idft_2d_from_1d(newArr2) idftArr1 = np.round(idftArr1.real) idftArr2 = np.round(idftArr2.real) # dftArrAbs1 = np.abs(fftshift(dftArr1)) dftArrAbs2 = np.abs(fftshift(dftArr2)) # plt.figure() plt.subplot(121) plt.title(file1) plt.imshow(imgarr1, cmap=cm.gray) #plt.colorbar() plt.subplot(122) plt.title(file2) plt.imshow(imgarr2, cmap=cm.gray) #plt.colorbar() # plt.figure() plt.subplot(121) plt.title('DFT of ' + file1) plt.imshow(np.int32(np.log(dftArrAbs1)), cmap=cm.gray) plt.colorbar() plt.subplot(122) plt.title('DFT of ' + file2) plt.imshow(np.int32(np.log(dftArrAbs2)), cmap=cm.gray) plt.colorbar() # plt.figure() plt.subplot(121) plt.title('IDFT1') plt.imshow(np.int32(idftArr1), cmap=cm.gray) #plt.colorbar() plt.subplot(122) plt.title('IDFT2') plt.imshow(np.int32(idftArr2), cmap=cm.gray)
def f1(filename): from scipy.fftpack import fftshift (imgarr, w, h) = pgm.pgmread(filename) imgarr = np.float32(imgarr) # dftArr = dft_2d_from_1d(imgarr) # idftArr = idft_2d_from_1d(dftArr) idftArr = np.round(idftArr.real) print 'Error : ', np.sum((imgarr - idftArr)**2) # dftArrAbs = np.abs(fftshift(dftArr)) # plt.figure() plt.subplot(131) plt.title(filename) plt.imshow(imgarr, cmap=cm.gray) plt.colorbar() plt.subplot(132) plt.title('DFT of ' + filename) plt.imshow(np.int32(np.log(dftArrAbs)), cmap=cm.gray) plt.colorbar() plt.subplot(133) plt.title('IDFT') plt.imshow(np.int32(idftArr), cmap=cm.gray) plt.colorbar()
def f1(filename): from scipy.fftpack import fftshift (imgarr,w,h) = pgm.pgmread(filename) imgarr = np.float32(imgarr) # dftArr = dft_2d_from_1d(imgarr) # idftArr = idft_2d_from_1d(dftArr) idftArr = np.round(idftArr.real) print 'Error : ', np.sum((imgarr - idftArr)**2) # dftArrAbs = np.abs(fftshift(dftArr)) # plt.figure() plt.subplot(131) plt.title(filename) plt.imshow(imgarr, cmap=cm.gray) plt.colorbar() plt.subplot(132) plt.title('DFT of ' + filename) plt.imshow(np.int32(np.log(dftArrAbs)), cmap=cm.gray) plt.colorbar() plt.subplot(133) plt.title('IDFT') plt.imshow(np.int32(idftArr), cmap=cm.gray) plt.colorbar()
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 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(): (imgarr,w,h) = pgm.pgmread('lena.pgm') imgs = Image.Image(imgarr) imgt1 = imgs.translate(3.75,4.3) imgt2 = imgs.translate(3.75,4.3,'bilinear') plt.figure(1) plt.imshow(imgs.pix,cmap=cm.gray) plt.figure(2) plt.imshow(imgt1.pix,cmap=cm.gray) plt.figure(3) plt.imshow(imgt2.pix,cmap=cm.gray) plt.show()
def f1(): (imgarr, w, h) = pgm.pgmread('lena.pgm') imgs = Image.Image(imgarr) imgt1 = imgs.translate(3.75, 4.3) imgt2 = imgs.translate(3.75, 4.3, 'bilinear') plt.figure(1) plt.imshow(imgs.pix, cmap=cm.gray) plt.figure(2) plt.imshow(imgt1.pix, cmap=cm.gray) plt.figure(3) plt.imshow(imgt2.pix, cmap=cm.gray) plt.show()
def f2(): (imgarr,w,h) = pgm.pgmread('pisa.pgm') imgs = Image.Image(imgarr) imgt1 = imgs.rotate(2.5) imgt2 = imgt1.rotate(-2.5) imgt3 = imgs - imgt2 plt.figure(1) plt.imshow(imgs.pix,cmap=cm.gray) plt.figure(2) plt.imshow(imgt1.pix,cmap=cm.gray) plt.figure(3) plt.imshow(imgt2.pix,cmap=cm.gray) plt.figure(4) plt.imshow(imgt3.pix,cmap=cm.gray) plt.show()
def f2(): (imgarr, w, h) = pgm.pgmread('pisa.pgm') imgs = Image.Image(imgarr) imgt1 = imgs.rotate(2.5) imgt2 = imgt1.rotate(-2.5) imgt3 = imgs - imgt2 plt.figure(1) plt.imshow(imgs.pix, cmap=cm.gray) plt.figure(2) plt.imshow(imgt1.pix, cmap=cm.gray) plt.figure(3) plt.imshow(imgt2.pix, cmap=cm.gray) plt.figure(4) plt.imshow(imgt3.pix, cmap=cm.gray) plt.show()
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 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 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')
return imgarr def plot_hist(img, plotTitle): 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)
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') if __name__ == "__main__": img_original = pgmread('balloons.ascii.pgm') # deepcopy para evitar condição de corrida img_tratamento_1 = copy.deepcopy(img_original) img_tratamento_2 = copy.deepcopy(img_original) # criando threads t1 = threading.Thread(target=alargamento_contraste, name='alargamento', args=(img_tratamento_1, )) t2 = threading.Thread(target=equalizacao_histograma, name='equalização', args=(img_tratamento_2, )) # inicio das execuções t1.start() t2.start()
def read_pgm(filename, byteorder='>'): """Return image data from a raw PGM file as numpy array. Format specification: http://netpbm.sourceforge.net/doc/pgm.html """ with open(filename, 'rb') as f: buffer = f.read() try: header, width, height, maxval = re.search( b"(^P5\s(?:\s*#.*[\r\n])*" b"(\d+)\s(?:\s*#.*[\r\n])*" b"(\d+)\s(?:\s*#.*[\r\n])*" b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups() except AttributeError: raise ValueError("Not a raw PGM file: '%s'" % filename) return numpy.frombuffer( buffer, dtype='u1' if int(maxval) < 256 else byteorder + 'u2', count=int(width) * int(height), offset=len(header)).reshape((int(height), int(width))) if __name__ == "__main__": from matplotlib import pyplot image = read_pgm("seg_example_results.pgm", byteorder='<') pyplot.imshow(image, pyplot.cm.gray) pyplot.show() image1 = pgm.pgmread("seg_example_results.pgm") print(image1)
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_hist(img, plotTitle): 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)