1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| import numpy as np
import readmagick
import os
import pylab
user=os.path.expanduser("~")
workdir=os.path.join(user,"Applications","ImagesTest","MFISH")
blue="Aqua.tif"
green="Green.tif"
gold="Gold.tif"
red="Red.tif"
frd="FarRed.tif"
complete_path=os.path.join(workdir,blue)
i1=readmagick.readimg(complete_path)
#
complete_path=os.path.join(workdir,green)
i2=readmagick.readimg(complete_path)
#
complete_path=os.path.join(workdir,gold)
i3=readmagick.readimg(complete_path)
#
complete_path=os.path.join(workdir,red)
i4=readmagick.readimg(complete_path)
#
complete_path=os.path.join(workdir,frd)
i5=readmagick.readimg(complete_path)
#
shape=i5.shape
b=i1.flatten()
g=i2.flatten()
y=i3.flatten()
r=i4.flatten()
f=i5.flatten()
#make a 2D array
#each line is a pixel, column=channel
channel5=np.vstack((f,r,y,g,b))
c5=channel5.T
#print c5.shape
#try some coef
m=np.array([[0.80, 0.10, 0.01],
[0.10, 0.80, 0.04],
[0.10, 0.05, 0.10],
[0.00, 0.03, 0.15],
[0.00, 0.02, 0.70]])
print m.shape
#produce a flat rgb image
#combine linearly the five channels into three RGB channels
c3=np.dot(c5,m)
#isolate R,G,B component
c3_r=c3[:,0]
c3_g=c3[:,1]
c3_b=c3[:,2]
#Reconstitute original image size
#should be a red image of size =shape
R=c3_r.reshape(shape)
G=c3_g.reshape(shape)
B=c3_b.reshape(shape)
#Try to build a rgb image
rgb = np.zeros((shape[0],shape[1],3),dtype=float)
print rgb.shape
mxr=np.max(R)
mxg=np.max(G)
mxb=np.max(B)
#normalize to 0-255 uint8
Rnorm=np.uint8((255*(R/mxr)))
Gnorm=np.uint8((255*(R/mxg)))
Bnorm=np.uint8((255*(R/mxb)))
#copy each RGB component in an RGB array
rgb[:,:,0]=Rnorm
rgb[:,:,1]=Gnorm
rgb[:,:,2]=Bnorm
#complete_path=os.path.join(workdir,"R.png")
#readmagick.writeimg(R,complete_path)
pylab.gray()
#
pylab.subplot(221,frameon=False, xticks=[], yticks=[])
pylab.imshow(Rnorm)
#complete_path=os.path.join(workdir,"R.png")
#readmagick.writeimg(Rnorm,complete_path)
#
pylab.subplot(222,frameon=False, xticks=[], yticks=[])
pylab.imshow(Gnorm)
#complete_path=os.path.join(workdir,"G.png")
#readmagick.writeimg(Gnorm,complete_path)
#
pylab.subplot(223,frameon=False, xticks=[], yticks=[])
pylab.imshow(Bnorm)
#complete_path=os.path.join(workdir,"B.png")
#readmagick.writeimg(Bnorm,complete_path)
#pylab.clf()
pylab.subplot(224, aspect='equal',frameon=False, xticks=[], yticks=[])
pylab.imshow(rgb)
pylab.show() |
Partager