How can I display the relevant pictures in Fig2 in real time after right-clicking to get the coordinate information from Fig1?
1 view (last 30 days)
Show older comments
Hi everyone,
I have a 3d dimensional stack A and I want to mark some points in a selected image depth, e.g. A(:,:,100).
I implemented in Fig1 the function to achieve left-click on the mouse marked point.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/950249/image.png)
Now I want to right-click to get its coordinates to view the contents of another section of the three-dimensional array, such as A(:,50,:),
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/950254/image.png)
and display it in Fig2 in real time.
But my result overwrites the picture in Fig1. Since I will right-click a lot, I don't want to pop up a figure every time I right click
Here is my code:
where stack is a 3 dimentional array, hFig is the the figure window opened in advance, input_image is a 2d section of stack in z direction.
function manual_select_zdirection(stack, hFig,input_img)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input:
% img: the image which is needed to plot contril points
% You can use 'space' to adjust image contrast
ud_slice.stack = stack;
ud_slice.pointList = [];
ud_slice.pointHands1 = [];
ud_slice.pointHands2 = [];
ud_slice.pointText = [];
ud_slice.pointnum = 0;
ud_slice.getPoint = 1;
ud_slice.ref_size = size(input_img);
ud_slice.original_img = input_img;
% set(ud_slice.sliceAx, 'HitTest', 'off');
ud_slice.sliceAxy = axes('Position', [0.05 0.05 0.9 0.9]);
hold(ud_slice.sliceAxy, 'on');
ud_slice.im = imshow(input_img);
Fig2 = figure('Name','z image display');
set(ud_slice.im, 'ButtonDownFcn', @(fig,event)MouseClickCallback(fig,event));
set(hFig, 'KeyPressFcn', @(fig,event)KeyboardHotkeyFcn(fig,event));
set(hFig, 'UserData', ud_slice)
% ------------------------------------------------
% Clicking function to register transform points
% ------------------------------------------------
function MouseClickCallback(fig,event)
f = get(get(fig, 'Parent'), 'Parent');
ud = get(f, 'UserData');
if strcmp(f.SelectionType,'alt')
clickX = round(event.IntersectionPoint(1));
clickY = round(event.IntersectionPoint(2));
yz = squeeze(stack(:,clickX,:));
yz = low_light_adjust(yz);
Fig2
subplot(221),
imshow(yz);
xz = squeeze(stack(clickY,:,:));
xz = low_light_adjust(xz);
xz = permute(xz,[2 1]);
Fig2
subplot(222),
imshow(xz);
elseif strcmp(f.SelectionType,'normal')
clickX = round(event.IntersectionPoint(1));
clickY = round(event.IntersectionPoint(2));
ud.pointList(end+1, :) = [clickX,clickY];
ud.pointHands1(end+1) = plot(ud.sliceAxy, clickX, clickY, 'ro', 'color', [0 0.5 0],'linewidth',2,'Marker','o','MarkerSize',12);
ud.pointHands2(end+1) = plot(ud.sliceAxy, clickX, clickY, 'ro', 'color', 'c','linewidth',1,'Marker','+','MarkerSize',6);
ud.pointnum = ud.pointnum + 1;
ud.pointText(end+1) = text(clickX,clickY,num2str(ud.pointnum),'Color','r');
set(f, 'UserData', ud);
end
end
% --------------------
% Respond to keypress
% use 'space' to adjust contrast
% --------------------
function KeyboardHotkeyFcn(fig,event)
ud = get(fig, 'UserData');
if strcmpi(event.Key, 'space') % adjust contrast
imcontrast(fig)
elseif strcmp(event.Key,'delete')
% disp('current transform points deleted')
% Try to delete only most recent point
if ud.pointnum == 0
disp('There are no more marked points!')
else
set(ud.pointHands1(ud.pointnum), 'Visible', 'off');
set(ud.pointHands2(ud.pointnum), 'Visible', 'off');
set(ud.pointText(ud.pointnum),'Visible','off');
ud.pointHands1(ud.pointnum) = [];
ud.pointHands2(ud.pointnum) = [];
ud.pointText(ud.pointnum)=[];
ud.pointList(ud.pointnum,:)=[];
disp('transform point deleted')
ud.pointnum = ud.pointnum - 1;
set(fig, 'UserData', ud);
end
end
end
% --------------------
% Respond to keypress
% use 'space' to adjust contrast
% --------------------
function out = low_light_adjust(input)
out = imadjust(input,stretchlim(input),[0,1]);
end
end
Another choice is to show all of them in fig1 using subplot(221),subplot(222),subplot(223), but my marker function seems does not to work.
I sincerely ask for your help, thank you!
0 Comments
Accepted Answer
Voss
on 2 Apr 2022
Merely issuing this command:
Fig2
does not make Fig2 the current figure, which I think is your intent.
Instead, you can say:
figure(Fig2);
(And you only need to do it the first time in MouseClickCallback - the second time, Fig2 will still be the current figure.)
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!