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

📄 segment_demo.m

📁 mean shift
💻 M
📖 第 1 页 / 共 4 页
字号:
function Segment_demo (Action, Arguments, varargin)

global All_vars Biomed_on;
if size(All_vars, 2) > 0
   eval (['global ', All_vars]);
end
% regions are represented as structures of:
% private chars:
	% Number - region number
	% Color - the original color to draw the region
	% Line - the actual points as a line object
	% Active - whether it was merged into another region
	% From - which regions it was obtained from (by merging or cutting)
% public chars:
	% Boundary (Boundary) given as set of points
	% Area - total area of the regions, since computed anyway
	% Inner point - telling where the interior of region is (Inner_pts)
	% Shifts - a matrix of size 2 by 2 giving min, max x and y vals for boundary
	% Region_matr - the filled with pixels boundary for various operations
% user defined chars:

if nargin < 1
	warning off;
  	Common_vars =[' Gray_image Image Luv_image Image_is_gray ', ...
      ' Region_colors Bndred_regions Curr_pt Curr_pt_line ',...
      ' Slices Centers Store_mouse_pts ', ...
      ' Regions Region_menu_added Min_region_size ', ...
      ' Region_menu Main_figure Main_axis Marker_size ', ...
		' Region_left Region_right Show_all_regions ', ...
		' Instance Active_instances Biomed_on '];

	Biomed_vars = [' Amnt_rot Window_regs_chosen Window1 Window2 '];

	All_vars = [Common_vars, Biomed_vars];
	eval (['global ', All_vars]);
	Action = 'Initialize';
end

if strcmp(Action, 'Initialize')
   if nargin < 2  							% the very first call to it
      Msg_box_hndl = msgbox('Welcome to the system ...', 'Welcome');
      Child = get(Msg_box_hndl, 'Children');			% the OK button
      set(Child(length(Child)), 'Style', 'Text',    'String', '');
      Image_menu = uimenu(Msg_box_hndl, 'Label','&Image');
      uimenu(Image_menu, 'Label', '&Open...', ...
         'Callback', 'close; Segment_demo(''Read image'')', 'Accelerator', 'O');
      uimenu(Image_menu, 'Label', '&Quit', ...
         'Callback', 'close', 'Separator','on', 'Accelerator', 'Q');
		Screen_size = get(0, 'ScreenSize');
		Marker_size = round(max(Screen_size)/200);
   else
		I = varargin{length(varargin)};		% last argument always
      if length(size(Arguments)) == 3		% color image
         Image_is_gray{I} = 0;
         Image{I} = Arguments;
      else
         Image_is_gray{I} = 1;
         Image{I} = Arguments;
         Gray_image{I} = double(Arguments);
      end

		if not(varargin{1})		% does not need new window
			Kids = get(Main_axis{I}, 'Children');
			for i=1:length(Kids)
				if strcmp(get(Kids(i), 'Type'), 'line')
					set(Kids(i), 'XData', 0, 'YData', 0, 'Visible', 'off');
				end
			end
			Segment_demo('Refresh image', 0, I);
		end

      Regions{I} = {};
		Bndred_regions{I} = 0;
      Region_left{I} = 0;  Region_right{I} = 0;
      Store_mouse_pts{I} = 0; Curr_pt_line{I} = 0;
		Show_all_regions{I} = 0;	% will show only active regions by default

		% Biomed vars go here
		if Instance == 1
			Window_regs_chosen = 0;
		end
     	Amnt_rot{I} = 8;

      % the list of colors
      Region_colors{1, 1} = 'magenta';
      Region_colors{1, 2} = 'yellow';
      Region_colors{1, 3} = 'green';
      Region_colors{1, 4} = 'red';
      Region_colors{1, 5} = 'cyan';
      Region_colors{1, 6} = 'blue';
      Region_colors{1, 7} = 'black';

		if varargin{1}		% needs new window
      	Region_menu_added{I} = 0;
			ButtonDown = sprintf('Segment_demo(''%s'',%d,%d)', 'Down', 0, I);
			CloseReq = sprintf('Segment_demo(''%s'',%d,%d)', 'Exit', 0, I);
	      set(gcf, 'WindowButtonDownFcn', ButtonDown, 'MenuBar', 'none', ...
				'CloseRequestFcn', CloseReq);
	      Main_figure{I} = gcf;
			Main_axis{I} = gca;
	      Image_menu = uimenu('Label','&Image');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Read image', 0, I);
	      uimenu(Image_menu, 'Label', '&Open...', ...
	         'Callback', Callback, 'Accelerator', 'O');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Smooth prompt', 0, I);
	      uimenu(Image_menu, 'Label', 's&Mooth...', ...
	         'Callback', Callback, 'Accelerator', 'M');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Segment prompt', 0, I);
	      uimenu(Image_menu, 'Label', 'se&Gment...', ...
	         'Callback', Callback, 'Accelerator', 'G');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Crop', 0, I);
	      uimenu(Image_menu, 'Label', '&Crop...', ...
	         'Callback', Callback, 'Accelerator', 'C');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Refresh image', 0, I);
	      uimenu(Image_menu, 'Label', 'r&Efresh!', ...
	         'Callback', Callback, 'Accelerator', 'E');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Label prompt', 0, I);
	      uimenu(Image_menu, 'Label', 'l&abel', ...
	         'Callback', Callback, 'Accelerator', 'A');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Save image', 0, I);
	      uimenu(Image_menu, 'Label', '&Save as...', ...
	         'Callback', Callback, 'Accelerator', 'S');
			Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Exit', 0, I);
	      uimenu(Image_menu, 'Label', '&Quit', ...
	         'Callback', Callback, 'Separator','on', 'Accelerator', 'Q');
			Segment_demo('Show image', 1, I);		% needs window size setting
		else
			Segment_demo('Show image', 0, I);		% no window size setting
		end
   end
end


%-----------------------------------------
% exiting if desired, after warning
if strcmp(Action, 'Exit')
	I = varargin{length(varargin)};		% last argument always
   set(Main_figure{I}, 'CloseRequestFcn', 'closereq');
   close;
	Active_instances = Active_instances-1;
	if Active_instances == 0
		eval (['clear global ', All_vars]);
	end

%-----------------------------------------
% keeping track of the pixel clicked onto by mouse
elseif strcmp(Action, 'Down')	% request to pick region follows
	I = varargin{length(varargin)};		% last argument always
  	Curr_pt_get{I} = get(gca, 'CurrentPoint');
   Curr_pt_get{I} = round(Curr_pt_get{I}(1, 1:2));
   Curr_pt_get{I} = [Curr_pt_get{I}(2); Curr_pt_get{I}(1)]; % switching x and y
   if Store_mouse_pts{I}
      Curr_pt{I} = [Curr_pt{I}, Curr_pt_get{I}];
   else
      Curr_pt{I} = Curr_pt_get{I};
   end
   if Curr_pt_line{I}
      set(Curr_pt_line{I}, 'XData',Curr_pt{I}(2, :), 'YData',Curr_pt{I}(1, :));
   else
      Curr_pt_line{I} = ...
			line('XData', Curr_pt{I}(2, :), 'YData', Curr_pt{I}(1, :), ...
         'Marker', '.', 'Color', 'yellow', 'EraseMode', 'none');
   end
 
 
%-----------------------------------------
% segmenting the image into a number of subregions by the subroutine
elseif strcmp(Action, 'Segment prompt')
	I = varargin{length(varargin)};		% last argument always
   Prompt  = {...
		'Window size (small window => small variation)', ...
		'Color components to use (fewer => quicker)', ...
      'Color group size (small size => fewer colors left)', ...
      'Min region size (small size => more small regions)', ...
      'Would you like to see separate layers?'};
	if Image_is_gray{I}
		Default = {20, 1, 200, 20, 'No'};
	 else
     	Default = {30, 2, 200, 20, 'No'};
	end
   Title = 'Segmentation parameters';
   Line_number  = 1;
   Segment_input  = My_inputdlg(Prompt, Title, Line_number, Default);
   if size(Segment_input, 2) > 0
      Segment_demo ('Initialize', Image{I}, 0, I); 	% does not need new wind
      Segment_demo ('Segment', Segment_input, I);
   end


%-----------------------------------------
% segmenting the image into a number of subregions by the subroutine
elseif strcmp(Action, 'Segment')
	I = varargin{length(varargin)};		% last argument always
   Segment_input = Arguments;
   Window_radius = base2dec(Segment_input{1}, 10);
	Components = base2dec(Segment_input{2}, 10);
	% Components = round(max(Components, 2));		% for a while
   Color_group = base2dec(Segment_input{3}, 10);
   Min_region_size{I} = base2dec(Segment_input{4}, 10);
	Plot_slices = length(findstr(lower(Segment_input{5}), 'y'));
   if Image_is_gray{I}
      [Segmented, Centers] = ...
         Gray_segment (Gray_image{I}, Window_radius, Color_group);
   else
      Luv_image{I} = Rgb_to_luv(Image{I}, 'Image');
      Gray_image{I} = squeeze(Luv_image{I}(:, :, 1));
		Lhs = '[Segmented, Centers] = ';
		Rhs = [sprintf('Color_segment_svd%d', Components), ...
			'(Luv_image{I}, Window_radius, Color_group);'];
		eval([Lhs, Rhs]);
   end

   Segments = size(Centers, 2);
   Slices{I} = zeros(size(Gray_image{I}));

   if Plot_slices
      figure;	% plotting the resulting regions
      Square_side = ceil(sqrt(Segments));
   end

   for i=1:Segments
      Slice = (Segmented == i);
      % some image processing - taken from morphology file
      Slice = bwmorph(Slice, 'majority');
      Slice = (Slice == 0);	% negation
      Slice = bwmorph(Slice, 'majority');
      Slice = (Slice == 0);	% negation
      % Slice = bwmorph(Slice, 'thicken', 2);    % no thickening for a while
      Slices{I}(:, :, i) = bwlabel(Slice);
      if Plot_slices
         subplot(Square_side, Square_side, i);
         image((Slices{I}(:, :, i)>0)*255); axis off;
         colormap(gray(255));
      end
   end
	if Plot_slices
   	figure(Main_figure{I});
	end

  	% appending the new submenu
	if not(Region_menu_added{I})
		Region_menu{I} = uimenu('Label', '&Region');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Find boundary', 0, I);
		uimenu(Region_menu{I}, 'Label', '&Find boundary', ...
			'Callback', Callback, 'Accelerator', 'F');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Select left region', 0, I);
		uimenu(Region_menu{I}, 'Label', '(un)select &1-st region', 'Enable', 'off', ...
            'Callback', Callback, 'Accelerator', '1');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Select right region', 0, I);
		uimenu(Region_menu{I}, 'Label', '(un)select &2-nd region', 'Enable', 'off', ...
            'Callback', Callback, 'Accelerator', '2');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)','Save boundary prompt',0,I);
		uimenu(Region_menu{I}, 'Label', 'sa&Ve as...', 'Enable', 'off', ...
			'Callback', Callback, 'Accelerator', 'V');
		Compare_menu = uimenu(Region_menu{I}, 'Label', '&Compare to...', ...
			'Enable', 'off');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
			'Compare disk regions prompt', 0, I);
		uimenu(Compare_menu, 'Label', '&Disk region...', ...
			'Callback', Callback, 'Accelerator', 'D');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
			'Compare image regions prompt', 0, I);
		uimenu(Compare_menu, 'Label', '&Image region...', ...
			'Callback', Callback, 'Accelerator', 'I');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
			'Compare window regions prompt', 0, I);
		uimenu(Compare_menu, 'Label', '&Window region...', ...
			'Callback', Callback, 'Accelerator', 'W');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Merge prompt', 0, I);
		uimenu(Region_menu{I}, 'Label', 'me&Rge...', 'Enable', 'off', ...
			'Callback', Callback, 'Accelerator', 'R');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Cut prompt', 0, I);
		uimenu(Region_menu{I}, 'Label', 'c&Ut...', 'Enable', 'off', ...
			'Callback', Callback, 'Accelerator', 'U');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
			'Intensity 3-d prompt', 0, I);
		uimenu(Region_menu{I},'Label', 'intensity &3-d...', 'Enable','off', ...
			'Callback', Callback, 'Accelerator', '3');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', ...
			'Background region prompt',0, I);
		uimenu(Region_menu{I}, 'Label', 'bac&Kground...', 'Enable', 'off', ...
            'Callback', Callback, 'Accelerator', 'K');
		Callback=sprintf('Segment_demo(''%s'',%d,%d)', 'Color prompt', 0, I);
		uimenu(Region_menu{I}, 'Label', 'co&Lor...', 'Enable', 'off', ...
            'Callback', Callback, 'Accelerator', 'L');
		Callback=sprintf('global Marker_size; Marker_size = Marker_size + 2;');
		uimenu(Region_menu{I}, 'Label', 'Thicken next line', 'Enable', 'off', ...
			'Callback', Callback);
		Callback=sprintf('global Marker_size; Marker_size = Marker_size - 2;');
		uimenu(Region_menu{I}, 'Label', 'Thin next line', 'Enable', 'off', ...
			'Callback', Callback);
		Region_menu_added{I} = 1;
	end


