How can I change the segmented colours to another one?
Show older comments
Hi all,
Currently I work on colour segmentation and intend to change to another one. As you can see, the attached picture 1.jpg is the original picture and has two colours, the blue and the gold (pattern) colours. I followed the provided source code from matlab and changed the gold-pattern colour to the red one (or it could be any other colours), see the 2.jpg image attached. However, the blue colour changed due to "masking process" (conversion from RGB to grayscale). I need to change the blue colour to any other colours (e.g. green) as well. So, the output must be green and red colours. How can I do that? Need help please.
Cheers.
function [sample_regions] = ChangeColor(imin)
load regioncoordinates;
nColors = 2;
sample_regions = false([size(imin,1) size(imin,2) nColors]);
for count = 1:nColors
sample_regions(:,:,count) = roipoly(imin,region_coordinates(:,1,count),...
region_coordinates(:,2,count));
end
lab_imin = rgb2lab(imin);
a = lab_imin(:,:,2);
b = lab_imin(:,:,3);
color_markers = zeros([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
color_labels = 0:nColors-1;
a = double(a);
b = double(b);
distance = zeros([size(a), nColors]);
for count = 1:nColors
distance(:,:,count) = ( (a - color_markers(count,1)).^2 + ...
(b - color_markers(count,2)).^2 ).^0.5;
end
[~, label] = min(distance,[],3);
label = color_labels(label);
clear distance;
rgb_label = repmat(label,[1 1 3]);
segmented_images = zeros([size(imin), nColors],'uint8');
for count = 1:nColors
color = imin;
color(rgb_label ~= color_labels(count)) = 0;
segmented_images(:,:,:,count) = color;
end
lappet = segmented_images(:,:,:,1);
grayImage = rgb2gray(lappet);
% Binarize (threshold) the image to find
% where the average is brighter than some threshold value.
thresholdValue = 100; % Change to whatever value you want.
binaryImage = grayImage > thresholdValue;
% Extract the individual red, green, and blue color channels.
redChannel = lappet(:, :, 1);
greenChannel = lappet(:, :, 2);
blueChannel = lappet(:, :, 3);
% Make binary pixels red.
redChannel(binaryImage) = 255;
greenChannel(binaryImage) = 0;
blueChannel(binaryImage) = 0;
% Get RGB image again.
newRGB = cat(3, redChannel, greenChannel, blueChannel);
imshow(newRGB);
Answers (0)
Categories
Find more on Color Segmentation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!