📄 im_segmentation.m
字号:
function varargout = im_segmentation(varargin)
% IM_SEGMENTATION M-file for im_segmentation.fig
% IM_SEGMENTATION, by itself, creates a new IM_SEGMENTATION or raises the existing
% singleton*.
%
% H = IM_SEGMENTATION returns the handle to a new IM_SEGMENTATION or the handle to
% the existing singleton*.
%
% IM_SEGMENTATION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IM_SEGMENTATION.M with the given input arguments.
%
% IM_SEGMENTATION('Property','Value',...) creates a new IM_SEGMENTATION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before im_segmentation_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to im_segmentation_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help im_segmentation
% Last Modified by GUIDE v2.5 18-May-2006 22:40:17
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @im_segmentation_OpeningFcn, ...
'gui_OutputFcn', @im_segmentation_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before im_segmentation is made visible.
function im_segmentation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to im_segmentation (see VARARGIN)
% Choose default command line output for im_segmentation
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes im_segmentation wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = im_segmentation_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
%seedgrow(56,65,7);
load file
T=imread(file);
[m,n]=size(T);
T=double(T);
subm=1;
subn=1;
s=strcat(num2str(n),'*',num2str(m),'大小的图片');
while subm<2
answer=inputdlg('请输入将原图分割成M*N,M=',s);
subm=str2num(char(answer));
end
while subn<2
answer=inputdlg('请输入将原图分割成M*N,N=',s);
subn=str2num(char(answer));
end
x1=floor(m/subm);
y1=floor(n/subn);
sum=0;
B=zeros(size(T));
h=waitbar(0,'请等待...');
for i=1:x1:floor(m/x1)*x1
for j=1:y1:floor(n/y1)*y1
for p=1:x1
for q=1:y1
sum=sum+T(i+p-1,j+q-1);
end
end
sum=sum/(x1*y1);
for p=1:x1
for q=1:y1
thex=i+p-1;
they=j+q-1;
if T(thex,they)>=sum&&B(thex,they)==0
B(thex,they)=255;
else
B(thex,they)=0;
end
end
end
sum=0;
end
waitbar(i/(floor(m/x1)*x1));
end
close(h);
T=B;
subplot(1,2,2);
imshow(T);
title('阈值分割后的图片');
save T
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load file
T=imread(file);
[m,n]=size(T);
T=double(T);
tempmax=zeros(4,1);
tempmin=zeros(4,1);
judgement=zeros(4,1);
X=T;
for i=1:floor(m/2)
for j=1:floor(n/2)
if T(i,j)>tempmax(1)
tempmax(1)=T(i,j);
end
if T(i,j)<tempmin(1)
tempmin(1)=T(i,j);
end
end
end
for i=1:floor(m/2)
for j=floor(n/2)+1:n
if T(i,j)>tempmax(2)
tempmax(2)=T(i,j);
end
if T(i,j)<tempmin(2)
tempmin(2)=T(i,j);
end
end
end
for i=floor(m/2)+1:m
for j=1:floor(n/2)
if T(i,j)>tempmax(3)
tempmax(3)=T(i,j);
end
if T(i,j)<tempmin(3)
tempmin(1)=T(i,j);
end
end
end
for i=floor(m/2)+1:m
for j=floor(n/2)+1:n
if T(i,j)>tempmax(4)
tempmax(4)=T(i,j);
end
if T(i,j)<tempmin(4)
tempmin(1)=T(i,j);
end
end
end
for i=1:4
judgement(i)=(tempmin(i)+tempmax(i))/2;
end
for i=1:floor(m/2)
for j=1:floor(n/2)
if T(i,j)>=judgement(1)
X(i,j)=255;
else
X(i,j)=0;
end
end
end
for i=1:floor(m/2)
for j=floor(n/2)+1:n
if T(i,j)>=judgement(2)
X(i,j)=255;
else
X(i,j)=0;
end
end
end
for i=floor(m/2)+1:m
for j=1:floor(n/2)
if T(i,j)>=judgement(3)
X(i,j)=255;
else
X(i,j)=0;
end
end
end
for i=floor(m/2)+1:m
for j=floor(n/2)+1:n
if T(i,j)>=judgement(4)
X(i,j)=255;
else
X(i,j)=0;
end
end
end
T=uint8(X);
subplot(1,2,2);
imshow(T);
title('阈值分割后的图片');
save T
% --------------------------------------------------------------------function Open_Callback(hObject, eventdata, handles)% hObject handle to Open (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)clear all;
clc;
[filename,pathname]=uigetfile({'*tif','TIF Files'},'打开文件');
if isequal([filename,pathname],[0,0])
return
else
file=fullfile(pathname,filename);
end
T=imread(file);
subplot(1,2,1)
imshow(T);
title('原始图片');
save T;
save file;% --------------------------------------------------------------------function File_Callback(hObject, eventdata, handles)% hObject handle to File (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Save_Callback(hObject, eventdata, handles)% hObject handle to Save (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)load T;
[filename,pathname]=uiputfile({'*.tif','TIF Files'},'保存文件');
if isequal([filename,pathname],[0,0])
return
else
file=fullfile(pathname,filename);
end
if findstr(lower(file),'.tif')
else
file=strcat(file,'.tif');
end
imwrite(T,file);
save T;
clear all
clc% --------------------------------------------------------------------function Close_Callback(hObject, eventdata, handles)% hObject handle to Close (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)clear all
clc
close% --------------------------------------------------------------------function Thresholding_Callback(hObject, eventdata, handles)% hObject handle to Thresholding (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
pushbutton2_Callback% load file
% T=imread(file);
% [m,n]=size(T);
% T=double(T);
% X=zeros(size(T));
% h=waitbar(0,'请等待...');
% for i=1:m
% for j=1:n
%
%
% end
% waitbar(i/m)
% end
% close(h);
% T=uint8(X);
% imshow(T);
% title(s)
% save T% --------------------------------------------------------------------function Regiongrow_Callback(hObject, eventdata, handles)% hObject handle to Regiongrow (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)pushbutton3_Callback
% seedgrow(56,79,1)
%
% for i=1:m
% for j=1:n
% switch Logical(i,j)
% case num
% end
% end
% end
% --------------------------------------------------------------------function Untitled_1_Callback(hObject, eventdata, handles)% hObject handle to Untitled_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)load file
T=imread(file);
[m,n]=size(T);
T=double(T);
subplot(1,2,2)
plot([0,m],[0,n],'LineStyle','none');
axis tight;
hold on;
B=T;
subseed=0;
delta=0;
while subseed<=0||subseed>10
answer=inputdlg('请输入种子点个数N=');
subseed=str2num(char(answer));
end
while delta<=0||delta>40
answer=inputdlg('请输域值T=');
delta=str2num(char(answer));
end
thex=zeros(1,subseed);
they=zeros(1,subseed);
for i=1:subseed
a=[-1,-1];
while a(1)<=0||a(1)>=m||a(2)<=0||a(2)>=n
a=ginput(1);
end
thex(i)=a(1);
they(i)=a(2);
plot(a(1),a(2),'LineStyle','none','Marker','o','MarkerFaceColor','g');
end
thex=floor(thex);
they=floor(they);
Test=zeros(size(T));
for i=1:subseed
% clear Seedsx;
% clear Seedsy;
s=strcat('第',num2str(i),'个种子点生长中请等待...');
h=waitbar(0,s);
Seedsx=zeros(1,m*n);
Seedsy=zeros(1,m*n);
seedx=thex(i);
seedy=they(i);
Seedsx(1)=thex(i);
Seedsy(1)=they(i);
head=1;
tail=1;
while head==1||head<tail
if (seedx-1)>=1&&Test(seedx-1,seedy)==0&&abs(T(seedx,seedy)-T(seedx-1,seedy))<=delta
tail=tail+1;
Seedsx(tail)=seedx-1;
Seedsy(tail)=seedy;
Test(seedx-1,seedy)=i;
end
if (seedx+1)<=m&&Test(seedx+1,seedy)==0&&abs(T(seedx,seedy)-T(seedx+1,seedy))<=delta
tail=tail+1;
Seedsx(tail)=seedx+1;
Seedsy(tail)=seedy;
Test(seedx+1,seedy)=i;
end
if (seedy-1)>=1&&Test(seedx,seedy-1)==0&&abs(T(seedx,seedy)-T(seedx,seedy-1))<=delta
tail=tail+1;
Seedsx(tail)=seedx;
Seedsy(tail)=seedy-1;
Test(seedx,seedy-1)=i;
end
if (seedy+1)<=n&&Test(seedx,seedy+1)==0&&abs(T(seedx,seedy)-T(seedx,seedy+1))<=delta
tail=tail+1;
Seedsx(tail)=seedx;
Seedsy(tail)=seedy+1;
Test(seedx,seedy+1)=i;
end
head=head+1;
seedx=Seedsx(head);
seedy=Seedsy(head);
waitbar(head/(m*n));
end
close(h);
end
for i=1:m
for j=1:n
for seed=1:subseed
if Test(i,j)==seed
B(i,j)=T(they(seed),thex(seed));
end
end
end
end
hold off;
T=uint8(B);
subplot(1,2,2);
imshow(T);
title('区域生长后的图片');
save T
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -