Skip to content

Commit

Permalink
finsihed plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jries committed Mar 31, 2021
1 parent 8f3c239 commit 543d7d9
Showing 1 changed file with 111 additions and 49 deletions.
160 changes: 111 additions & 49 deletions plugins/+ROIManager/+Evaluate/plot3D_annotate.m
Expand Up @@ -3,7 +3,9 @@
% with a Gaussian or double-Gaussian model
properties
roicoordinates
roihandles
roihandles={[]};
roisize
roisizey
% currentroi=0;
axis
images
Expand All @@ -17,7 +19,9 @@
out=[];
make3Daxis(obj) %make axis,
plot3Dviews(obj,p);%plot image,

if ~isfield(obj.site.annotation,'polarangle')
obj.site.annotation.polarangle=0;
end
% obj.currentroi=0;
if isfield(obj.site.evaluation,obj.name) && isfield(obj.site.evaluation.(obj.name),'roicoordinates')
obj.roicoordinates=obj.site.evaluation.(obj.name).roicoordinates;
Expand All @@ -32,19 +36,30 @@
end
function addroi_callback(obj,a,b)
roistyle=obj.getSingleGuiParameter('roiform').selection;
[roifun,roifunmake,vertexbased]=roistyle2fun(roistyle);
[roifun,roifunmake,vertexbased,par]=roistyle2fun(roistyle);

newroi1=roifun(obj.axis);
newroi1=roifun(obj.axis,'DrawingArea',[-obj.roisize -obj.roisizey obj.roisize*2 obj.roisizey*2],'Color','w',par{:});
[roipospix,roipos]=getpixcoord(obj,newroi1.Position,zeros(size(newroi1.Position,1),3));
roiposnm=roipospix; %later: convert with rotation and position

roiposnm=coord3Dpix2nm(roipospix,obj.site.pos,obj.site.annotation.rotationpos.angle,obj.site.annotation.polarangle,obj.roisize);

% roiposnm=roipospix; %later: convert with rotation and position
% obj.currentroi=obj.currentroi+1;
obj.roicoordinates(end+1).roistyle=roistyle;
obj.roicoordinates(end).Position=roiposnm;
delete(newroi1);
obj.plotrois;
end
function deleteroi_callback(obj,a,b)
obj.roicoordinates=[];
% obj.plotrois;
plot3Dviews(obj);
end

function plotrois(obj)
if isempty(obj.roicoordinates)
return
end
%loop over roicoords
% k=obj.currentroi;
%delete old rois
Expand All @@ -53,24 +68,26 @@ function plotrois(obj)
delete(obj.roihandles{2}(k));
end

colors=lines(255);

for k=1:length(obj.roicoordinates)
[roifun,roifunmake,vertexbased]=roistyle2fun(obj.roicoordinates(k).roistyle);
[roifun,roifunmake,vertexbased,par]=roistyle2fun(obj.roicoordinates(k).roistyle);
roiposnm=obj.roicoordinates(k).Position;
roipospix=roiposnm; %later convert
roipospix=coord3Dnm2pix(roiposnm,obj.site.pos,obj.site.annotation.rotationpos.angle,obj.site.annotation.polarangle,obj.roisize);
roipos1=[];roipos2=[];
roipos1(:,1:2)=roipospix(:,1:2);
roipos2(:,1)=roipospix(:,1)+obj.images.rangex(2);
roipos2(:,1)=roipospix(:,1)+obj.images.rangex(2)*1000;
roipos2(:,2)=roipospix(:,3);

newroi1(k)=roifunmake(obj.axis,'Position',roipos1);
newroi1(k)=roifunmake(obj.axis,'Position',roipos1,'DrawingArea',[-obj.roisize -obj.roisizey obj.roisize obj.roisizey*2],'Color',colors(k,:),par{:});
addlistener(newroi1(k),'ROIMoved',@(src,evt) obj.updateposition(src,evt,k,1));
addlistener(newroi1(k),'DeletingROI',@(src,evt) obj.deleteroi(src,evt,k,1));
if vertexbased
addlistener(newroi1(k),'VertexAdded',@(src,evt) obj.updateposition(src,evt,k,1));
addlistener(newroi1(k),'VertexDeleted',@(src,evt) obj.updateposition(src,evt,k,1));
end

newroi2(k)=roifunmake(obj.axis,'Position',roipos2);
newroi2(k)=roifunmake(obj.axis,'Position',roipos2,'DrawingArea',[0 -obj.roisizey obj.roisize obj.roisizey*2],'Color',colors(k,:),par{:});
addlistener(newroi2(k),'ROIMoved',@(src,evt) obj.updateposition(src,evt,k,2));
addlistener(newroi2(k),'DeletingROI',@(src,evt) obj.deleteroi(src,evt,k,2));
if vertexbased
Expand All @@ -83,43 +100,52 @@ function plotrois(obj)
end

