Skip to content

Commit

Permalink
myvi update
Browse files Browse the repository at this point in the history
  • Loading branch information
yxdragon committed Mar 11, 2019
1 parent c4eefb8 commit aadbd6d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
38 changes: 36 additions & 2 deletions imagepy/core/myvi/canvas3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __init__(self, parent, manager=None):
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnMouseDown)
self.Bind(wx.EVT_RIGHT_UP, self.OnMouseUp)
self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
self.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheel)
self.lastx, self.lasty = None, None
Expand Down Expand Up @@ -73,12 +75,24 @@ def OnMouseMotion(self, evt):
x, y = evt.GetPosition()
dx, dy = x-self.lastx, y-self.lasty
self.lastx, self.lasty = x, y
#self.manager.h -= dx/200
angx = self.manager.angx - dx/200
angy = self.manager.angy + dy/200
#print('ang', angx, angy)
self.manager.set_pers(angx=angx, angy=angy)
self.Refresh(False)
if evt.Dragging() and evt.RightIsDown():
light = self.manager.light
x, y = evt.GetPosition()
dx, dy = x-self.lastx, y-self.lasty
self.lastx, self.lasty = x, y
angx, angy = dx/200, dy/200
vx, vy, vz = self.manager.light
ay = math.asin(vz/math.sqrt(vx**2+vy**2+vz**2))-angy
xx = math.cos(angx)*vx - math.sin(angx)*vy
yy = math.sin(angx)*vx + math.cos(angx)*vy
ay = max(min(math.pi/2-1e-4, ay), -math.pi/2+1e-4)
zz, k = math.sin(ay), math.cos(ay)/math.sqrt(vx**2+vy**2)
self.manager.set_light((xx*k, yy*k, zz))
self.Refresh(False)

def save_bitmap(self, path):
context = wx.ClientDC( self )
Expand Down Expand Up @@ -142,6 +156,14 @@ def __init__( self, parent, manager=None):
#pan = wx.Panel(self.toolbar, size=(50, 50))
self.btn_color = wx.ColourPickerCtrl( self.toolbar, wx.ID_ANY, wx.Colour( 128, 128, 128 ), wx.DefaultPosition, [(33, 38), (-1, -1)][platform.system() in ['Windows', 'Linux']], wx.CLRP_DEFAULT_STYLE )
tsizer.Add( self.btn_color, 0, wx.ALIGN_CENTER|wx.ALL|(0, wx.EXPAND)[platform.system() in ['Windows', 'Linux']], 0 )
tsizer.Add(wx.StaticLine( self.toolbar, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_VERTICAL), 0, wx.ALL|wx.EXPAND, 2 )
self.cho_light = wx.Choice( self.toolbar, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, ['force light', 'normal light', 'weak light', 'off light'], 0 )
self.cho_light.SetSelection( 1 )
tsizer.Add( self.cho_light, 0, wx.ALIGN_CENTER|wx.ALL, 1 )
self.cho_bg = wx.Choice( self.toolbar, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, ['force scatter', 'normal scatter', 'weak scatter', 'off scatter'], 0 )
self.cho_bg.SetSelection( 1 )
tsizer.Add( self.cho_bg, 0, wx.ALIGN_CENTER|wx.ALL, 1 )

self.toolbar.SetSizer( tsizer )
tsizer.Layout()

Expand Down Expand Up @@ -199,6 +221,8 @@ def __init__( self, parent, manager=None):

self.cho_obj.Bind( wx.EVT_CHOICE, self.on_select )
self.cho_mode.Bind( wx.EVT_CHOICE, self.on_mode )
self.cho_light.Bind( wx.EVT_CHOICE, self.on_light )
self.cho_bg.Bind( wx.EVT_CHOICE, self.on_bg )
self.chk_visible.Bind( wx.EVT_CHECKBOX, self.on_visible)
self.sli_blend.Bind( wx.EVT_SCROLL, self.on_blend )
self.col_color.Bind( wx.EVT_COLOURPICKER_CHANGED, self.on_color )
Expand Down Expand Up @@ -229,6 +253,16 @@ def on_bgcolor(self, event):
self.canvas.manager.set_background(c)
self.canvas.Refresh(False)

def on_bg(self, event):
scatter = 3 - self.cho_bg.GetSelection()
self.canvas.manager.set_bright_scatter(scatter=scatter/3)
self.canvas.Refresh(False)

def on_light(self, event):
bright = 3 - self.cho_light.GetSelection()
self.canvas.manager.set_bright_scatter(bright=bright/3)
self.canvas.Refresh(False)

def on_save(self, evt):
dic = {'open':wx.FD_OPEN, 'save':wx.FD_SAVE}
filt = 'PNG files (*.png)|*.png'
Expand Down
29 changes: 20 additions & 9 deletions imagepy/core/myvi/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ def set_style(self, mode=None, blend=None, color=None, visible=None):
self.vbo.write(self.buf.tobytes())
self.color = color if isinstance(color, tuple) else (0,0,0)

def draw(self, mvp):
def draw(self, mvp, light, bright, scatter):
if not self.visible: return
self.ctx.line_width = self.width
mvp = np.dot(*mvp)
self.prog['Mvp'].write(mvp.astype(np.float32).tobytes())
self.prog['blend'].value = self.blend