%-----------------------------------------
elseif strcmp(Action, 'Crop')
	I = varargin{length(varargin)};		% last argument always
	msgbox('Please crop a subregion', 'Cropping');
   waitforbuttonpress;
   Point1 = get(gca,'CurrentPoint');    % button down detected
   Final_rect = rbbox;                  % return Figure units
   Point2 = get(gca,'CurrentPoint');    % button up detected
   Point1 = round(Point1(1,1:2));       % extract x and y
   Point2 = round(Point2(1,1:2));
   P1 = min(Point1,Point2);             % calculate locations
   Offset = abs(Point1-Point2);         % and dimensions
   Rect_x = [P1(1) P1(1)+Offset(1) P1(1)+Offset(1) P1(1) P1(1)];
   Rect_y = [P1(2) P1(2) P1(2)+Offset(2) P1(2)+Offset(2) P1(2)];
   hold on;
   axis manual;
   plot(Rect_x, Rect_y);

   Button = questdlg('Is it suitable?', 'Crop/Undo cropping','Yes','No','Yes');
   if strcmp(Button,'Yes')
      Left_x = min(Rect_y);  Right_x = max(Rect_y);	% again shift
      Low_y = min(Rect_x);  High_y = max(Rect_x);		% again shift
      if Image_is_gray{I}
         Image_cropped = Gray_image{I}(Left_x:Right_x, Low_y:High_y);
      else
         Image_cropped = Image{I}(Left_x:Right_x, Low_y:High_y, :);
      end

		% creating a window for the newly cropped image
		figure;							% creating one more window
		set (gcf, 'Name', [get(Main_figure{I}, 'Name'), ' cropped']);
		if length(Instance) > 0	% was defined
			Instance = Instance+1;
			Active_instances = Active_instances+1;
		 else
		 	Instance = 1;
			Active_instances = 1;
		end
   	Segment_demo ('Initialize', Image_cropped, 1, Instance);	%needs new window
	else
		Segment_demo('Refresh image', 0, I);
   end   


%-----------------------------------------
elseif strcmp(Action, 'Smooth prompt')
	I = varargin{length(varargin)};		% last argument always
   Scr_sz = get(0, 'ScreenSize');
   New_wind = dialog('Name', 'Which filter to use?', 'Position', ...
      [Scr_sz(3)*0.3 Scr_sz(4)*0.4 Scr_sz(3)*0.3 Scr_sz(4)*0.1]);
   Choice = uicontrol('Parent', New_wind);

⌨️ 快捷键说明

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