⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 labcolorsegmentation.m

📁 lab颜色模型下的图像分割方法
💻 M
字号:
%Color-Based Segmentation Using the L*a*b* Color Space
%**********STEP********************
%Step 1: Read image

fabric = imread('d:\matlab7\work\pics\2.jpg');
figure(1), imshow(fabric), title('tomato');
%Step 2: Calculate sample colors in L*a*b* color space for each region
load regioncoordinates;
nColors = 6;
%size(fabric,1)=480;size(fabric,1)=640;
%fabric=480X640X3 uint8
sample_regions = false([size(fabric,1) size(fabric,2) nColors]);%创建矩阵480X640X6

for count = 1:nColors
  sample_regions(:,:,count) = roipoly(fabric,region_coordinates(:,1,count),...
                                      region_coordinates(:,2,count));
end

%????region=????
figure,imshow(sample_regions(:,:,2)),title('sample region for red');
%Convert your fabric RGB image into an L*a*b* image using makecform and applycform. 
cform = makecform('srgb2lab');
lab_fabric = applycform(fabric,cform);
%Calculate the mean 'a*' and 'b*' value for each area that you extracted
%with roipoly. These values serve as your color markers in 'a*b*' space. 
a = lab_fabric(:,:,2);
b = lab_fabric(:,:,3);
color_markers = repmat(0, [nColors, 2]);

for count = 1:nColors
  color_markers(count,1) = mean2(a(sample_regions(:,:,count)));
  color_markers(count,2) = mean2(b(sample_regions(:,:,count)));
end
%For example, the average color of the red sample region in 'a*b*' space is
disp(sprintf('[%0.3f,%0.3f]',color_markers(2,1),color_markers(2,2)));
%[198.183,149.714]
%Step 3: Classify each pixel using the nearest neighbor rule
%Each color marker now has an 'a*' and a 'b*' value. You can classify each
%pixel in the lab_fabric image by calculating the Euclidean distance between 
%that pixel and each color marker. The smallest distance will tell you that 
%the pixel most closely matches that color marker. For example, if the distance
%between a pixel and the red color marker is the smallest, then the pixel would 
%be labeled as a red pixel. Create an array that contains your color labels, 
%i.e., 0 = background, 1 = red, 2 = green, 3 = purple, 4 = magenta, and 5 = yellow. 
color_labels = 0:nColors-1;
%Initialize matrices to be used in the nearest neighbor classification.
a = double(a);
b = double(b);
distance = repmat(0,[size(a), nColors]);
%Perform classification
for count = 1:nColors
  distance(:,:,count) = ( (a - color_markers(count,1)).^2 + ...
                      (b - color_markers(count,2)).^2 ).^0.5;
end

[value, label] = min(distance,[],3);
label = color_labels(label);
clear value distance;
%Step 4: Display results of nearest neighbor classification
%The label matrix contains a color label for each pixel in the fabric image.
%Use the label matrix to separate objects in the original fabric image by color. 
rgb_label = repmat(label,[1 1 3]);
segmented_images = repmat(uint8(0),[size(fabric), nColors]);

for count = 1:nColors
  color = fabric;
  color(rgb_label ~= color_labels(count)) = 0;
  segmented_images(:,:,:,count) = color;
end 

figure,imshow(segmented_images(:,:,:,2)), title('red objects');
figure,imshow(segmented_images(:,:,:,3)), title('green objects');
figure,imshow(segmented_images(:,:,:,4)), title('purple objects');
figure,imshow(segmented_images(:,:,:,5)), title('magenta objects');
figure,imshow(segmented_images(:,:,:,6)), title('yellow objects');
%Step 5: Display 'a*' and 'b*' values of the labeled colors.
%You can see how well the nearest neighbor classification separated the different
%color populations by plotting the 'a*' and 'b*' values of pixels that were classified
%into separate colors. For display purposes, label each point with its color label.
purple = [119/255 73/255 152/255];
plot_labels = {'k', 'r', 'g', purple, 'm', 'y'};

figure
for count = 1:nColors
  plot(a(label==count-1),b(label==count-1),'.','MarkerEdgeColor', ...
       plot_labels{count}, 'MarkerFaceColor', plot_labels{count});
  hold on;
end
  
title('Scatterplot of the segmented pixels in ''a*b*'' space');
xlabel('''a*'' values');
ylabel('''b*'' values');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -