⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segment_demo.m

📁 mean shift
💻 M
📖 第 1 页 / 共 4 页
字号:
      Min_y = min(Region2.Public.Boundary(2, :));
		Max_y = max(Region2.Public.Boundary(2, :));
      Region2.Public.Shifts = [Min_x, Max_x; Min_y, Max_y];
      fclose(fid);
      [Bdry_dist, Reg_dist] = ...
			Svd_dist_reg(Regions{I}{Region_left{I}}.Public, Region2.Public);
      Msg = [sprintf('Boundary difference is  %6.2f\n\n', Bdry_dist), ...
      		 sprintf('Region  difference  is  %6.2f', Reg_dist)]; 
      msgbox(Msg, 'Difference');
   end
   Segment_demo('Draw boundary', Region_left{I}, I);
   Region_left{I} = 0;
   
   
%-----------------------------------------
elseif strcmp(Action, 'Intensity 3-d prompt')
	I = varargin{length(varargin)};		% last argument always
   Operation_prompt;									% the common piece
	if Interactive 
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Intensity 3-d', 0, I);
		if Ok_button
			set(Ok_button, 'Callback', Callback);
		end
	else
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Intensity 3-d', -1, I);
		eval(Callback);
	end

%-----------------------------------------
elseif strcmp(Action, 'Intensity 3-d')
	I = varargin{length(varargin)};		% last argument always
	if Arguments == 0							% interactive call
		Region_number = Region_left{I};
   	close;									% prompt
	elseif Arguments == -1					% was selected not by menu
		Region_number = Region_left{I};
	else
		Region_number = Arguments;
	end
   Volume = 0;
	Area = 0;
   [Rows, Cols] = size(Regions{I}{Region_number}.Public.Region_matr);
   To_display = zeros(Cols, Rows);
   Shift = Regions{I}{Region_number}.Public.Shifts(1:2, 1);
   for i=1:Rows
      for j=1:Cols
         if Regions{I}{Region_number}.Public.Region_matr(i, j)
            To_display(j, i) = Gray_image{I}(i+Shift(1)-1, j+Shift(2)-1);
            Volume = Volume+To_display(j, i);
				Area = Area+1;
         end
      end
   end
   Min = min(To_display(find(To_display > 0)));
   To_display = max(To_display, ones(size(To_display))*Min);
	figure;	% (2)
   mesh(To_display);
	set(gca, 'XTick', [], 'YTick', [], 'view', [77, 67]);	% a better viewpoint
	zlabel('Intensity');
	Wind_name = sprintf('Region %d Intensity 3-d', Region_number);
   set(gcf, 'MenuBar', 'none', 'Name', Wind_name);
   Rotate_menu = uimenu('Label','&Rotate');
	Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate left', 0, I);
   uimenu (Rotate_menu, 'Label', 'Left', ...
      'Callback', Callback, 'Accelerator', 'L');
	Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate right', 0, I);
   uimenu (Rotate_menu, 'Label', 'Right', ...
      'Callback', Callback, 'Accelerator', 'R');
	Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate up', 0, I);
   uimenu (Rotate_menu, 'Label', 'Up', ...
      'Callback', Callback, 'Accelerator', 'U');
	Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Rotate down', 0, I);
   uimenu (Rotate_menu, 'Label', 'Down', ...
      'Callback', Callback, 'Accelerator', 'D');
	Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Increase rotation', 0, I);
   uimenu (Rotate_menu, 'Label', 'Increase', ...
      'Callback', Callback, 'Accelerator', 'A');
	Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Decrease rotation', 0, I);
   uimenu (Rotate_menu, 'Label', 'Decrease', ...
      'Callback', Callback, 'Accelerator', 'V');
   
   Max = max(max(To_display));
	
	Regions{I}{Region_number}.Public.Area = Area;		% storing area
   Area_string = sprintf('Region area\t%10.2f', Area);
	Regions{I}{Region_number}.Public.Volume = Volume;	% storing volume
   Volume_string = sprintf('Volume\t%10.2f', Volume); % compute avg intens
	Avg_intensity = Volume/Area;
   Regions{I}{Region_number}.Public.Avg_intensity = Avg_intensity;
   Avg_intensity_string = sprintf('Average intensity\t%10.2f', Avg_intensity);
   Intensity_normed_volume_string = ...
      sprintf('Intensity normalized volume\t%10.2f', Volume/Max);
	
   Values_menu = uimenu('Label', '&Values');
   uimenu (Values_menu, 'Label', Area_string);
   uimenu (Values_menu, 'Label', Volume_string);
   uimenu (Values_menu, 'Label', Avg_intensity_string);
   uimenu (Values_menu, 'Label', Intensity_normed_volume_string);

	if Arguments <= 0		% interactive
   	Segment_demo('Draw boundary', Region_left{I}, I);
   	Region_left{I} = 0;
	end

