Skip to content

Instantly share code, notes, and snippets.

@jyoshida-sci
Last active December 6, 2016 03:15
Show Gist options
  • Save jyoshida-sci/baea2f6413f701b7dc5f to your computer and use it in GitHub Desktop.
Save jyoshida-sci/baea2f6413f701b7dc5f to your computer and use it in GitHub Desktop.
import cv2
import glob
import os
import struct
import numpy as np
import pyTS
import sys
########################
if __name__ == '__main__':
argvs = sys.argv
argc = len(argvs)
if (argc != 2):
print 'Usage: >python %s directory' % argvs[0]
quit()
fout = open("{0}.txt".format(argvs[1]), 'w')
files = glob.glob(".\\{0}\\*.dat".format(argvs[1]))
files.sort(key=os.path.getmtime)
for file in files:
print file
divpath=file.split("\\")
vxvybxby = divpath[-1].split("_")
vxvybxby[-1] = vxvybxby[-1][:-4]
imgs = []
f = open(file,'rb')
for i in range(16):
buf = f.read(440*512)
img = np.fromstring(buf, dtype=np.uint8)
img = img.reshape(440,512)
img = pyTS.dog(img)
img = pyTS.contrast(img)
ret,thre = cv2.threshold(img,50,1,cv2.THRESH_BINARY)
imgs.append(thre)
#cv2.imshow('img', thre*255)
#cv2.waitKey(0)
f.close()
params = {'ax':3, 'ay':3, 'thre':10}
rawmicros = pyTS.trackselect(imgs,params)
micros = pyTS.clustering(rawmicros)
for m in micros:
x = float(vxvybxby[0]) - (m['cx']-256)*(135.0/512.0)
y = float(vxvybxby[1]) + (m['cy']-220)*(115.0/440.0)
mystr = "{0:.1f} {1:.1f} {2:.1f} {3:.1f} ".format(m['ph'], m['pv'], m['ax'], m['ay'])
mystr += "{0:.1f} {1:.1f} {2} {3} {4:.1f} {5:.1f}\n".format(x, y, vxvybxby[0], vxvybxby[1], m['cx'], m['cy'])
print mystr
fout.write(mystr)
cv2.destroyAllWindows()
import cv2
import glob
import os
#import struct
import numpy as np
import itertools
def dog(img):
img= cv2.GaussianBlur(img, (3,3), 0)
gau= cv2.GaussianBlur(img, (31,31), 0)
sub = cv2.subtract(gau,img)
return sub
def contrast(img):
(minval,maxval,minloc,maxloc) = cv2.minMaxLoc(img)
if maxval-minval == 0:
return img
enh = np.zeros_like(img)
cv2.convertScaleAbs(img, enh, 255/(maxval-minval),-255*minval/(maxval-minval))
return enh
def trackselect(imgs, params):
rawmicros = []
#axr = 3 if params.get('ax','empty') == 'empty' else axr = int(params['ax'])
#ayr = 3 if params.get('ay','empty') == 'empty' else ayr = int(params['ay'])
#thre = 9 if params.get('thre','empty') == 'empty' else thre = int(params['thre'])
axr = int(params['ax'])
ayr = int(params['ay'])
thre = int(params['thre'])
#for ax,ay in itertools.product(range(-3,4), range(-3,4)):
for ax,ay in itertools.product(range(-axr,axr+1), range(-ayr,ayr+1)):
big_img = np.zeros((800,800),np.uint8)
#print "{0} {1}".format(ax, ay)
for p in range(len(imgs)):
big_h, big_w = big_img.shape
img_h, img_w = imgs[p].shape
sx = big_w/2 - img_w/2 + int(p*ax*4.0/16.0)
sy = big_h/2 - img_h/2 + int(p*ay*4.0/16.0)
#print "{0} {1} {2} {3} {4}".format(ax,ay,p,sx,sy)
big_img[sy:sy+img_h, sx:sx+img_w] += imgs[p]
ret,big_img = cv2.threshold(big_img, thre, 255, cv2.THRESH_TOZERO)
#cv2.imshow('big_img', big_img*30)
#cv2.imwrite("{0}_{1}.png".format(ax,ay), big_img*30)
#cv2.waitKey(0)
big_img_copy = big_img.copy()
contours,hierarchy = cv2.findContours(big_img_copy, 1, 2)
for cnt in contours:
mask = np.zeros(big_img.shape, np.uint8)
cv2.drawContours(mask,[cnt],0,255,-1)
masked = cv2.bitwise_and(big_img, mask)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(masked)
rawmicro = {'ph':int(max_val),'pv':masked.sum(),'ax':ax, 'ay':ay\
,'cx':max_loc[0] - (big_w/2 - img_w/2),'cy':max_loc[1] - (big_h/2 - img_h/2), 'flag':False}
rawmicros.append(rawmicro)
return rawmicros
def clustering(rawmicros):
micros = []
srawmicros = sorted(rawmicros, key=lambda member: member['pv'], reverse=True)
for i in range(len(srawmicros)):
a = srawmicros[i]
if a['flag']==True:
continue
srawmicros[i]['flag']=True
micro = {'ph':a['ph']*a['pv']*1.0,'pv':a['pv']*a['pv']*1.0,\
'ax':a['ax']*a['pv']*1.0, 'ay':a['ay']*a['pv']*1.0,\
'cx':a['cx']*a['pv']*1.0, 'cy':a['cy']*a['pv']*1.0, 'n':1}
pvsum=a['pv']*1.0
for j in range(i+1,len(srawmicros)):
b = srawmicros[j]
if b['flag']==True:
continue
if abs(a['cx']-b['cx'])>3 or abs(a['cy']-b['cy'])>3:
continue
if abs(a['ax']-b['ax'])>2 or abs(a['ay']-b['ay'])>2:
continue
micro['ph'] += b['ph']*b['pv']*1.0
micro['pv'] += b['pv']*b['pv']*1.0
micro['ax'] += b['ax']*b['pv']*1.0
micro['ay'] += b['ay']*b['pv']*1.0
micro['cx'] += b['cx']*b['pv']*1.0
micro['cy'] += b['cy']*b['pv']*1.0
micro['n'] += 1
pvsum += b['pv']*1.0
srawmicros[j]['flag']=True
micro['ph'] /= pvsum
micro['pv'] /= pvsum
micro['ax'] /= pvsum
micro['ay'] /= pvsum
micro['cx'] /= pvsum
micro['cy'] /= pvsum
micros.append(micro)
return micros
########################
if __name__ == '__main__':
fout = open('tracks_beam_s.txt', 'w')
files = glob.glob(".\\beam_s\\*.png")
files.sort(key=os.path.basename)
imgs = []
for file in files:
print file
divpath=file.split("\\")
xyz = divpath[-1].split("_")
layer = int(xyz[1][:-4])
if layer > 15 :
continue
if layer==0 :
del imgs[:]
img = cv2.imread(file,cv2.CV_LOAD_IMAGE_GRAYSCALE)
ret,thre = cv2.threshold(img,100,1,cv2.THRESH_BINARY)
imgs.append(thre)
#cv2.imshow('thre', thre*255)
#cv2.waitKey(0)
if layer == 15:
params = {'ax':5, 'ay':5, 'thre':9}
rawmicros = trackselect(imgs, params)
for rm in rawmicros:
mystr= "{0} {1} {2} {3} {4:.1f} {5:.1f}\n".format(rm['ph'], rm['pv'], rm['ax'], rm['ay'], rm['cx'], rm['cy'])
print mystr
fout.write(mystr)
#micros = clustering(rawmicros)
#print rawmicros
#print micros
fout.close()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment