1+ classdef FloodFillTool < imagem .gui .Tool
2+ % Set pixel color to white
3+ %
4+ % output = FloodFillToolTool(input)
5+ %
6+ % Example
7+ % FloodFillToolTool
8+ %
9+ % See also
10+ %
11+
12+ % ------
13+ % Author: David Legland
14+ 15+ % Created: 2011-11-21, using Matlab 7.9.0.529 (R2009b)
16+ % Copyright 2011 INRA - Cepia Software Platform.
17+
18+ %% Properties
19+ properties
20+ ButtonPressed = false ;
21+ end
22+
23+ %% Constructor
24+ methods
25+ function obj = FloodFillTool(viewer , varargin )
26+ % Creates a new tool using parent gui and a name
27+ obj
= [email protected] (
viewer ,
' floofill' );
28+ end % constructor
29+
30+ end % construction function
31+
32+ %% General methods
33+ methods
34+ function onMouseButtonPressed(obj , hObject , eventdata ) % #ok<INUSD>
35+ processCurrentPosition(obj );
36+ obj.ButtonPressed = true ;
37+ end
38+
39+ function onMouseButtonReleased(obj , hObject , eventdata ) % #ok<INUSD>
40+ obj.ButtonPressed = false ;
41+ end
42+
43+ function onMouseMoved(obj , hObject , eventdata ) % #ok<INUSD>
44+ if ~obj .ButtonPressed
45+ return ;
46+ end
47+ processCurrentPosition(obj );
48+ end
49+
50+ function processCurrentPosition(obj )
51+ doc = currentDoc(obj );
52+ img = doc .Image ;
53+
54+ if ~isScalarImage(img )
55+ return ;
56+ end
57+
58+ point = get(obj .Viewer .Handles .ImageAxis , ' CurrentPoint' );
59+ coord = round(pointToIndex(obj , point(1 , 1 : 2 )));
60+
61+ % control on bounds of image
62+ if any(coord < 1 ) || any(coord > size(img , [1 2 ]))
63+ return ;
64+ end
65+
66+ % apply floodfill and update current image
67+ res = floodFill(img , coord , obj .Viewer .Gui .App .BrushValue );
68+ img.Data = res .Data ;
69+
70+ updateDisplay(obj .Viewer );
71+ end
72+
73+ function index = pointToIndex(obj , point )
74+ % Converts coordinates of a point in physical dimension to image index
75+ % First element is column index, second element is row index, both are
76+ % given in floating point and no rounding is performed.
77+ doc = currentDoc(obj );
78+ img = doc .Image ;
79+ spacing = img .Spacing(1 : 2 );
80+ origin = img .Origin(1 : 2 );
81+ index = (point - origin ) ./ spacing + 1 ;
82+ end
83+
84+ end % methods
85+
86+ end % classdef
0 commit comments