1 function [New_image, Sorted_modes] = ...
\r
2 Gray_segm (Gray_image, Window_radius, Min_group_size)
\r
3 % Gray_segment - segment a gray-level image according to Meer's algorithm
\r
4 % reads image from the file, finds significant maxima by mean shift algo
\r
5 % (to generalize to the case of 3-d vectors), groups all the pixels
\r
6 % whose color falls within the given interval (with peak being the
\r
7 % center, and Window_radius being the radius),
\r
9 % Min_group_size : need at least that many pixels to qualify for a color
\r
11 Rows = size(Gray_image, 1);
\r
12 Cols = size(Gray_image, 2);
\r
13 Min_image_size = 0.005*Rows*Cols; % to stop when image is small
\r
14 Gray_image = round(Gray_image);
\r
16 % creating a new array - color based
\r
17 Cumulative = sparse(1, max(max(Gray_image))+1);
\r
20 Cumulative(Gray_image(i, j)+1) = Cumulative(Gray_image(i, j)+1)+1;
\r
24 Cumulative(1) = 0; % corresponds to only manually zeroed out regions
\r
25 k = 0; L = length(Cumulative);
\r
26 while sum(Cumulative) > Min_image_size
\r
27 Non_zero_pos = find(Cumulative > 0);
\r
28 if length(Non_zero_pos) == 0
\r
32 Initial = Non_zero_pos(1);
\r
33 [Mode, Number_values] = M_shift1(Cumulative, Window_radius, Initial);
\r
34 Mode = full(round(Mode));
\r
36 if Number_values > Min_group_size% good group
\r
39 Cumulative(max(1, Mode-Window_radius):min(L, Mode+Window_radius)) = ...
\r
40 zeros(size(Cumulative(max(1, Mode-Window_radius): ...
\r
41 min(L, Mode+Window_radius))));
\r
43 Cumulative(max(1,Initial-Window_radius):min(L,Initial+Window_radius)) =...
\r
44 zeros(size(Cumulative(max(1, Initial-Window_radius): ...
\r
45 min(L, Initial+Window_radius))));
\r
49 Sorted_modes = sort(Modes);
\r
50 New_image = zeros(size(Gray_image));
\r
52 To_group = find((Gray_image >= Sorted_modes(i)-Window_radius)...
\r
53 & (Gray_image < Sorted_modes(i)+Window_radius));
\r
54 New_image(To_group) = i;
\r
56 % Gray_image == 0 added to provide for background detection
\r
57 To_zero = find(Gray_image == 0);
\r
58 New_image(To_zero) = 0;
\r