Number of pixels while modifying freehand on a figure

1 次查看(过去 30 天)
Hi there,
Is there a way so matlab contniously display the number of the pixels within ROI as Im modyfing the shape (shape of freehand) object on a figure?
So for example I displayed a freehand object on my matlab figure, now I want to modify the shape using the WayPoints, tho while Im modifying I need to khow much my number of pixels within the ROI changes.
I would really appreciate it, if you can help me with this issue.
Thank you.
Nikan

回答(1 个)

DGM
DGM 2021-8-3
编辑:DGM 2021-8-3
Here is a very simple example
inpict = imread('cameraman.tif');
% show image and make ROI
imshow(inpict);
R = drawfreehand(gca);
% specify CBF to be executed when ROI is moved
addlistener(R,'MovingROI',@whenmove);
addlistener(R,'ROIMoved',@whenmove);
% show initial area before manipulation
area = sum(sum(createMask(R)))
% function to be executed whenever ROI or nodes are moved
function whenmove(R,~)
% just dump the area to console
% you can choose to do something fancier
area = sum(sum(createMask(R)))
end
As an example of "something fancier", you might want to tidy up console output a bit. This is a very crude approach, but it's cleaner than dumping everything.
% pad the console so that redraw doesn't back into prior output/warnings
fprintf('\n \n')
% function to be executed whenever ROI or nodes are moved
function whenmove(R,~)
area = sum(sum(createMask(R)));
rpad = sprintf(repmat(sprintf('\b'),[1 19]));
thisnum = sprintf('%d',area);
fprintf([rpad 'Area is ' thisnum repmat(' ',[1 10-numel(thisnum)]) '\n'])
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by