MATLAB Command Window: 'Coins - PNG'

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

MATLAB Command Window Page 1

>> %TUGAS 2 KOMFISMED (SEGMENTASI IMAGE)


%ABYAN JADIDAN/2106780285
%==========================================================================
%1. MULTITHRESH

%Segment image menjadi dua region

I = imread('coins.png'); %membaca gambar


imshow(I) %menampilkan gambar
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> level = multithresh(I) %menghitung nilai threshold tunggal image

level =

uint8

126

>> seg_I = imquantize(I,level); %Segmentasi image menjadi 2 region


figure %menampilkan bingkai gambar
imshow(seg_I,[]) %menampilkan gambar hasil segmentasi
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> %====================================================
MATLAB Command Window Page 2

%Segment Image into Three Levels Using Two Thresholds

I = imread('circlesBrightDark.png'); %membaca gambar


imshow(I) %menampilkan gambar
axis off %menonaktifkan semua garis axis, tanda, dan label
title('Original Image') %memberikan judul gambar

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> thresh = multithresh(I,2)%menghitung threshold 2 level

thresh =

1×2 uint8 row vector

66 189

>> seg_I = imquantize(I,thresh); %segmentasi gambar menjadi 3 level

RGB = label2rgb(seg_I); %mengkonversi gambar menjadi warna rgb

figure; %menampilkan bingkai gambar


imshow(RGB) %menampilkan gambar hasil segmentasi warna rgb
axis off %menonaktifkan semua garis axis, tanda, dan label
title('RGB Segmented Image') %memberikan judul gambar
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
MATLAB Command Window Page 3

>>
>> %sumber : https://www.mathworks.com/help/images/ref/multithresh.html
>>
>> %===================================================================
>> % 2. HISTOGRAM

%Menghitung Histogram

I = imread('pout.tif'); %membaca gambar grayscale


imshow(I); %menampilkan gambar
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> imhist(I) %Menampilkan Histogram dari gambar tersebut
>> imhist(I) %Menampilkan Histogram dari gambar tersebut
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> %===============================================================
%Display the Histogram of a 3-D Intensity Image

load mristack %Load a 3-D dataset.


>> load mristack %Load a 3-D dataset.
>> imhist(mristack) %Display the histogram of the data
>>
>> %sumber= https://www.mathworks.com/help/images/ref/imhist.html
>>
MATLAB Command Window Page 4

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> %=================================================================
>> % 3. GRAYTHRESH
%(Global image threshold using Otsu's method)

%Convert Intensity Image to Binary Image Using Level Threshold

I = imread('coins.png'); %membaca grayscale image


>> level = graythresh(I) %menghitung threshold

level =

0.4941

>> BW = imbinarize(I,level); %konversi image ke binary image


>> imshowpair(I,BW,'montage') %menampilkan image asli dan binary image
imshowpair(I,BW,'montage') %menampilkan image asli dan binary image
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> %sumber=https://www.mathworks.com/help/images/ref/graythresh.html
MATLAB Command Window Page 1

>> % 4. Segment Image Using Thresholding in Image Segmenter

I = dicomread('knee1'); %membaca file dicom


knee = mat2gray(I); %converts the matrix A to a grayscale image I that
%contains values in the range 0 (black) to 1 (white)

>>
>> %membuka Apps-->image segmenter
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
MATLAB Command Window Page 1

>> load fisheriris


X = meas(:,3:4);

figure;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> %Cluster the data. Specify k = 3 clusters.
rng(1); % For reproducibility
[idx,C] = kmeans(X,3);

%Use kmeans to compute the distance from each centroid to points on a grid
x1 = min(X(:,1)):0.01:max(X(:,1));
x2 = min(X(:,2)):0.01:max(X(:,2));
[x1G,x2G] = meshgrid(x1,x2);
XGrid = [x1G(:),x2G(:)]; % Defines a fine grid on the plot

idx2Region = kmeans(XGrid,3,'MaxIter',1,'Start',C);
MATLAB Command Window Page 2

>> %Warning: Failed to converge in 1 iterations.


% Assigns each node in the grid to the closest centroid

%kmeans displays a warning stating that the algorithm did not converge,
%which you should expect since the software only implemented one iteration.
%Plot the cluster regions.
figure;
gscatter(XGrid(:,1),XGrid(:,2),idx2Region,...
[0,0.75,0.75;0.75,0,0.75;0.75,0.75,0],'..');
hold on;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';
legend('Region 1','Region 2','Region 3','Data','Location','SouthEast');
hold off;

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> %================================================================
>> %Partition Data into Two Clusters
>> %
>> %Randomly generate the sample data
>> rng default; % For reproducibility
X = [randn(100,2)*0.75+ones(100,2);
randn(100,2)*0.5-ones(100,2)];

figure;
plot(X(:,1),X(:,2),'.');
title 'Randomly Generated Data';
>>
>>
>>
MATLAB Command Window Page 3

>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> %Partition the data into two clusters, and choose the best arrangement out of five
initializations. Display the final output.
>> opts = statset('Display','final');
[idx,C] = kmeans(X,2,'Distance','cityblock',...
'Replicates',5,'Options',opts);
Replicate 1, 3 iterations, total sum of distances = 201.533.
Replicate 2, 5 iterations, total sum of distances = 201.533.
Replicate 3, 3 iterations, total sum of distances = 201.533.
Replicate 4, 3 iterations, total sum of distances = 201.533.
Replicate 5, 2 iterations, total sum of distances = 201.533.
Best total sum of distances = 201.533
>> %Plot the clusters and the cluster centroids.
>> figure;
plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)
hold on
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12)
plot(C(:,1),C(:,2),'kx',...
'MarkerSize',15,'LineWidth',3)
legend('Cluster 1','Cluster 2','Centroids',...
'Location','NW')
title 'Cluster Assignments and Centroids'
hold off
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

You might also like