MATLAB: Color detected region in an image

digital image processingImage Processing Toolboximage segmentation

Hi all. i m trying to detected some small blobs from this input image
and after processing i want it to look like this
i have processed it using bottom hat filter and i was able to detect the blobs. like this
now i want to color the detected blobs in orignal image as shown in sample output please tell me how can i do it.. here is the code for processing
original = imread('fivTest.png');
figure, imshow(original)
se = strel('disk',12);
tophatFiltered = imbothat(original,se);
figure, imshow(tophatFiltered);
[r c] = size(tophatFiltered);
for i = 1:r
for j = 1:c
if tophatFiltered(i,j) < 60
tophatFiltered(i,j) = 0;
end
end
end
figure, imshow(tophatFiltered);

Best Answer

Why is it called tophatFiltered when you actually used a bottom hat filter? Let's just call it filteredImage. Try this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.

folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).

button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Read in the image
original = imread(fullFileName);
subplot(2, 2, 1);
imshow(original, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
se = strel('disk',12);
filteredImage = imbothat(original,se);
subplot(2, 2, 2);
imshow(filteredImage, []);
title('Filtered Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold the image.
binaryImage = filteredImage > 128 %< 60;
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Make an RGB image. Make individual color channels first.
redChannel = original;
greenChannel = original;
blueChannel = original;
% Now make the image red where the binary image is
redChannel(binaryImage) = 255;
greenChannel(binaryImage) = 0;
blueChannel(binaryImage) = 0;
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the rgb image.
subplot(2, 2, 4);
imshow(rgbImage, []);
title('Original Image with Spots Overlaid', 'FontSize', fontSize, 'Interpreter', 'None');
Related Question