Jun 7, 2007 ... LAMPIRAN SOURCE CODE MATLAB 7.1. APLIKASI. SEGMENTASI CITRA
SPOT DENGAN MENGGUNAKAN. PENDEKATAN FUZZY C- ...
L1
LAMPIRAN SOURCE CODE MATLAB 7.1 APLIKASI SEGMENTASI CITRA SPOT DENGAN MENGGUNAKAN PENDEKATAN FUZZY C-MEANS •
Main Program Source Code
function varargout = Image_Segmentation_Using_Fuzzy_C_Means(varargin) % IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS M-file for Image_Segmentation_Using_Fuzzy_C_Means.fig % IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS, by itself, creates a new IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS or raises the existing % singleton*. % % H = IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS returns the handle to a new IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS or the handle to % the existing singleton*. % % IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS('CALLBACK',hObject,eventData,han dles,...) calls the local % function named CALLBACK in IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS.M with the given input arguments. % % IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS('Property','Value',...) creates a new IMAGE_SEGMENTATION_USING_FUZZY_C_MEANS or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before Image_Segmentation_Using_Fuzzy_C_Means_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to Image_Segmentation_Using_Fuzzy_C_Means_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 Image_Segmentation_Using_Fuzzy_C_Means % Last Modified by GUIDE v2.5 07-Jun-2007 03:37:13 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Image_Segmentation_Using_Fuzzy_C_Means_OpeningFcn, ...
L2 'gui_OutputFcn', @Image_Segmentation_Using_Fuzzy_C_Means_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(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 Image_Segmentation_Using_Fuzzy_C_Means is made visible. function Image_Segmentation_Using_Fuzzy_C_Means_OpeningFcn(hObject, eventdata, handles, varargin) global img; set(handles.Image3,'visible','off'); set(handles.cluster1,'visible','off'); set(handles.cluster2,'visible','off'); set(handles.cluster3,'visible','off'); set(handles.cluster4,'visible','off'); set(handles.cluster5,'visible','off'); set(handles.cluster6,'visible','off'); set(handles.btncluster1,'visible','off'); set(handles.btncluster2,'visible','off'); set(handles.btncluster3,'visible','off'); set(handles.btncluster4,'visible','off'); set(handles.btncluster5,'visible','off'); set(handles.btncluster6,'visible','off'); set(handles.valuecluster1,'visible','off'); set(handles.valuecluster2,'visible','off'); set(handles.valuecluster3,'visible','off'); set(handles.valuecluster4,'visible','off'); set(handles.valuecluster5,'visible','off'); set(handles.valuecluster6,'visible','off'); set(handles.clusterbox1,'visible','off'); set(handles.clusterbox2,'visible','off'); set(handles.clusterbox3,'visible','off'); set(handles.clusterbox4,'visible','off'); set(handles.clusterbox5,'visible','off'); set(handles.clusterbox6,'visible','off'); handles.output = hObject; img.status = 0; % Update handles structure guidata(hObject, handles); % UIWAIT makes Image_Segmentation_Using_Fuzzy_C_Means wait for user response (see UIRESUME) % uiwait(handles.info);
L3
% --- Outputs from this function are returned to the command line. function varargout = Image_Segmentation_Using_Fuzzy_C_Means_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;
% -------------------------------------------------------------------function open_Callback(hObject, eventdata, handles) global img;
img.open = uigetfile({'*.jpg';'*.gif'},'Open Image'); img.read = imread (img.open); axes(handles.Image3); img.load = imshow (img.read); img.status = 1; % -------------------------------------------------------------------function save_Callback(hObject, eventdata, handles) global img; img.save = uiputfile ({'*.jpg';'*.gif';'*.tif';'*.raw';'*.png'},'Save Image'); img.get = getimage(handles.Image3); imwrite(img.get,img.save); % -------------------------------------------------------------------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 about_Callback(hObject, eventdata, handles) % hObject handle to about (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) helpdlg(sprintf(' Jimmy Johan / 0700708953 \n Sherwin Sasmita Rachmat / 0700710314'),'About Us'); % -------------------------------------------------------------------function exit_Callback(hObject, eventdata, handles) button = questdlg('Do you want to close application?','Exit','Yes','No','Yes'); if button=='Yes' close(figure(Image_Segmentation_Using_Fuzzy_C_Means));
L4 elseif button=='No' clc; Image_Segmentation_Using_Fuzzy_C_Means_OpeningFcn; end; % --- Executes on button press in start. function start_Callback(hObject, eventdata, handles) global img; if(img.status == 0) msgbox(' Please Load Image First','Image Load'); elseif (img.status == 2) msgbox(' The Image Has been Classified','Image Classification'); else
%Parse and initialize input and output arguments if ndims(img.read)~=3 msgbox('Input image must be m x n x 3 (RGB)'); end %Operate on image as double, regardless of input type if ~isa(img.read,'double') img.read = im2double(img.read); end %Get individual RGB colors included in segmentation [a,b,c]=size(img.read); rc = img.read(:,:,1); gc = img.read(:,:,2); bc = img.read(:,:,3); img.temp = img.read; segcolors = double([rc(:) gc(:) bc(:)]); axes(handles.Image3); img.load = imshow (img.temp); nClusters = 6; title('Segmenting. Please wait.......'); drawnow; [Idx,U,fcnobj] = fcm(segcolors,nClusters); [g,j]=size(U); img.status = zeros(a,b); img.clus1 = 0; img.clus2 = 0; img.clus3 = 0; img.clus4 = 0; img.clus5 = 0; img.clus6 = 0;
degree = max(U); for i=1 : j if ( U(1,i)== degree(1,i) )
L5 Id(i,:) = Idx(1,:); img.clus1 = img.clus1+1; elseif ( U(2,i)== degree(1,i) Id(i,:) = Idx(2,:); img.clus2 = img.clus2+1; elseif ( U(3,i)== degree(1,i) Id(i,:) = Idx(3,:); img.clus3 = img.clus3+1; elseif ( U(4,i)== degree(1,i) Id(i,:) = Idx(4,:); img.clus4 = img.clus4+1; elseif ( U(5,i)== degree(1,i) Id(i,:) = Idx(5,:); img.clus5 = img.clus5+1; elseif ( U(6,i)== degree(1,i) Id(i,:) = Idx(6,:); img.clus6 = img.clus6+1; end; end;
)
)
)
)
)
img.size = j; %------------------------------------------------------------------------% Cluster #1 clus1(1,:) = U(1,:); cluster1 = reshape(clus1,size(img.read(:,:,1))); markcluster1 = zeros(a,b); img.imagecluster1 = zeros(a,b,c); for x=1 : a for y=1 :b if cluster1(x,y)> 0.5 markcluster1(x,y) = 1; end; end; end; [pixel,I] = contour_trace(markcluster1,1,1); [batasx,batasy]=size(I); I(batasx,:) = 0; I(batasx-1,:) = 0; I(:,batasy) = 0; I(:,batasy-1) = 0; for x=1 : a for y=1 :b if I(x,y) == 255 img.imagecluster1(x,y,1)=Idx(1,1); img.imagecluster1(x,y,2)=Idx(1,2); img.imagecluster1(x,y,3)=Idx(1,3); end; end; end; %------------------------------------------------------------------------% Cluster #2 clus2(1,:) = U(2,:); cluster2 = reshape(clus2,size(img.read(:,:,1))); markcluster2 = zeros(a,b);
L6 img.imagecluster2 = zeros(a,b,c); for x=1 : a for y=1 :b if cluster2(x,y)> 0.5 markcluster2(x,y) = 1; end; end; end; [pixel,I] = contour_trace(markcluster2,1,1); [batasx,batasy]=size(I); I(batasx,:) = 0; I(batasx-1,:) = 0; I(:,batasy) = 0; I(:,batasy-1) = 0; for x=1 : a for y=1 :b if I(x,y) == 255 img.imagecluster2(x,y,1)=Idx(2,1); img.imagecluster2(x,y,2)=Idx(2,2); img.imagecluster2(x,y,3)=Idx(2,3); end; end; end; %------------------------------------------------------------------------% Cluster #3 clus3(1,:) = U(3,:); cluster3 = reshape(clus3,size(img.read(:,:,1))); markcluster3 = zeros(a,b); img.imagecluster3 = zeros(a,b,c); for x=1 : a for y=1 :b if cluster3(x,y)> 0.5 markcluster3(x,y) = 1; end; end; end; [pixel,I] = contour_trace(markcluster3,1,1); [batasx,batasy]=size(I); I(batasx,:) = 0; I(batasx-1,:) = 0; I(:,batasy) = 0; I(:,batasy-1) = 0; for x=1 : a for y=1 :b if I(x,y) == 255 img.imagecluster3(x,y,1)=Idx(3,1); img.imagecluster3(x,y,2)=Idx(3,2); img.imagecluster3(x,y,3)=Idx(3,3); end; end; end; %------------------------------------------------------------------------% Cluster #4 clus4(1,:) = U(4,:); cluster4 = reshape(clus4,size(img.read(:,:,1)));
L7 markcluster4 = zeros(a,b); img.imagecluster4 = zeros(a,b,c); for x=1 : a for y=1 :b if cluster4(x,y)> 0.5 markcluster4(x,y) = 1; end; end; end; [pixel,I] = contour_trace(markcluster4,1,1); [batasx,batasy]=size(I); I(batasx,:) = 0; I(batasx-1,:) = 0; I(:,batasy) = 0; I(:,batasy-1) = 0; for x=1 : a for y=1 :b if I(x,y) == 255 img.imagecluster4(x,y,1)=Idx(4,1); img.imagecluster4(x,y,2)=Idx(4,2); img.imagecluster4(x,y,3)=Idx(4,3); end; end; end; %------------------------------------------------------------------------% Cluster #5 clus5(1,:) = U(5,:); cluster5 = reshape(clus5,size(img.read(:,:,1))); markcluster5 = zeros(a,b); img.imagecluster5 = zeros(a,b,c); for x=1 : a for y=1 :b if cluster5(x,y)> 0.5 markcluster5(x,y) = 1; end; end; end; [pixel,I] = contour_trace(markcluster5,1,1); [batasx,batasy]=size(I); I(batasx,:) = 0; I(batasx-1,:) = 0; I(:,batasy) = 0; I(:,batasy-1) = 0; for x=1 : a for y=1 :b if I(x,y) == 255 img.imagecluster5(x,y,1)=Idx(5,1); img.imagecluster5(x,y,2)=Idx(5,2); img.imagecluster5(x,y,3)=Idx(5,3); img.status(x,y) =5; end; end; end; %-------------------------------------------------------------------------
L8 % Cluster #6 clus6(1,:) = U(6,:); cluster6 = reshape(clus6,size(img.read(:,:,1))); markcluster6 = zeros(a,b); img.imagecluster6 = zeros(a,b,c); for x=1 : a for y=1 :b if cluster6(x,y)> 0.5 markcluster6(x,y) = 1; end; end; end; [pixel,I] = contour_trace(markcluster6,1,1); [batasx,batasy]=size(I); I(batasx,:) = 0; I(batasx-1,:) = 0; I(:,batasy) = 0; I(:,batasy-1) = 0; for x=1 : a for y=1 :b if I(x,y) == 255 img.imagecluster6(x,y,1)=Idx(6,1); img.imagecluster6(x,y,2)=Idx(6,2); img.imagecluster6(x,y,3)=Idx(6,3); end; end; end; %-------------------------------------------------------------------------
pixel_labels = reshape(Id,size(img.read)); axes(handles.Image3); img.load = imshow(pixel_labels,[]); title('Image labeled by (FCM) Fuzzy C-Means Clustering'); img.Idx = Idx; colormap(Idx); tmp = colorbar; set(tmp,'ytick',1:nClusters); img.status = 2; img.classimage = pixel_labels; end % --- Executes on button press in information. function information_Callback(hObject, eventdata, handles) % hObject handle to information (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global img; if(img.status == 0) msgbox(' Please Load Image First','Image Load'); elseif (img.status == 1) msgbox(' Please Classification The Image First','Image Classification'); else
L9
set(handles.btncluster1,'visible','on'); set(handles.btncluster2,'visible','on'); set(handles.btncluster3,'visible','on'); set(handles.btncluster4,'visible','on'); set(handles.btncluster5,'visible','on'); set(handles.btncluster6,'visible','on'); set(handles.valuecluster1,'visible','on'); set(handles.valuecluster2,'visible','on'); set(handles.valuecluster3,'visible','on'); set(handles.valuecluster4,'visible','on'); set(handles.valuecluster5,'visible','on'); set(handles.valuecluster6,'visible','on'); set(handles.clusterbox1,'visible','on'); set(handles.clusterbox2,'visible','on'); set(handles.clusterbox3,'visible','on'); set(handles.clusterbox4,'visible','on'); set(handles.clusterbox5,'visible','on'); set(handles.clusterbox6,'visible','on'); %Pembuatan Image Cluster cluster1 = zeros(10,10,3); cluster1(:,:,1)=img.Idx(1,1); cluster1(:,:,2)=img.Idx(1,2); cluster1(:,:,3)=img.Idx(1,3); cluster2 = zeros(10,10,3); cluster2(:,:,1)=img.Idx(2,1); cluster2(:,:,2)=img.Idx(2,2); cluster2(:,:,3)=img.Idx(2,3); cluster3 = zeros(10,10,3); cluster3(:,:,1)=img.Idx(3,1); cluster3(:,:,2)=img.Idx(3,2); cluster3(:,:,3)=img.Idx(3,3); cluster4 = zeros(10,10,3); cluster4(:,:,1)=img.Idx(4,1); cluster4(:,:,2)=img.Idx(4,2); cluster4(:,:,3)=img.Idx(4,3); cluster5 = zeros(10,10,3); cluster5(:,:,1)=img.Idx(5,1); cluster5(:,:,2)=img.Idx(5,2); cluster5(:,:,3)=img.Idx(5,3); cluster6 = zeros(10,10,3); cluster6(:,:,1)=img.Idx(6,1); cluster6(:,:,2)=img.Idx(6,2); cluster6(:,:,3)=img.Idx(6,3); %Menampilkan Image Cluster axes(handles.cluster1) imshow(cluster1); axes(handles.cluster2) imshow(cluster2); axes(handles.cluster3)
L10 imshow(cluster3); axes(handles.cluster4) imshow(cluster4); axes(handles.cluster5) imshow(cluster5); axes(handles.cluster6) imshow(cluster6); nilaicluster1 nilaicluster2 nilaicluster3 nilaicluster4 nilaicluster5 nilaicluster6
= = = = = =
0; 0; 0; 0; 0; 0;
nilaicluster1 nilaicluster2 nilaicluster3 nilaicluster4 nilaicluster5 nilaicluster6
= = = = = =
(img.clus1/img.size)*100; (img.clus2/img.size)*100; (img.clus3/img.size)*100; (img.clus4/img.size)*100; (img.clus5/img.size)*100; (img.clus6/img.size)*100;
set(handles.valuecluster1,'String',[nilaicluster1]); set(handles.valuecluster2,'String',[nilaicluster2]); set(handles.valuecluster3,'String',[nilaicluster3]); set(handles.valuecluster4,'String',[nilaicluster4]); set(handles.valuecluster5,'String',[nilaicluster5]); set(handles.valuecluster6,'String',[nilaicluster6]);
for i =1 :6 if ((img.Idx(i,1)>=110/255) && (img.Idx(i,1)=120/255) && (img.Idx(i,2)=100/255) && (img.Idx(i,3)=130/255)&& (img.Idx(i,1)=140/255) && (img.Idx(i,2)=130/255) && (img.Idx(i,3)=45/255) && (img.Idx(i,1)=55/255) && (img.Idx(i,2)=50/255) && (img.Idx(i,3)=85/255)&& (img.Idx(i,1)=100/255) && (img.Idx(i,2)=85/255) && (img.Idx(i,3)=70/255)&& (img.Idx(i,1)=80/255) && (img.Idx(i,2)=70/255) && (img.Idx(i,3)=170/255) && (img.Idx(i,1)=180/255) && (img.Idx(i,2)=170/255) && (img.Idx(i,3)=1 && (sp_x(loop,2)-1)>=1) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)-1),(sp_x(loop,2)-1))==0) k=k+1; sp_x(k,2)=sp_x(loop,2)-1; sp_x(k,1)=sp_x(loop,1)-1; BW((sp_x(loop,1)-1),(sp_x(loop,2)-1))=1; end end if ((sp_x(loop,1))>=1 && (sp_x(loop,2)-1)>=1) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)),(sp_x(loop,2)-1))==0) k=k+1; sp_x(k,2)=sp_x(loop,2)-1; sp_x(k,1)=sp_x(loop,1); BW((sp_x(loop,1)),(sp_x(loop,2)-1))=1;
L15 end end
if ((sp_x(loop,1)+1)>=1 && ((sp_x(loop,2)-1)>=1)) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)+1),(sp_x(loop,2)-1))==0) k=k+1; sp_x(k,2)=sp_x(loop,2)-1; sp_x(k,1)=sp_x(loop,1)+1; BW((sp_x(loop,1)+1),(sp_x(loop,2)-1))=1; end end
if ((sp_x(loop,1)-1)>=1 && ((sp_x(loop,2)+1)>=1)) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)-1),(sp_x(loop,2)+1))==0) k=k+1; sp_x(k,2)=sp_x(loop,2)+1; sp_x(k,1)=sp_x(loop,1)-1; BW((sp_x(loop,1)-1),(sp_x(loop,2)+1))=1; end end if ((sp_x(loop,1))>=1 && ((sp_x(loop,2)+1)>=1)) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)),(sp_x(loop,2)+1))==0) k=k+1; sp_x(k,2)=sp_x(loop,2)+1; sp_x(k,1)=sp_x(loop,1); BW((sp_x(loop,1)),(sp_x(loop,2)+1))=1; end end
L16 if ((sp_x(loop,1)+1)>=1 && ((sp_x(loop,2)+1)>=1)) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)+1),(sp_x(loop,2)+1))==0) k=k+1; sp_x(k,2)=sp_x(loop,2)+1; sp_x(k,1)=sp_x(loop,1)+1; BW((sp_x(loop,1)+1),(sp_x(loop,2)+1))=1; end end if ((sp_x(loop,1)+1)>=1 && ((sp_x(loop,2))>=1)) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)+1),(sp_x(loop,2)))==0) k=k+1; sp_x(k,2)=sp_x(loop,2); sp_x(k,1)=sp_x(loop,1)+1; BW((sp_x(loop,1)+1),(sp_x(loop,2)))=1; end end if ((sp_x(loop,1)-1)>=1 && ((sp_x(loop,2))>=1)) if (sp_x(loop,1)+1 >= batasx) sp_x(loop,1)= sp_x(loop,1)-1; end; if (sp_x(loop,2)+1 >= batasy) sp_x(loop,2)= sp_x(loop,2)-1; end; if (BW((sp_x(loop,1)-1),(sp_x(loop,2)))==0) k=k+1; sp_x(k,2)=sp_x(loop,2); sp_x(k,1)=sp_x(loop,1)-1; BW((sp_x(loop,1)-1),(sp_x(loop,2)))=1; end end if (loop>=prev_size) break; end end % end of while if (prev_size==size(sp_x,1)) break; end end % end of while for loop=1:size(sp_x,1) C(sp_x(loop,1),sp_x(loop,2))=0; end C=uint8(C);
L17
LAMPIRAN FIGURE MATLAB 7.1 APLIKASI SEGMENTASI CITRA SPOT DENGAN MENGGUNAKAN PENDEKATAN FUZZY C-MEANS
L18
LAMPIRAN CITRA
L19
L20
L21
L22
L23