color detected region in an image

3 views (last 30 days)
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);

Accepted Answer

Image Analyst
Image Analyst on 22 Nov 2015
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');
  4 Comments
mehroz irshad
mehroz irshad on 22 Nov 2015
Edited: mehroz irshad on 22 Nov 2015
clc;clear all;
original = rgb2gray(imread('fivTest.png'));
figure, imshow(original)
se = strel('disk',12);
bothatFiltered = imbothat(original,se);
figure, imshow(bothatFiltered);
[r c] = size(bothatFiltered);
for i = 1:r
for j = 1:c
if bothatFiltered(i,j) < 60
bothatFiltered(i,j) = 0;
end
end
end
figure, imshow(bothatFiltered);
red = original;
green = original;
blue = original;
% Now make the image red where the binary image is
red(bothatFiltered) = 0;
green(bothatFiltered) = 255;
blue(bothatFiltered) = 0;
figure, imshow(bothatFiltered);
% Recombine separate color channels into a single, true color RGB image.
rgb = cat(3, red, green, blue);
figure, imshow(rgb);
and the error message is
Subscript indices must either be real positive integers or logicals.
Image Analyst
Image Analyst on 22 Nov 2015
That's because bothatFiltered is a uint8 image not a logical image because you chose not to follow my suggestions, especially the one where I thresholded the image - you chose to do it in a for loop instead. Here's what you should have done.
clc;
clear all;
close all;
fontSize = 18;
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, 'fivTest.png');
% Read in the image
original = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(original);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
original = original(:, :, 2); % Take green channel.
end
subplot(2, 2, 1);
imshow(original, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
se = strel('disk',12);
filteredImage = imbothat(original,se);
subplot(2, 2, 2);
imshow(filteredImage, []);
axis on;
title('Filtered Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold the image.
binaryImage = filteredImage > 60;
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
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, []);
axis on;
title('Original Image with Spots Overlaid', 'FontSize', fontSize, 'Interpreter', 'None');

Sign in to comment.

More Answers (0)

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!