MATLAB Answers

Number of pixels while modifying freehand on a figure

1 view (last 30 days)
Nikan Fakhari
Nikan Fakhari on 2 Aug 2021
Edited: DGM on 3 Aug 2021
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

Answers (1)

DGM
DGM on 3 Aug 2021
Edited: DGM on 3 Aug 2021
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!