ตัวอย่างโค๊ด Digital image Processing ด้วย Matlab
การดึงวัถตุออกจากพื้นหลัง โดยใช้ Morphology Method
clc,
clear all,
close all,
im = imread(‘K.jpg’);figure, imshow(im)
imgray = rgb2gray(im);
figure, imshow(imgray), IMPIXELINFO
imhisto = histeq(imgray);
figure, imshow(imhisto)
imedge = edge(imgray,’canny’);
figure, imshow(imedge)
se = strel(‘line’,10,5);
bw2 = imdilate(imedge,se);
figure, imshow(bw2), title(‘Dilated’)
BW5 = imfill(bw2,’holes’);
figure, imshow(BW5), title(‘Filled’)
se = strel(‘disk’,8,8);
bw2 = imerode(BW5,se);
figure, imshow(bw2), title(‘Erode’)
นับจำนวนวัถตุในภาพ โดยใช้ bwlabel
clc,close all,
clear all,
im = imread(‘image.jpg’);
% step 1: แปลงค่า RGB ไปเป็น Gray Scale %%%%%%%%%%%%%%%%%%%%%
imgray = rgb2gray(im);
figure, imshow(imgray),impixelinfo;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure, imshow(imgray(1:686,1:400));
figure, imshow(imgray(1:686,400:959));
% step 2: Threshold %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[r c] = size(imgray);
for i = 1:r
for j = 1:c
if (imgray(i,j) <= 120)
imgray(i,j) = 255;
else
imgray(i,j) = 0;
end
end
end
figure, imshow(imgray),impixelinfo;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% step 3: ป้อนหลุมให้เต็ม %%%%%%%%%%%%%%%%%%%%%
BW5 = imfill(imgray,’holes’);
figure, imshow(BW5),impixelinfo;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I3 =BW5;
%step 4: Make binary morphology
bwAreaOpenBW = bwareaopen(I3,25); %Morphologically open binary image (remove small objects)
figure, imshow(bwAreaOpenBW) % Display Morphologically open binary image
%%%%%%%step 5: Erode morphology
se = strel(‘square’,5);
erodedBW2 = imerode(bwAreaOpenBW,se);
figure, imshow(erodedBW2), title(‘Erode2′)
%step 6: Make binary morphology %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bwAreaOpenBW = bwareaopen(erodedBW2,15); %Morphologically open binary image (remove small objects)
figure, imshow(bwAreaOpenBW) % Display Morphologically open binary image
figure, imshow(bwAreaOpenBW(1:686,1:400));
figure, imshow(bwAreaOpenBW(1:686,400:959));
%% step 7: Label and count connected components
[L Ne]=bwlabel(double(bwAreaOpenBW));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Comments
Post a Comment