Jumat, 21 Juli 2017

Matlab BlackBox Uji SVM dengan diawali proses segmentasi dan Invers Citra

Sbb Uji datalatih terhadap sistem SVM yang dibuat:


w=100;
h=100;
wh=w*h;

banyak=552; %ada 552 huruf yang hendak di latih
dataLatih=[];
for i=1:banyak
    AL=['datalatih/' num2str(i) '.PNG'];
    I=imread(AL);
    R=double(genData(I));
    RN=imresize(R,[w,h]);
 
    S=reshape(RN,[1,wh]);
    dataLatih(i,:)=S;
    dataTarget(i)=i;
end

% dataLatih       552x10000            44160000  double            
% dataTarget        1x552                  4416  double      

save dataLatih.mat dataLatih;
save dataTarget.mat dataTarget;


for i=1:size(dataLatih,1)
     dataUji=dataLatih(i,:);
     r=multisvmNew(dataLatih,dataTarget,double(dataUji))
end  


hasilnya adalah harus sama urutannnya dengan data Target

Selanjutnya kita akan menguji kalimat :

artinya langkahnya adalah sbb:
1.segmentasi 
2.mencocokan setiap karakter yang telah terbentuk ke sistem SVM
3 manganalsia hasilnya...

sebagaia berikut kodenya:

function btnProcess_Callback(hObject, eventdata, handles)
ff=get(handles.axes1,'Userdata');
f=imresize(ff,[400 NaN]);

g=rgb2gray(f);

    
g=medfilt2(g,[3 3]);
    set(handles.figure1,'CurrentAxes',handles.axes4);
    imshow(g);  
    set(handles.axes4,'Userdata',g);
%**********************************
conc=strel('disk',1);
gi=imdilate(g,conc);
ge=imerode(g,conc);            %%%% morphological image processing
gdiff=imsubtract(gi,ge);
gdiff=mat2gray(gdiff);
gdiff=conv2(gdiff,[1 1;1 1]);
gdiff=imadjust(gdiff,[0.5 0.7],[0 1],.1);
B=logical(gdiff);
[a1 b1]=size(B);
    set(handles.figure1,'CurrentAxes',handles.axes5);
    imshow(B);  
    set(handles.axes5,'Userdata',B);

er=imerode(B,strel('line',100,0));
    set(handles.figure1,'CurrentAxes',handles.axes6);
    imshow(er);  
    set(handles.axes6,'Userdata',er);
    
out1=imsubtract(B,er);
F=imfill(out1,'holes');      %%%filling the object
H=bwmorph(F,'thin',1);
H=imerode(H,strel('line',3,90));
    set(handles.figure1,'CurrentAxes',handles.axes7);
    imshow(H);  
    set(handles.axes7,'Userdata',H);
%%
final=bwareaopen(H,floor((a1/15)*(b1/15)));  
final(1:floor(.9*a1),1:2)=1;
final(a1:-1:(a1-20),b1:-1:(b1-2))=1;

    set(handles.figure1,'CurrentAxes',handles.axes8);
    imshow(final);  
    set(handles.axes7,'Userdata',final);
   
    
w1=get(handles.edw,'String');
h1=get(handles.edh,'String');

w=str2num(w1);
h=str2num(h1);
wh=w*h;


load dataLatih.mat;
load dataTarget.mat;

% dataLatih      6x40000            1920000  double              
% dataTarget      1x6                48  double              
% dataUji      1x40000            40000  logical    
  
Iprops=regionprops(final,'BoundingBox','Image');
hold on
 mm=1;
for n=1:size(Iprops,1)
    PPN=Iprops(n);
    IM=PPN.Image;
    IB=PPN.BoundingBox;
    rectangle('Position',PPN.BoundingBox,'EdgeColor','g','LineWidth',2); 
    
    wv=size(IM,1);
    hv=size(IM,2);
    whv=wv*hv;
   
   if whv>20000
        %figure     
        mm
        Citra=imresize(IM,[w,h]);
        ICitra=imcomplement(Citra);
        ALB=['datacapture/H' num2str(mm) '.png'];
        ALW=['datacapture/H' num2str(mm) '.png'];
        imwrite(Citra,ALB);
        imwrite(ICitra,ALW);
        dataUji=reshape(ICitra,[1,wh]);     
            %         whos dataLatih
            %         whos dataTarget
            %         whos dataUji
        r=multisvmNew(dataLatih,dataTarget,double(dataUji))
        mm=mm+1;
    end
end
hold off

Akan menghasilkan 3 gambar hasil segmentasi sbb:


Selanjutnya diinvers gambar tersebuat agar sesuai dengan pola datalatih:

 ICitra=imcomplement(Citra);



adapun engine svm yang bs digunakan adalah sbb:


function [itrfin] = multisvmNew( T,C,test )
%MULTISVM(3.0) classifies the class of given training vector according to the 
% given group and gives us result that which class it belongs.
% We have also to input the testing matrix

%Inputs: T=Training Matrix, C=Group, test=Testing matrix
%Outputs: itrfin=Resultant class(Group,USE ROW VECTOR MATRIX) to which tst set belongs 

%----------------------------------------------------------------------%
% IMPORTANT: DON'T USE THIS PROGRAM FOR CLASS LESS THAN 3,             %
%            OTHERWISE USE svmtrain,svmclassify DIRECTLY or            %
%            add an else condition also for that case in this program. %
%            Modify required data to use Kernel Functions and Plot also%
%----------------------------------------------------------------------%
%                       Date:11-08-2011(DD-MM-YYYY)                    %
% This function for multiclass Support Vector Machine is written by
% ANAND MISHRA (Machine Vision Lab. CEERI, Pilani, India) 
% and this is free to use. email: anand.mishra2k88@gmail.com

% Updated version 2.0 Date:14-10-2011(DD-MM-YYYY)
% Updated version 3.0 Date:04-04-2012(DD-MM-YYYY)

itrind=size(test,1);
itrfin=[];
Cb=C;
Tb=T;
for tempind=1:itrind
    tst=test(tempind,:);
    C=Cb;
    T=Tb;
    u=unique(C);
    N=length(u);
    c4=[];
    c3=[];
    j=1;
    k=1;
    if(N>2)
        itr=1;
        classes=0;
        cond=max(C)-min(C);
        while((classes~=1)&&(itr<=length(u))&& size(C,2)>1 && cond>0)
        %This while loop is the multiclass SVM Trick
            c1=(C==u(itr));
            newClass=c1;
            svmStruct = svmtrain(T,newClass,'kernel_function','rbf');   % I am using rbf kernel function, you must change it also
            classes = svmclassify(svmStruct,tst);
        
            % This is the loop for Reduction of Training Set
            for i=1:size(newClass,2)
                if newClass(1,i)==0;
                    c3(k,:)=T(i,:);
                    k=k+1;
                end
            end
        T=c3;
        c3=[];
        k=1;
        
            % This is the loop for reduction of group
            for i=1:size(newClass,2)
                if newClass(1,i)==0;
                    c4(1,j)=C(1,i);
                    j=j+1;
                end
            end
        C=c4;
        c4=[];
        j=1;
        
        cond=max(C)-min(C); % Condition for avoiding group 
                            %to contain similar type of values 
                            %and the reduce them to process
        
            % This condition can select the particular value of iteration
            % base on classes
            if classes~=1
                itr=itr+1;
            end    
        end
    end

valt=Cb==u(itr); % This logic is used to allow classification
val=Cb(valt==1); % of multiple rows testing matrix
val=unique(val);
itrfin(tempind,:)=val;  
end

end

% Give more suggestions for improving the program.



Tambahan Button:
function btnBrowse_Callback(hObject, eventdata, handles)
[handles.namafile,handles.direktori]=uigetfile({'*.png;*.jpg','file citra(*.bmp,*.jpg)';'*.bmp','file bitmap(*.bmp)';'*.jpg','file jpg(*.jpg)';'*.*','semua file(*.*)'},'buka file citra host/asli');
I=imread(fullfile(handles.direktori,handles.namafile)); 
if size(handles.namafile,2)>0   
    set(handles.btnReset,'Enable','on');
    set(handles.btnProcess,'Enable','on');
    set(handles.figure1,'CurrentAxes',handles.axes1);
    set(imshow(I));  
    set(handles.axes1,'Userdata',I);

    mywidth=int2str(size(I,1));
    myheight=int2str(size(I,2));
    mysize=(['size image:',mywidth,'x',myheight]);
    mypath=(['path image:',handles.direktori,handles.namafile]);
    
    [infogambar]=([mypath ' Resolusi:',mysize]);
    set(handles.edPath,'string',infogambar);
else
    set(handles.edPath,'string','');
    set(handles.edProses,'string','');
    set(handles.edResult,'string','00 0000 00');
    return;
end



% --- Executes on button press in btnReset.
function btnReset_Callback(hObject, eventdata, handles)
mau=questdlg(['Reset ' get(handles.figure1,'Name')''],['Anda ingin Reset Aplikasi ini?'],'Ya','Tidak','Ya');
if strcmp(mau,'Tidak')
return;
else
    arrayfun(@cla,findall(0,'type','axes'))
       set(handles.edPath,'string','');
       
    set(handles.edProses,'string','');
    set(handles.edResult,'string','00 0000 00');

    
         axes(handles.axes1);cla;
         axes(handles.axes4);cla;
         set(handles.btnReset,'Enable','off');%on
         set(handles.btnProcess,'Enable','off');%on
    hm = msgbox('Reset','Reset Sukses','help');
