【實踐】數字圖像處理DIP課程課業打卡實驗5 圖像分割


叮嘟!這裏是小啊嗚的學習課程資料整理。好記性不如爛筆頭,今天也是努力進步的一天。一起加油進階吧!
在這裏插入圖片描述

一、實驗目的

1、掌握p參數分割的工作原理和算法實現
2、掌握均勻性度量法分割的工作原理和算法實現

二、實驗內容

1、實現P-參數法的圖像分割的代碼。

測試代碼如下:

Im=imread('yw2_g.jpg'); 
[Im2]=pParam0(Im,0.7974);
imshow(Im2);

(代碼複製到此處)

% Im=imread('yw2_g.jpg' );
% [Im2]=pParam0(Im, 0.7974) ;
% imshow (Im2) ;
 
function Im2=pParam0 (Im, perct)
bestDelta =Inf ;
BestThrd = 0;
[m, n]=size(Im);
for Thrd = 0:255
    ind1=find (Im<=Thrd) ;
    ind2=find (Im> Thrd) ;
    if (~isempty(ind1) && ~isempty (ind2))
        p1 = length(ind1)/ (m*n) ;
        p2 = length(ind2)/ (m*n) ;
        Delta = abs(p2-perct) ;
        if ( Delta < bestDelta );
            BestThrd = Thrd ;
            bestDelta = Delta;
        end
    end
end
Im2= zeros(m, n);
Im2(find(Im>BestThrd))=1;
Im2=logical(Im2);
 
end

代碼效果展示如下:
在這裏插入圖片描述

2、實現均勻性度量法的圖像分割的代碼。

測試代碼如下:

Im=imread('cameraman.tif'); 
[Im2,BestClThrd]=jyxdl(Im);
imshow(Im2);

(代碼複製到此處)

% Im=imread('cameraman.tif'); 
% [Im2,BestClThrd]=jyxdl(Im);
% imshow(Im2);
 
 
function [Im2, BestClThrd]=jyxdl(Im) %% Im mustbe grayScale image
BestCost = Inf ;
BestClThrd = 0;
[m, n]=size(Im);
for ClThrd = 0:255
    ind1=find (Im<=ClThrd);
    ind2=find(Im>ClThrd);
    if (~isempty(ind1) && ~isempty (ind2))
        mu1 = mean (Im(ind1));
        mu2 = mean(Im(ind2));
        sigmal_sq = sum((Im(ind1) -mu1).^2);
        sigma2_sq = sum( (Im(ind2)-mu2).^2);
        p1 = length(ind1)/(m*n);
        p2 = length(ind2)/(m*n);
        Cost = p1*sigmal_sq + p2*sigma2_sq;
        if ( Cost < BestCost )
            BestClThrd = ClThrd ;
            BestCost = Cost;
            %disp (BestC1Thrd);
        end
    end
end
Im2= zeros(m, n);
Im2(find(Im>BestClThrd))=1;
Im2=logical(Im2);
end
 

代碼效果展示如下:
在這裏插入圖片描述

Ending!
更多課程知識學習記錄隨後再來吧!

就醬,嘎啦!

在這裏插入圖片描述

注:
人生在勤,不索何獲。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章