fill contour with specified colour

1 view (last 30 days)
UNHAS HASANUDDIN
UNHAS HASANUDDIN on 15 Aug 2011
hy there,, how can i do this code below on RGB image??? can anybody help me please!!! need help immediatly,, thank u so much Gbu^^
here is the code:
How about doing it this way:
% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
% Clean up and initialization
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Get a thresholded, binary image.
binaryImage = grayImage < 80;
% Display the binary image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary "Mask" Image', 'FontSize', fontSize);
% Average it with the image and put into the red channel
averageImage = (256 * single(binaryImage) + single(grayImage))/2;
redChannel = grayImage; % Initialize to same as gray.
% Make it reddish just where the mask is.
redChannel(binaryImage) = averageImage(binaryImage);
% Create the RGB image.
coloredImage = cat(3, redChannel, grayImage, grayImage);
% Display the binary image.
subplot(2, 2, 4);
imshow(coloredImage, []);
title('Colored Image', 'FontSize', fontSize);
  1 Comment
Image Analyst
Image Analyst on 16 Aug 2011
What do you mean by "fill"? That code "tints" the pixels reddish. You can still get an idea of the underlying image. Do you mean that, or do you mean fill the entire mask area with a single solid color?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 15 Aug 2011
Once you get your binary image - your mask - you simply do what it did. Change the red, green, and blue channels and combine them again using cat(3,...) into an RGB image.

Categories

Find more on Image Processing Toolbox 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!