%-----------------------------------------
elseif strcmp(Action, 'Rotate right')
	I = varargin{length(varargin)};		% last argument always
   Az_el = get(gca, 'View');
   set(gca, 'View', [Az_el(1)-Amnt_rot{I}, Az_el(2)]);

%-----------------------------------------
elseif strcmp(Action, 'Rotate left')
	I = varargin{length(varargin)};		% last argument always
   Az_el = get(gca, 'View');
   set(gca, 'View', [Az_el(1)+Amnt_rot{I}, Az_el(2)]);

%-----------------------------------------
elseif strcmp(Action, 'Rotate up')
	I = varargin{length(varargin)};		% last argument always
   Az_el = get(gca, 'View');
   set(gca, 'View', [Az_el(1), Az_el(2)-Amnt_rot{I}]);

%-----------------------------------------   
elseif strcmp(Action, 'Rotate down')
	I = varargin{length(varargin)};		% last argument always
   Az_el = get(gca, 'View');
   set(gca, 'View', [Az_el(1), Az_el(2)+Amnt_rot{I}]);
 
%-----------------------------------------   
elseif strcmp(Action, 'Increase rotation')
	I = varargin{length(varargin)};		% last argument always
   Amnt_rot{I} = Amnt_rot{I}*2;

%-----------------------------------------   
elseif strcmp(Action, 'Decrease rotation')
	I = varargin{length(varargin)};		% last argument always
   Amnt_rot{I} = Amnt_rot{I}/2;

   
%-----------------------------------------
elseif strcmp(Action, 'Cut prompt')
	I = varargin{length(varargin)};		% last argument always
   Operation_prompt;									% the common piece
	if Interactive 
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region cut prompt', 0, I);
		if Ok_button
			set(Ok_button, 'Callback', Callback);
		end
	else
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Region cut prompt', 1, I);
		eval(Callback);
	end
 
%-----------------------------------------
elseif strcmp(Action, 'Region cut prompt')
	I = varargin{length(varargin)};		% last argument always
	if Arguments == 0		% interactive input
   	close;
	end
   Scr_sz = get(0, 'ScreenSize');
   New_wind = dialog('Name', 'Draw cutting line', 'Position', ...
      [Scr_sz(3)*0.3 Scr_sz(4)*0.4 Scr_sz(3)*0.3 Scr_sz(4)*0.1]);
   set(New_wind, 'WindowStyle', 'normal');
   Cancel_button = uicontrol('Parent', New_wind, ...
      'String', 'Cancel', 'Style', 'Pushbutton', 'Callback', 'close', ...
      'Units', 'Normalized', 'Position', [0.6 0.3 0.3 0.4]);
	Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Cut', 0, I);
   Ok_button = uicontrol('Parent', New_wind, ...
      'String', 'OK', 'Style', 'Pushbutton', 'Callback', Callback, ...
      'Units', 'Normalized', 'Position', [0.1 0.3 0.3 0.4]);
   Store_mouse_pts{I} = 1;
   Curr_pt{I}= zeros(2, 0);
 
%-----------------------------------------
elseif strcmp(Action, 'Cut')
	I = varargin{length(varargin)};		% last argument always
   close;											% the dialogue window
   Dense_pts = Fit_spline(Curr_pt{I});
   
   [Bdry{1}, Inner{1}, Bdry{2}, Inner{2}] = ...
      Cut_boundary_reg(Regions{I}{Region_left{I}}.Public, Dense_pts);
   if (size(Bdry{1}, 2) > 1) & (size(Bdry{2}, 2) > 1)		% detected boundaries
      for i=1:2
         Bndred_regions{I} = Bndred_regions{I}+1;
         Regions{I}{Bndred_regions{I}}.Public.Boundary = Bdry{i};
         Regions{I}{Bndred_regions{I}}.Public.Inner_pts = Inner{i};
         Min_x = min(Regions{I}{Bndred_regions{I}}.Public.Boundary(1, :));  
         Max_x = max(Regions{I}{Bndred_regions{I}}.Public.Boundary(1, :));
         Min_y = min(Regions{I}{Bndred_regions{I}}.Public.Boundary(2, :));  
         Max_y = max(Regions{I}{Bndred_regions{I}}.Public.Boundary(2, :));
         Regions{I}{Bndred_regions{I}}.Public.Shifts = ...
				[Min_x, Max_x; Min_y, Max_y];
         Regions{I}{Bndred_regions{I}}.Private.Number = Bndred_regions{I};
		   Regions{I}{Bndred_regions{I}}.Public.Region_matr = ...
				Region_matrix(Regions{I}{Bndred_regions{I}}.Public);
         Regions{I}{Bndred_regions{I}}.Private.Color = ...
            Region_colors{1, 1+rem(Bndred_regions{I}-1,size(Region_colors, 2))};
		   Regions{I}{Bndred_regions{I}}.Private.From = [Region_left{I}];
        	Regions{I}{Bndred_regions{I}}.Public.Area = -99;
        	Regions{I}{Bndred_regions{I}}.Public.Volume= -99;
        	Regions{I}{Bndred_regions{I}}.Public.Avg_intensity = -99;
         Segment_demo('Draw boundary first time', Bndred_regions{I}, I);% Line
			Segment_demo('Enable region', Bndred_regions{I}, I);
      end
		Segment_demo('Disable region', Region_left{I}, I);
   end

   Region_left{I} = 0;
   Store_mouse_pts{I} = 0;
	set(Curr_pt_line{I}, 'EraseMode', 'normal');
	set(Curr_pt_line{I}, 'Visible', 'off');
	Curr_pt_line{I} = 0;
   Segment_demo('Down', 0, I);
	Segment_demo('Refresh', 0, I);
 

% ----------------------------------------- "left" region
elseif strcmp(Action, 'Left')
	I = varargin{length(varargin)};		% last argument always
   Index_left = get(Arguments, 'Value');
   Labels = get(Arguments, 'String');
   Temp_region_left = base2dec(Labels{Index_left}, 10);
   % restoring the old region first
   if Region_left{I}									% was assigned some value
      if not(Region_left{I} == Region_right{I})
         Segment_demo('Draw boundary', Region_left{I}, I);
      end
   end
   Region_left{I} = Temp_region_left;
   Segment_demo('Draw boundary', Region_left{I}, 'white', I);
   
 
%-----------------------------------------   
% "right" region
elseif strcmp(Action, 'Right')
	I = varargin{length(varargin)};		% last argument always
   Index_right = get(Arguments, 'Value');
   Labels = get(Arguments, 'String');
   Temp_region_right = base2dec(Labels{Index_right}, 10);
   % restoring the old region first
   if Region_right{I}								% was defined
      if not(Region_left{I} == Region_right{I})
         Segment_demo('Draw boundary', Region_right{I}, I);
      end
   end
   Region_right{I} = Temp_region_right;
   Segment_demo('Draw boundary', Region_right{I}, 'white', I);
 

%-----------------------------------------   
elseif strcmp(Action, 'Cancel operation')		% don't perform operation
	I = varargin{length(varargin)};		% last argument always
   close;
   if Region_left{I}
      Segment_demo('Draw boundary', Region_left{I}, I);
      Region_left{I} = 0;
   end
   if Region_right{I}
      Segment_demo('Draw boundary', Region_right{I}, I);
      Region_right{I} = 0;   
   end
   Store_mouse_pts{I} = 0;
 