function updateposition(obj,src,evt,currentroi,roiside)
oldpospix=obj.roicoordinates(currentroi).Position; %convert
[roipospix,roipos]=getpixcoord(obj,src.Position,oldpospix);

newroiposnm=roipospix;

oldposnm=obj.roicoordinates(currentroi).Position; %convert
oldpospix=coord3Dnm2pix(oldposnm,obj.site.pos,obj.site.annotation.rotationpos.angle,obj.site.annotation.polarangle,obj.roisize);

newpospix=src.Position;
%vertex added or deleted: oldpos has different number
if size(oldpospix,1) > size(newpospix,1) %vertex deleted
ind=findextra(oldpospix,newpospix);
oldpospix(ind,:)=[];
elseif size(oldpospix,1) < size(newpospix,1) %vertex added
ind=findextra(newpospix,oldpospix);
oldpospix=[oldpospix(1:ind-1,:); zeros(1,3); oldpospix(ind:end,:)];
end

[roipospix,roipos]=getpixcoord(obj,newpospix,oldpospix);

newroiposnm=coord3Dpix2nm(roipospix,obj.site.pos,obj.site.annotation.rotationpos.angle,obj.site.annotation.polarangle,obj.roisize);
obj.roicoordinates(currentroi).Position=newroiposnm;
plotrois(obj)

end
% function plotdistances(obj)
% modality=obj.getSingleGuiParameter('modality').selection;
% pos=obj.site.evaluation.(obj.name).(modality).Position;
%
% period2=mean(diff(pos(:,1)));
% form='%2.2f';
% period=(pos(end,1)-pos(1,1))/(size(pos,1)-1);
%
% title(obj.axis,['Period: ' num2str(period,form)])
%
% obj.site.evaluation.(obj.name).(modality).Period=period;
%
% end
% function fit_callback(obj,a,b)
% pos=obj.site.evaluation.(obj.name).Position;
% period=mean(diff(pos(:,1)));
%
%
% end

function deleteroi(obj,src,evt,k,roiside)
obj.roicoordinates(k)=[];
obj.site.evaluation.(obj.name).roicoordinates(k)=[];
plotrois(obj)
end

end

end

function ind=findextra(A,B)
xA=A(:,1);
xB=B(:,1);
mind=min((xA-xB').^2,[],2);
[~,ind]=max(mind);
end

function [roipospix,roipos]=getpixcoord(obj,roiposh,roiposold)
roipospix=roiposold;
if mean(roiposh(:,1))>0
roipos=2; %right
roiposh(:,1)=max(roiposh(:,1),0);
roipospix(:,1)=roiposh(:,1)-obj.images.rangex(2);
roipospix(:,1)=roiposh(:,1)-obj.roisize;
roipospix(:,3)=roiposh(:,2); %move to left
% roipospix(:,2)=0;
else
Expand All @@ -130,17 +156,40 @@ function updateposition(obj,src,evt,currentroi,roiside)
end
end


function roiposnm=coord3Dpix2nm(roipos,pos,rotationanglez,polarangle,winsize)
xp=roipos(:,1)+winsize/2;
yp=roipos(:,2);zp=roipos(:,3);
[y2,zi]=rotcoorddeg(yp,zp,polarangle);
[xi,yi]=rotcoorddeg(xp,y2,rotationanglez);
roiposnm(:,1)=xi+pos(1);
roiposnm(:,2)=yi+pos(2);
roiposnm(:,3)=zi+pos(3);
end
function roipos=coord3Dnm2pix(roiposnm,pos,rotationanglez,polarangle,winsize)
xi=roiposnm(:,1)-pos(1);
yi=roiposnm(:,2)-pos(2);
zi=roiposnm(:,3)-pos(3);
[xp,y2]=rotcoorddeg(xi,yi,-rotationanglez);
[yp,zp]=rotcoorddeg(y2,zi,-polarangle);
roipos(:,1)=xp-winsize/2;
roipos(:,2)=yp;
roipos(:,3)=zp;
end

function pard=guidef(obj)
pard.addroi.object=struct('Style','pushbutton','String','add','Callback',@obj.addroi_callback);
pard.addroi.position=[1,1];
pard.addroi.Width=1;

pard.roiform.object=struct('Style','popupmenu','String',{{'line','polyline','rectangle','ellipse','polygon','free'}});
pard.roiform.object=struct('Style','popupmenu','String',{{'polyline','line','free','polygon'}});
pard.roiform.position=[1,2];
pard.roiform.Width=2;


pard.deleterois.object=struct('Style','pushbutton','String','delete all rois','Callback',@obj.deleteroi_callback);
pard.deleterois.position=[2,1];
pard.deleterois.Width=2;

% pard.equaldistance.object=struct('Style','checkbox','String','equal distances');
% pard.equaldistance.position=[2,1];
% pard.equaldistance.Width=2;
Expand Down Expand Up @@ -208,13 +257,13 @@ function plot3Dviews(obj,p)
prz.sr_axes=-1;
prz.normalizeFoV=prz.sr_sizeRecPix(1)/obj.locData.getPar('se_sitefov')*obj.locData.getPar('se_siteroi')/2;

posh=[prz.sr_pos(1) prz.sr_pos(2) prz.sr_size(1)*2 prz.sr_size(2)*2];
% posh=[prz.sr_pos(1) prz.sr_pos(2) prz.sr_size(1)*2 prz.sr_size(2)*2];
locz=obj.locData.getloc({'xnm','ynm','znm','locprecnm','locprecznm','PSFxnm','intensity_render','phot','numberInGroup',prz.renderfield.selection},'layer',k,'position',obj.site);
if length(prz.sr_pos)<3
prz.sr_pos(3)=0;
end
xi=locz.xnm-posh(1);
yi=locz.ynm-posh(2);
xi=locz.xnm-site.pos(1);
yi=locz.ynm-site.pos(2);
zi=locz.znm-site.pos(3);
[x2,y2]=rotcoorddeg(xi,yi,prz.rotationanglez);
[y3,z3]=rotcoorddeg(y2,zi,prz.polarangle);
Expand All @@ -239,6 +288,8 @@ function plot3Dviews(obj,p)
imzc.rangex=[ imz{2}.rangex(1)-imz{2}.rangex(2) imz{1}.rangex(2)-imz{1}.rangex(1)];

displayimage(imzc,obj.axis);
obj.roisize=imzc.rangex(2)*1000;
obj.roisizey=imzc.rangey(2)*1000;
fl='%2.0f';
title(obj.axis,['\theta=' num2str(p.polarangle,fl) '\circ, \rho=' num2str(p.rotationanglez,fl) '\circ, z= ' num2str(site.pos(3),fl) ' nm'])
obj.images=imzc;
Expand All @@ -259,11 +310,11 @@ function make3Daxis(obj)

function sideview_click(hax,dat,obj)
site=obj.site;
dx=(obj.images.rangex(2)-obj.images.rangex(1))/2;
dy=obj.images.rangey(2)-obj.images.rangey(1);
dx=(obj.images.rangex(2)-obj.images.rangex(1))/2*1000;
dy=(obj.images.rangey(2)-obj.images.rangey(1))*1000;
pos=dat.IntersectionPoint;
if pos(1)>0 && pos(1)<dx*0.75 %only right side: side view%
site.pos(3)=site.pos(3)+pos(2)*1000;
site.pos(3)=site.pos(3)+pos(2);
elseif pos(1)>dx*.75 %outside: rotate polarangle
if ~isfield(site.annotation,'polarangle')
site.annotation.polarangle=0;
Expand All @@ -280,16 +331,17 @@ function sideview_click(hax,dat,obj)
end

plot3Dviews(obj)
obj.plotrois
end

function displayimage(img,hax)
if isempty(img)
return
end
imagesc(img.rangex,img.rangey,img.image,'Parent',hax,'Pickable','none','HitTest','off')
imagesc(img.rangex*1000,img.rangey*1000,img.image,'Parent',hax,'Pickable','none','HitTest','off')

set(hax,'Xlim',double(img.rangex))
set(hax,'Ylim',double(img.rangey))
set(hax,'Xlim',double(img.rangex*1000))
set(hax,'Ylim',double(img.rangey*1000))
set(hax,'YDir','reverse')
hax.HitTest='on';
hax.PickableParts='all';
Expand All @@ -308,24 +360,33 @@ function displayimage(img,hax)
end


function [roifun,roifunmake,vertexbased]=roistyle2fun(roistyle)
vertexbased=true;
function [roifun,roifunmake,vertexbased,par]=roistyle2fun(roistyle)
vertexbased=false;
par={};
switch roistyle
case 'rectangle'
roifun=@drawrectangle;
roifunmake=@Rectangle;
roifunmake=@images.roi.Rectangle;

case 'ellipse'
roifun=@mydrawellipse;
roifun=@drawellipse;
roifunmake=@images.roi.Ellipse;
case 'polygon'
roifun=@drawpolygon;
roifunmake=@images.roi.Polygon;
vertexbased=true;
case 'free'
roifun=@drawfreehand;
roifunmake=@images.roi.Freehand;
par={'Closed',false};
case 'polyline'
roifun=@drawpolyline;
roifunmake=@images.roi.Polyline;
vertexbased=true;
case 'line'
roifun=@drawline;
vertexbased=false;
roifunmake=@images.roi.Line;

end
end
function resetview(a,b,obj,site)
Expand All @@ -334,6 +395,7 @@ function resetview(a,b,obj,site)
site.pos(3)=0;
obj.setPar('se_currentPolarAngle',0)
plot3Dviews(obj)
obj.plotrois
end
function info(a,b)
text='To rotate in x-y plane: click in the left top view image in the direction you want to point left. \nTo change the z-position, click on the right image, left part, this position will be centered. \nTo change the polar angle, click in the right image, right part. The closer you are to the center, the smaller the change.';
Expand Down

0 comments on commit 543d7d9

Please sign in to comment.