In [7]:
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 3126 17:50:00 20167
pyNVscan Advanced Tools by Scipy for hot spot
@author: listen
"""
import matplotlib
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import skimage.morphology as morph
import skimage.exposure as skie
PZT_LIMIT_X = 100.0
PZT_LIMIT_Y = 100.0
def nothing(x):
pass
def read_CSV_Head(csv_head):
x0 = csv_head[0]
y0 = csv_head[1]
x1 = csv_head[2]
y1 = csv_head[3]
step_move = csv_head[4]
return x0, y0, x1, y1, step_move
#由区域扫描结果Data.csv,创建head
def creat_CSV_Head_File(my_matrix):
x0 = np.min(my_matrix[:,0])
x1 = np.max(my_matrix[:,0])
y0 = np.min(my_matrix[:,1])
y1 = np.max(my_matrix[:,1])
step_move = my_matrix[1,0] - my_matrix[0,0]
dx = x1 - x0
dy = y1 - y0
lx = np.size(my_matrix[:,0])
ldata = np.size(my_matrix[:,2])
if abs(lx-(dx/step_move+1.0)*(dy/step_move+1.0)) >= 0.01:
print "Error: creat large_scan_head.csv"
sys.exit(255)
elif x0<0 or y0<0 or x1>PZT_LIMIT_X or y1>PZT_LIMIT_Y:
print "Error: PZT XY LIMITs"
sys.exit(255)
elif x0>=x1 or y0>=y1:
print "Error: creat Large Scan"
sys.exit(255)
head = [x0, y0, x1, y1, step_move, lx, ldata]
return head
#最大公约数
def gcd(a, b):
if a < b:
a, b = b, a
while b != 0:
temp = a % b
a = b
b = temp
return a
#由csv绘制纯图像,返回比例尺值
def csv_to_PNG(my_matrix, csv_head, mode, contour_i):
# print csv_head
x0,y0,x1,y1,step_move = read_CSV_Head(csv_head)
# Becouse of numpy arange()
x2 = x1 + step_move * 0.5
y2 = y1 + step_move * 0.5
size_xy = int(csv_head[5])
progress_len = int(csv_head[6])
wave_data = my_matrix[:,2]
w_data = np.zeros(size_xy) # ready for one-photon_count
#progress_len
w_data[0:progress_len] = wave_data[0:progress_len] # Warning: NOT progress_len-1
X = np.arange(x0, x2, step_move)
Y = np.arange(y0, y2, step_move)
len_X = np.size(X)
len_Y = np.size(Y)
Z0 = w_data.reshape(-1,len_X)
Z0[1::2,:] = Z0[1::2,::-1]
#创建浮点灰度图
img = Z0 / Z0.max()
#圈定坐标和筛选
lm = morph.local_maxima(img)
xc1, yc1 = np.where(lm.T == True)
v = img[(yc1, xc1)]
lim = 0.5
xc2, yc2 = xc1[v > lim], yc1[v > lim]
extent = np.array([x0, x1, y0, y1]) + np.array([-step_move,step_move,-step_move,step_move])*0.5
#自适应去除matplotlib figure tight_layout PAD,放大到6*100像素的宽
fd = gcd(len_X,len_Y)
fx = len_X / fd
fy = len_Y / fd
if fx>(fy*0.5) and fx<=3:
fd = 6/fx
fx = 6
fy = fd*fy
fig = plt.figure(figsize=(fx,fy))
axes = plt.subplot(111)
axes.set_xlim(0, img.shape[1])
axes.set_ylim(0, img.shape[0])
axes.scatter(xc2, yc2, s=80, facecolor='none', edgecolor='#FF7400')
# axes.imshow(Z0, extent=extent, origin="lower",interpolation='nearest')
# imshow, contour mode,
#0 + -
#1 + +
#2 - +
if mode < 2 :
axes.imshow(img , cmap='gray',origin="lower")
if mode > 0 :
axes.contour(X, Y, img , contour_i, cmap='cool')
#for OpenCV ,NO AXEX,NO PAD
axes.set_xticks([])
axes.set_yticks([])
axes.spines['right'].set_color('none')
axes.spines['top'].set_color('none')
axes.spines['bottom'].set_color('none')
axes.spines['left'].set_color('none')
plt.tight_layout(pad=0)
#png_ruler = (x1 - x0 + step_move) / (fx * 100.0) # 保存图像的比例尺:1个像素代表的微米
fig.show()
#图片像素坐标[左上角(0,0)],变换到PZT坐标(微米)[左下角(0,0)] ,(dx,dy)偏移比例
def pngXY_to_pztXY(csv_head, gray, png_x, png_y):
x0,y0,x1,y1,step_move = read_CSV_Head(csv_head)
Lx,Ly = gray.shape
# pzt_x = png_x / Lx * (x1 - x0 + step_move) + x0 - step_move*0.5
# pzt_y = (Ly - png_y) / Ly * (y1 - y0 + step_move) + y0 - step_move*0.5
pzt_x = png_x / (Lx - 1.0) * (x1 - x0 + step_move) + x0 - step_move*0.5
pzt_y = (Ly - png_y) / (Ly - 1.0) * (y1 - y0 + step_move) + y0 - step_move*0.5
if pzt_x<0:
pzt_x = 0;
elif pzt_x>PZT_LIMIT_X:
pzt_x =PZT_LIMIT_X
elif pzt_y<0:
pzt_y = 0;
elif pzt_y>PZT_LIMIT_Y:
pzt_y = PZT_LIMIT_Y
return pzt_x, pzt_y
#PZT坐标(微米)变换到large_scan.png图片像素坐标
def pztXY_to_pngXY(csv_head, gray, pzt_x, pzt_y):
x0,y0,x1,y1,step_move = read_CSV_Head(csv_head)
Lx,Ly = gray.shape
# png_x = (pzt_x - x0 + step_move*0.5) / (x1 - x0 + step_move) * Lx
# png_y = Ly - (pzt_y - y0 + step_move*0.5) / (y1 - y0 + step_move) * Ly
png_x = (pzt_x - x0 + step_move*0.5) / (x1 - x0 + step_move) * (Lx - 1.0)
png_y = Ly - (pzt_y - y0 + step_move*0.5) / (y1 - y0 + step_move) * (Ly - 1.0)
# png_x = np.uint16(round(png_x))
# png_y = np.uint16(round(png_y))
return png_x, png_y
In [8]:
def main_pic(mode = 1):
contour_i = 10
csv_filepath_large_data = 'Z39.00um_dX0.20um_CT80ms10ms.csv'
with open(csv_filepath_large_data,"rb") as f:
my_matrix = np.loadtxt(f, delimiter=",", skiprows=0)
csv_head_large = creat_CSV_Head_File(my_matrix)
csv_to_PNG(my_matrix, csv_head_large, mode, contour_i)
#return gray
In [9]:
main_pic(0)
In [ ]: