From 15543ede3546c7677d23697e2a1345ea883847d0 Mon Sep 17 00:00:00 2001 From: Malte Brammerloh Date: Tue, 25 Apr 2017 12:16:04 +0200 Subject: [PATCH] first one --- imageManipulation.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 imageManipulation.py diff --git a/imageManipulation.py b/imageManipulation.py new file mode 100644 index 0000000..ba1acc5 --- /dev/null +++ b/imageManipulation.py @@ -0,0 +1,47 @@ +__all__ = ['rgb2gray', 'rgb2mix','rgb2red','rgb2blue','plotHist','makeHist','myHist','saveNifti', 'hist2curve'] +import numpy as np +import nibabel as nib +import matplotlib.pyplot as plt +import os + + +def rgb2gray(rgb): # visual weight + r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2] + gray = 0.2989 * r + 0.5870 * g + 0.1140 * b + return gray + +def rgb2mix(rgb): + return 1/3. * (1.0 * rgb[:,:,0] + 1.0 * rgb[:,:,1] + 1.0 * rgb[:,:,2]) + +def rgb2red(rgb): + return rgb[:,:,0] + +def rgb2green(rgb): + return rgb[:,:,1] + +def rgb2blue(rgb): + return rgb[:,:,2] + +def plotHist(hist, bins): + width = 0.7 * (bins[1] - bins[0]) + center = (bins[:-1] + bins[1:]) / 2 + plt.bar(center, hist, align='center', width=width) + plt.show();plt.close() + +def makeHist(data): + mhist, mbins = np.histogram(data, bins = np.around(np.amax(data))) + plotHist(mhist, mbins) + +def myHist(data, nbins): + mhist, mbins = np.histogram(data, nbins) + plotHist(mhist, mbins) + +def saveNifti(data, name): + img = nib.Nifti1Image(data, np.eye(4)) + + nib.save(img, name)# + +def hist2curve(hist): # Takes a tuple as generated by pyplot's hist() + y,bins, delme = np.array(hist) + x = [(bins[i+1]+bins[i])/2 for i in range(bins.shape[0]-1)] + return np.asarray([x,y]) \ No newline at end of file