Code covered by the BSD License  

Highlights from
color image segmentation

image thumbnail

color image segmentation

by

 

24 Nov 2010 (Updated )

segment color image robust to texture

cmp_Jmap(img, scale, n_colors)
function [Jmap, q_img] = cmp_Jmap(img, scale, n_colors)
%compute Jmap introduce in [1]
%inputs:
%img: input image
%scale: J-window scale
%n_color: color quanlitized levels
%reference:
%[1]Yining Deng, B.S. Manjunath, Hyundoo Shin, "Color Image Segmentation," 
%   cvpr, vol. 2, pp.2446, 1999 IEEE Computer Society Conference on Computer 
%   Vision and Pattern Recognition (CVPR'99)- Volume 2, 1999
%sample usage:
%complie:
%mex scale_sum.cpp
%img = imread('tiger.jpg');
%Jmap = cmp_Jmap(img, 3, 7);
%subplot(1, 2, 1), imshow(img, []), title("original image");
%subplot(1, 2, 2), imshow(Jmap, []), title("Jmap");
qint_img = rgb2ind(img, n_colors);
q_img = double(qint_img(:));
q_img = reshape(q_img, size(qint_img));

imgHeight = size(q_img, 1);
imgWidth = size(q_img, 2);
hor_idx = 1:imgWidth;
x_map = repmat(hor_idx, [imgHeight, 1]);
vert_idx = [1:imgHeight]';
y_map = repmat(vert_idx, [1, imgWidth]);
tv = 0;
for idx_color=0:n_colors-1
    color_img = (q_img==idx_color);
    color_img = im2double(color_img);
    color_img_size = scale_sum(color_img, scale);
    color_img_x = color_img.*x_map;
    color_img_y = color_img.*y_map;
    color_img_x_square = color_img.*(x_map.^2);
    color_img_y_square = color_img.*(y_map.^2);
    color_img_tvx = cmp_variance(scale_sum(color_img_x, scale), ...
                                 scale_sum(color_img_x_square, scale),...
                                 color_img_size);
    color_img_tvy = cmp_variance(scale_sum(color_img_y, scale), ...
                                 scale_sum(color_img_y_square, scale),...
                                 color_img_size);
    tv = tv+color_img_tvx+color_img_tvy;
end
addpath('CoherenceFilter');
Jmap = im2double(tv)/max(tv(:));
Jmap = CoherenceFilter(Jmap,struct('T',4,'rho',.5,'Scheme','R'));

function v=cmp_variance(x_sum, x_square_sum, x_size)
%comput variance
v = zeros(size(x_sum));
idx_valid = find(x_size > 0);
x_sum_valid = x_sum(idx_valid);
x_square_sum_valid = x_square_sum(idx_valid);
x_size_valid = x_size(idx_valid);
x_mean_valid = x_sum_valid ./ x_size_valid;
var_valid = x_square_sum_valid - 2*x_mean_valid.*x_sum_valid + ...
    x_size_valid.*(x_mean_valid.*x_mean_valid);
v(idx_valid) = var_valid;

Contact us