end


% --- Executes on button press in btnClose.
function btnClose_Callback(hObject, eventdata, handles)
mau=questdlg(['Keluar ' get(handles.figure1,'Name')''],['Keluar ' get(handles.figure1,'Name')''],'Ya','Tidak','Ya');
if strcmp(mau,'Tidak')
return;
end
delete(handles.figure1);%close;


% --- Executes on button press in btnGenerate.
function btnGenerate_Callback(hObject, eventdata, handles)
w1=get(handles.edw,'String');
h1=get(handles.edh,'String');

w=str2num(w1);
h=str2num(h1);
wh=w*h;

banyak=552;
dataLatih=[];
for i=1:banyak
    AL=['datalatih/' num2str(i) '.PNG'];
    I=imread(AL);
    R=double(genData(I));
    RN=imresize(R,[w,h]);
    
    S=reshape(RN,[1,wh]);
    dataLatih(i,:)=S;
    dataTarget(i)=i;
end 

whos dataLatih
save dataLatih.mat dataLatih;
save dataTarget.mat dataTarget;

msgbox('Generate Sukses','Sukses',help);


Selamat Mentjoba....


Literatur Pengujian Black Box


w=100;
h=100;
wh=w*h;

banyak=552;
dataLatih=[];
for i=1:banyak
    AL=['datalatih/' num2str(i) '.PNG'];
    I=imread(AL);
    R=double(genData(I));
    RN=imresize(R,[w,h]);
 
    S=reshape(RN,[1,wh]);
    dataLatih(i,:)=S;
    dataTarget(i)=i;
end

save dataLatih.mat dataLatih;
save dataTarget.mat dataTarget;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load dataLatih.mat
load dataTarget.mat

for i=1:1
    dataUji=dataLatih(58,:);
    r=multisvmNew(dataLatih,dataTarget',double(dataUji))
    %r=multisvm(dataLatih(50:100,:),dataTarget(:,50:100),double(dataLatih(1
    ,:)))
end

%%%%%%%%%%%%%%%%%%%%%%
AL=['datacapture/H1.png'];
I=imread(AL);
    R=double(genData(I));
    RN=imresize(R,[w,h]);
    S=reshape(RN,[1,wh]);
    dataUji=S;
    r=multisvm(dataLatih(50:100,:),dataTarget(:,50:100),double(dataUji))
    r=multisvm(dataLatih(50:100,:),dataTarget(:,50:100),double(dataLatih(70,:)))
 
 
TrainingSet=[ 1 10;2 20;3 30;4 40;5 50;6 66;3 30;4.1 42];
TestSet=[3 34; 1 14; 2.2 25; 6.2 63];
GroupTrain=[1;1;2;2;3;3;2;2];
results = multisvm(TrainingSet, GroupTrain, TestSet);
disp('multi class problem');
disp(results);

%   GroupTrain       8x1                64  double            
%   TestSet          4x2                64  double            
%   TrainingSet      8x2               128  double            

%   GroupTrain       8x1                64  double            
%   TestSet          1x200                64  double            
%   TrainingSet      8x200               128  double

M=5;
TrainingSet=dataLatih(1:552,:);
TestSet=double(dataLatih(M,:));
GroupTrain=dataTarget(:,1:552)';
results = multisvmNew(TrainingSet, GroupTrain, TestSet);
disp('multi class problem');
disp(results);

%   GroupTrain       1b, banyak C                      
%   TestSet          1b, cbebas                        
%   TrainingSet      banyak b , cbebas          


 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%Kasus Just   two group

%figure;
%   Xnew             2x2                32  double            
%   group          100x1             13100  cell              
%   xdata          100x2              1600  double    
% two group
xdata=dataLatih(50:100,:)';
group=dataTarget(:,50:100)';
Xnew=double(dataLatih(5,:))';

svmStruct = svmtrain(xdata,group,'ShowPlot',true);
Xnew = [5 2; 4 1.5];
species = svmclassify(svmStruct,Xnew,'ShowPlot',true)


%%%http://www.codeforge.com/read/11659212/oaasvm.m__html
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
load fisheriris
xdata = meas(51:end,3:4);
group = species(51:end);
%figure;
svmStruct = svmtrain(xdata,group,'ShowPlot',true);
Xnew = [5 2; 4 1.5];
species = svmclassify(svmStruct,Xnew,'ShowPlot',true)
% hold on;
% plot(Xnew(:,1),Xnew(:,2),'ro','MarkerSize',12);
% hold off







Tidak ada komentar:

Posting Komentar