Skip to content

Commit

Permalink
bringing git up to date with what is on stanford's copy
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollman committed Dec 17, 2014
1 parent 06cf793 commit 32597e8
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 87 deletions.
39 changes: 36 additions & 3 deletions ImageCollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import matplotlib.pyplot as plt
import os
from PIL import Image
from libtiff import TIFFimage
import time
import json
from Rectangle import Rectangle
import traceback,sys
#from imageSourceMM import imageSource


Expand Down Expand Up @@ -89,8 +91,11 @@ def file_has_metadata(self,path):
return 'txt' in theext

def saveData(self,data):
img = Image.fromarray(data)
img.save(self.imagePath)
#img = Image.fromarray(data)
#img.save(self.imagePath)
tiff = TIFFimage(data, description='')
tiff.write_file(self.imagePath, compression='none')
del tiff

def contains_rect(self,box):
return self.boundBox.contains_rect(box)
Expand All @@ -100,6 +105,7 @@ def contains_point(self,x,y):
def getData(self):
img=Image.open(self.imagePath,mode='r')
thedata=img.getdata()

(width,height)=img.size
data=np.reshape(np.array(thedata,np.dtype('uint8')),(height,width))
return data
Expand All @@ -120,10 +126,28 @@ def __init__(self,rootpath,imageClass=MyImage,imageSource=None,axis=None):
self.matplot_images=[]
self.minvalue=0
self.maxvalue=512


def display8bit(self,image, display_min, display_max):
image = np.array(image, copy=True)
image.clip(display_min, display_max, out=image)
image -= display_min
image //= (display_max - display_min + 1) / 256.
return image.astype(np.uint8)

def lut_convert16as8bit(self,image, display_min, display_max) :
lut = np.arange(2**16, dtype='uint16')
lut = self.display8bit(lut, display_min, display_max)
return np.take(lut, image)



def get_pixel_size(self):
return self.imageSource.get_pixel_size()

def get_image_size_um(self):
(fw,fh)=self.imageSource.get_frame_size_um()
return (fw,fh)

def set_view_home(self,box=None):
if box==None:
self.axis.set_xlim(left=self.bigBox.left,right=self.bigBox.right)
Expand Down Expand Up @@ -168,9 +192,15 @@ def add_image_at(self,x,y):
#go get an image at x,y
try:
(thedata,bbox)=self.imageSource.take_image(x,y)
if thedata.dtype == np.uint16:
print "converting"
maxval=self.imageSource.get_max_pixel_value()
thedata=self.lut_convert16as8bit(thedata,0,maxval)

except:
#todo handle this better
print "ahh no! imageSource failed us"
traceback.print_exc(file=sys.stdout)
print "hmm.. bbox is"
bbox.printRect()
print "x,y is",x,y
Expand Down Expand Up @@ -263,6 +293,9 @@ def printBoundBoxes(self):
def loadImageCollection(self):
#list all metadata files in rootdir
testimage=self.imageClass()
if not os.path.isdir(self.rootpath):
os.makedirs(self.rootpath)

metafiles=[os.path.join(self.rootpath,f) for f in os.listdir(self.rootpath) if f.endswith('.txt') ]

print "loading metadata"
Expand Down
26 changes: 14 additions & 12 deletions MosaicImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(self,axis,one_axis,two_axis,corr_axis,imgSrc,rootPath):
self.oneImage=None
self.twoImage=None
self.corrImage=None

self.imgSrc = imgSrc
self.imgCollection=ImageCollection(rootpath=rootPath,imageSource=imgSrc,axis=self.axis)

(x,y)=imgSrc.get_xy()
Expand All @@ -141,9 +141,8 @@ def __init__(self,axis,one_axis,two_axis,corr_axis,imgSrc,rootPath):
self.imgCollection.loadImageCollection()


self.maxvalue=255
imgSrc.set_channel('Violet')
imgSrc.set_exposure(250)
self.maxvalue=512


self.axis.set_title('Mosaic Image')

Expand Down Expand Up @@ -490,8 +489,11 @@ def align_by_sift(self,xy1,xy2,window=70):

#one_cuta=np.minimum(one_cut*256.0/self.maxvalue,255.0).astype(np.uint8)
#two_cuta=np.minimum(two_cut*256.0/self.maxvalue,255.0).astype(np.uint8)
one_cuta=cv2.equalizeHist(one_cut)
two_cuta=cv2.equalizeHist(two_cut)
one_cuta = np.copy(one_cut)
two_cuta = np.copy(two_cut)

one_cuta=cv2.equalizeHist(one_cuta)
two_cuta=cv2.equalizeHist(two_cuta)


sift = cv2.SIFT(nfeatures=500,contrastThreshold=.2)
Expand All @@ -502,8 +504,8 @@ def align_by_sift(self,xy1,xy2,window=70):



img_one = cv2.drawKeypoints(one_cuta,kp1)
img_two = cv2.drawKeypoints(two_cuta,kp2)
#img_one = cv2.drawKeypoints(one_cut,kp1)
#img_two = cv2.drawKeypoints(two_cut,kp2)


#image2=self.two_axis.imshow(img_two)
Expand Down Expand Up @@ -597,13 +599,13 @@ def align_by_sift(self,xy1,xy2,window=70):
print (dx_um,dy_um)


self.paintImageOne(img_one,xy=xy1)
self.paintImageTwo(img_two,xy=xy2,xyp=(x2-dx_um,y2-dy_um))
self.paintImageOne(one_cut,xy=xy1)
self.paintImageTwo(two_cut,xy=xy2,xyp=(x2-dx_um,y2-dy_um))

return ((dx_um,dy_um),len(bestInlierIdx))
else:
self.paintImageOne(img_one,xy=xy1)
self.paintImageTwo(img_two,xy=xy2)
self.paintImageOne(one_cut,xy=xy1)
self.paintImageTwo(two_cut,xy=xy2)

return ((0.0,0.0),0)

Expand Down
Loading

0 comments on commit 32597e8

Please sign in to comment.