1. 程式人生 > >【MATLAB】一個簡單的程式實現細胞計數

【MATLAB】一個簡單的程式實現細胞計數

##實現帶UI介面的程式,對細胞計數

首先在建立一個圖形使用者介面,副檔名為.fIg的檔案,在MATlAB命令視窗啟動GUIDE,進行佈局,大致佈局為自己想要的介面,下圖是我自己做的介面(圖一)。可以選中屬性進行編輯 

圖一
圖二

右鍵按鈕進入編輯器,即按鈕1的回撥函式,在函式裡進行編輯  ,

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)
axis on  %關閉座標顯示
global im;%設定全域性變數
[filename, pathname] =uigetfile({'*.Bmp';'*.jpg';'*.*'},'開啟圖片');
str=[pathname filename];
im=imread(str);
axes(handles.axes2);%顯示在axes2位置上
imshow(im);

在進行閾值切割的時候,使用的閾值T是根據全域性閾值計算出來的

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)
global im;
global im1;
T=globalthreshold( im );
im1=im2bw(im,T/255);
axes(handles.axes3);
imshow(im1);

在進行去噪時,剛開始採用的是中值濾波,效果不佳,影響到了計數,後採用了形態學的閉運算進行處理,效果更佳。

中值濾波
形態學閉運算
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)
global im1;
global A;
A=im1;
B=[0 1 0
   1 1 1
   0 1 0];
% B=strel('disk',3);

%膨脹
for i =1:5
    A=imdilate(A,B);
end
%腐蝕
for i =1:6
    A=imerode(A,B);
end
se1=strel('disk',1);%這裡是建立一個半徑為1的平坦型圓盤結構元素
A=imerode(A,se1);
axes(handles.axes4);
imshow(A);

最後進行計數

function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined .in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%約 29個
global A;
[Label, Number]=bwlabel(A,4);
set(handles.edit1,'string',Number);

最終結果計數為31個,與人工數出一致,較為成功

軟體介面