/
example02_kmeans.m
46 lines (39 loc) · 1.7 KB
/
example02_kmeans.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
%% Example 02: segment the darkest area of an image using kmeans on RGB values
%
% This script segments a target region depending on the image intensity
% with k-means approach applied to the RGB values. Afterwards, the fraction
% of segmented pixels within patches of specificed regions is calculated
% and shown as a quantitative patch-wise fraction map.
%
% Author: Francesco Grussu, <f.grussu@ucl.ac.uk> <francegrussu@gmail.com>
% UCL Institute of Neurology, University College London
% London, United Kingdom
%
% Code developed between January 2013 and July 2016
toolboxdir = fileparts(pwd);
addpath(genpath(toolboxdir));
%% Load data
I = imread('rectangles_color.tif');
mymask = true(size(I,1),size(I,2));
PW = size(I,2); % Patch-width
PH = size(I,1); % Patch-height
nclusters = 5; % There are 5 different colours
nsegm = 10; % Repeat the segmentation 10 times to avoid local minima
%% Segment darkest rectangle
binaryimage = PerformPatchwiseKMeans_NClusters(I,mymask,PW,PH,nclusters,'cityblock',nsegm);
%% Extract patch-by-patch segmented fraction
myPatchWidth = 60; % Patch width in number of pixels
myPatchHeight = 60; % Patch height in number of pixels
[FracNativeRes, FracPathRes] = ExtractPatchwiseSegmentedFraction(binaryimage,ones(size(binaryimage)),myPatchWidth,myPatchHeight); % Get segmented pixel fraction within the patches
%% Plot segmentation
figure,
subplot(3,1,1)
imshow(I)
title('Original image')
subplot(3,1,2)
imshow(cat(3,binaryimage,binaryimage,binaryimage));
title('Segmented image: find the darkest rectangle')
subplot(3,1,3)
imagesc(FracPathRes,[0 1]); axis image; colorbar;
title('Segmented pixel fraction map (patch-wise resolution)')
set(gca,'XTick',[],'YTick',[]);