Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
first one
- Loading branch information
Malte Brammerloh
committed
Apr 25, 2017
0 parents
commit 15543ed
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) |