%-----------------------------------------
elseif strcmp(Action, 'Merge prompt')
	I = varargin{length(varargin)};		% last argument always
   Operation_prompt;								% the common piece
   Right_button;									% one more button here
	if Interactive
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Merge', 0, I);
		if Ok_button
			set(Ok_button, 'Callback', Callback);
		end
	else
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Merge', 1, I);
		eval(Callback);
	end
 
%-----------------------------------------   
elseif strcmp(Action, 'Merge')		% the two regions have been defined
	I = varargin{length(varargin)};		% last argument always
	if Arguments == 0							% interactive call
   	close;	% prompt
   end
   Bndred_regions{I} = Bndred_regions{I}+1;
   [Regions{I}{Bndred_regions{I}}.Public.Inner_pts, ...
   	Regions{I}{Bndred_regions{I}}.Public.Boundary, ...
		Regions{I}{Bndred_regions{I}}.Public.Shifts] = Merge_regions(...
			Regions{I}{Region_left{I}}.Public, Regions{I}{Region_right{I}}.Public);
   Regions{I}{Bndred_regions{I}}.Public.Region_matr = ...
		Region_matrix(Regions{I}{Bndred_regions{I}}.Public);
   Regions{I}{Bndred_regions{I}}.Private.Color = ...
      Region_colors{1, 1+rem(Bndred_regions{I}-1, size(Region_colors, 2))};
   Regions{I}{Bndred_regions{I}}.Private.Number = Bndred_regions{I};
	Regions{I}{Bndred_regions{I}}.Private.From=[Region_left{I}, Region_right{I}];
   Regions{I}{Bndred_regions{I}}.Public.Area = -99;
   Regions{I}{Bndred_regions{I}}.Public.Volume = -99;
   Regions{I}{Bndred_regions{I}}.Public.Avg_intensity = -99;
	Segment_demo('Draw boundary first time', Bndred_regions{I}, I);

  	Segment_demo('Enable region', Bndred_regions{I}, I);
   
	Segment_demo('Disable region', Region_left{I}, I);
	Segment_demo('Disable region', Region_right{I}, I);
   Region_left{I} = 0;	Region_right{I} = 0;


% --------------------
elseif strcmp(Action, 'Draw boundary first time')
	I = varargin{length(varargin)};		% last argument always
   Regions{I}{Arguments}.Private.Line = ...
      line('XData', Regions{I}{Arguments}.Public.Boundary(2, :), ...
      'YData', Regions{I}{Arguments}.Public.Boundary(1, :), ...
      'Parent', Main_axis{I}, 'Color', Regions{I}{Arguments}.Private.Color, ...
		'Marker', '.', 'LineStyle', '.', 'EraseMode', 'none', ...
		'Visible', 'off', 'LineWidth', Marker_size, 'MarkerSize', Marker_size);

% -------------------- since repeates so often
elseif strcmp(Action, 'Draw boundary')   
	I = varargin{length(varargin)};		% last argument always
   if length(varargin) == 1					% draw the region in the usual color
	  	if Regions{I}{Arguments}.Private.Active
      	set(Regions{I}{Arguments}.Private.Line, 'Visible', 'off');
      	set(Regions{I}{Arguments}.Private.Line, ...
         	'Color', Regions{I}{Arguments}.Private.Color, 'Visible', 'on');
	 	else
      	set(Regions{I}{Arguments}.Private.Line, 'Visible', 'off');
	 	end
   else
      if strcmp(varargin{1}, 'white')		% draw in white
         set(Regions{I}{Arguments}.Private.Line, ...
            'Color', 'white', 'Visible', 'on');
      elseif strcmp(varargin{1}, 'none')	% don't draw
         set(Regions{I}{Arguments}.Private.Line, 'Visible', 'off');
      end
   end
end

% let's represent regions (whose boundaries we were asked to find) by
% those boundaries and make the merging part as filling those
% boundaries and taking union as it was done before.  In this fashion
% we can handle new regions, not to abuse the original image.  Merging
% will be done on pairs of regions resulting in a new region

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -