www.gusucode.com > IPCV_Eval_Kit_R2019a_0ce6858工具箱matlab程序源码 > IPCV_Eval_Kit_R2019a_0ce6858/code/demo_files/I2_01_3_myGear_7g_J_short.m

    %% 帟幵偺寚娮専弌
%
% 弶婜壔
clc;close all;imtool close all;clear;

%% [夋憸偺撉崬傒]
G = imread('I2_01_3_gear7g.jpg'); % 曄悢愰尵晄梫丅梕堈側懡師尦攝楍庢埖偄
imtool(G);                   % 梕堈側壜帇壔

%% [慜張棟] 擇抣(敀崟)夋憸偵曄姺 (僾儘僢僩->histogram偱妋擣 or imhist(G) )
BW = G > 130;     % 130: 擇師尦偺攝楍偲丄悢抣偺斾妑丅帺摦鑷抣愝掕丗BW=im2bw(G, graythresh(G));     150
imtool(BW);       % 僺僋僙儖抣傪妋擣 (0偲1偺丄2抣夋憸)

%% [僼傿儖僞張棟]僩僢僾僴僢僩 僼傿儖僞張棟偱丄帟偺晹暘傪拪弌 乮尦夋憸 - Opening夋憸乯
BWtoph = imtophat(BW, strel('Disk',30,8));
imshow(BWtoph); shg;

%% [屻張棟] 嵶偐側僲僀僘偺彍嫀
BWclear = bwareaopen(BWtoph, 50); % 50僺僋僙儖埲壓偺傕偺傪嶍彍
imshow(BWclear); shg;

%% [搑拞寢壥偺壜帇壔] 偙偙傑偱偺寢壥傪丄暲傋偰昞帵
figure; imshowpair(BW, BWclear, 'montage');shg;

%% [搑拞寢壥偺夝愅] 俀偮偺夋憸傪廳偹偰昞帵
imshowpair(BW, BWclear);shg;   % 敀丗曄壔側偄晹暘丄椢丗尦偺夋憸偺傒偵懚嵼

%% [寁應] 奺帟偺柺愊偲拞怱揰偺應掕
stats = regionprops('table', BWclear, 'Area', 'Centroid')

%% [僌儔僼壔] 僸僗僩僌儔儉偺昞帵 or 僀儊乕僕偺椞堟夝愅APPS
figure; hist([stats.Area], [1:179]);

%% [寢壥偺壜帇壔]
ind = find([stats.Area] < 100);  % 儀僋僩儖偲悢抣偺斾妑
Gresult2 = insertShape(G, 'Circle', [stats.Centroid(ind,:) 18], 'LineWidth',4, 'Color','red', 'Opacity',1);
Gresult3 = insertText(Gresult2, [10, 10], ['Defect Tooth: indicated by Red Circle'], 'FontSize', 30, 'BoxColor','red', 'BoxOpacity',1);
imshow(Gresult3); shg

%% [廔椆]
%% [儗億乕僩惗惉]

%% Copyright 2014 The MathWorks, Inc.
%        Masa Otobe (masa.otobe@mathworks.co.jp)