Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
image-manipulation/imageManipulation.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47 lines (35 sloc)
1.25 KB
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
__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]) |