Segmentation processing of MATLAB image processing

fabric = imread('fabric.png');% read image
figure; subplot(121); imshow(fabric), %display
title('fabric');
load regioncoordinates;%download color region coordinates to work space
nColors = 6 ;
sample_regions = false([size(fabric,1) size(fabric,2) nColors]);
for count = 1:nColors
sample_regions(:,:,count) = roipoly(fabric,…
region_coordinates(:,1,count) ,…
Region_coordinates(:,2,count));% select the sample area of ​​each small color
end
subplot(122),
imshow(sample_regions(:,:,2));% show the sample of the red area
title('sample region for red');
cform = makecform('srgb2lab'); %rgb space is converted to L a b space structure
lab_fabric = applycform(fabric, cform); %rgb space is converted to L
a b space
a = lab_fabric(:,:,2); b = lab_fabric(:,:,3);
color_markers = repmat(0, [nColors, 2]);% initialize the color average
for count = 1:nColors
color_markers(count,1 )= mean2(a(sample_regions(:,:,count)));%a mean
color_markers(count,2) = mean2(b(sample_regions(:,:,count)));%b mean
end
disp(sprintf( '[%0.3f,%0.3f]',color_markers(2,1),...
color_markers(2,2)));% shows the mean value of the red component sample
color_labels = 0:nColors-1;
a = double(a) ; b = double(b);
distance = repmat(0,[size(a), nColors]);% initialize distance matrix
for count = 1:nColors
distance(:,:,count) = ((a-color_markers(count ,1)).^2 +…
(b-color_markers(count,2)).^2 ).^0.5;% Calculate the distance to various colors
end
[value, label] = min(distance,[],3 );% Find the color of the minimum distance
label = color_labels(label);
clear value distance;
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;% pixels that are not label colors are set to 0
segmented_images(:,:,:,count) = color;
end
figure;
imshow(segmented_images(:,:,:, 1)),% show background
title('background'); figure;
imshow(segmented_images(:,:,:,2)),% show red target
title('red objects'); figure;
imshow(segmented_images(:, :,:,3)), %Display the green target
title('green objects'); figure,
imshow(segmented_images(:,:,:,4)), %Display the purple target
title('purple objects'); figure,
imshow(segmented_images(:,:,:,5)), %display
magenta target title('magenta objects'); figure,
imshow(segmented_images(:,:,:,6)), %display yellow target
title(' yellow objects');
purple = [119/255 73/255 152/255];
plot_labels = {'k','r','g', purple,'m','y'};
figure
for count = 1 :nColors
plot(a(labelcount-1),b(labelcount-1),'.','MarkerEdgeColor',… plot_labels{count},'MarkerFaceColor',…
plot_labels{count});% display a scatter plot of various colors
hold on;
end
title('Scatterplot of the segmented pixels in''a b '' space');
xlabel('''a*'' values'); ylabel('''b*'' values');
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_38127487/article/details/115259615