self.prog['scatter'].value = scatter
self.prog['light'].value = tuple(light)
self.prog['bright'].value = bright
self.vao.render({'mesh':moderngl.TRIANGLES, 'grid':moderngl.LINES}[self.mode])

class MarkText:
Expand All @@ -114,7 +116,7 @@ def set_style(self, mode=None, blend=None, color=None, visible=None):
if not visible is None: self.visible = visible
if not color is None: self.color = color

def draw(self, mvp):
def draw(self, mvp, light, bright, scatter):
if not self.visible: return
self.ctx.line_width = 2
self.prog['mv'].write(mvp[0].astype(np.float32).tobytes())
Expand All @@ -129,6 +131,8 @@ def __init__(self):
self.ratio, self.dial = 1.0, 1.0
self.pers, self.center = True, (0,0,0)
self.background = 0.4, 0.4, 0.4
self.light = (1,0,0)
self.bright, self.scatter = 0.66, 0.66
self.objs = {}
self.ctx = None

Expand All @@ -151,13 +155,15 @@ def on_ctx(self):
''',
fragment_shader='''
#version 330
uniform vec3 light = vec3(1,1,0.8);
uniform float blend = 0.1;
uniform vec3 light;
uniform float blend;
uniform float scatter;
uniform float bright;
in vec3 f_norm;
in vec3 f_color;
out vec4 color;
void main() {
float d = clamp((dot(light, f_norm)+1)*0.5, 0, 1);
float d = clamp(dot(light, f_norm)*bright+scatter, 0, 1);
color = vec4(f_color*d, blend);
}
'''
Expand Down Expand Up @@ -215,7 +221,7 @@ def draw(self):
self.ctx.enable(moderngl.DEPTH_TEST)
#self.ctx.enable(ModernGL.CULL_FACE)
self.ctx.enable(moderngl.BLEND)
for i in self.objs.values(): i.draw(self.mvp)
for i in self.objs.values(): i.draw(self.mvp, self.light, self.bright, self.scatter)

def count_box(self):
minb = np.array([i.box[0] for i in self.objs.values() if not i.box is None]).min(axis=0)
Expand All @@ -237,8 +243,13 @@ def set_viewport(self, x, y, width, height):
self.ctx.viewport = (x, y, width, height)
self.ratio = width*1.0/height

def set_background(self, rgb):
self.background = rgb
def set_background(self, rgb): self.background = rgb

def set_light(self, light): self.light = light

def set_bright_scatter(self, bright=None, scatter=None):
if not bright is None: self.bright = bright
if not scatter is None: self.scatter = scatter

def reset(self, fovy=45, angx=0, angy=0):
self.fovy, self.angx, self.angy = fovy, angx, angy
Expand Down
12 changes: 11 additions & 1 deletion imagepy/menus/Kit3D/Analysis 3D/regionprops3d_plgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import numpy as np
from imagepy.core.engine import Simple, Filter
from scipy.ndimage import label, generate_binary_structure
from skimage.measure import marching_cubes_lewiner, mesh_surface_area
from skimage.segmentation import find_boundaries
from skimage.measure import regionprops
from numpy.linalg import norm
import pandas as pd
Expand All @@ -27,12 +29,13 @@ class RegionCounter(Simple):
note = ['8-bit', '16-bit', 'stack3d']

para = {'con':'8-connect', 'center':True, 'extent':False, 'vol':True,
'ed':False, 'holes':False, 'fa':False, 'cov':False}
'ed':False, 'holes':False, 'fa':False, 'cov':False, 'surf':True}

view = [(list, 'con', ['4-connect', '8-connect'], str, 'conection', 'pix'),
('lab', None, '========= indecate ========='),
(bool, 'center', 'center'),
(bool, 'vol', 'volume'),
(bool, 'surf', 'surface area'),
(bool, 'extent', 'extent'),
(bool, 'ed', 'equivalent diameter'),
(bool, 'cov', 'eigen values')]
Expand All @@ -43,6 +46,7 @@ def run(self, ips, imgs, para = None):

titles = ['ID']
if para['center']:titles.extend(['Center-X','Center-Y','Center-Z'])
if para['surf']:titles.append('Surface')
if para['vol']:titles.append('Volume')
if para['extent']:titles.extend(['Min-Z','Min-Y','Min-X','Max-Z','Max-Y','Max-X'])
if para['ed']:titles.extend(['Diameter'])
Expand All @@ -60,6 +64,12 @@ def run(self, ips, imgs, para = None):
dt.append([round(i.centroid[1]*k,1) for i in ls])
dt.append([round(i.centroid[0]*k,1) for i in ls])
dt.append([round(i.centroid[2]*k,1) for i in ls])
if para['surf']:
buf[find_boundaries(buf, mode='outer')] = 0
vts, fs, ns, cs = marching_cubes_lewiner(buf, level=0)
lst = [[] for i in range(n+1)]
for i in fs: lst[int(cs[i[0]])].append(i)
dt.append([0 if len(i)==0 else mesh_surface_area(vts, np.array(i))*k**2 for i in lst][1:])
if para['vol']:
dt.append([i.area*k**3 for i in ls])
if para['extent']:
Expand Down

0 comments on commit aadbd6d

Please sign in to comment.