Show a ROI over the original image

30 views (last 30 days)
Hi friends;
I´m using imfreehand to select a region (roi) on a greyscale image in order to show this roi over the greyscale image. The question is...I would like to attribute a colormap for the roi (like to open it using imagesc), however I don´t know how to do that. My code is:
if true
i = imread('cameraman.tif');
imshow(i);
h = imfreehand;
mask = createMask(h);
i(~mask) = NaN;
figure;
image(i);
j = imread('cameraman.tif');
mergeim = imadd(j,i);
figure;
imshow(mergeim);
end

Accepted Answer

Image Analyst
Image Analyst on 22 Mar 2013
Henrique, see the demo code below that I wrote to produce this figure:
% Demo to have the user freehand draw an irregular shape over
% a gray scale image, have it convert that portion to a
% color RGB image defined by a colormap.
clc; % Clear command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
% Now make it smaller so we can show more images.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
drawnow;
title('Original Grayscale Image', 'FontSize', fontSize);
% Display the freehand mask.
subplot(2, 2, 2);
imshow(binaryImage);
axis on;
title('Binary mask of the region', 'FontSize', fontSize);
% Convert the grayscale image to RGB using the jet colormap.
rgbImage = ind2rgb(grayImage, jet(256));
% Scale and convert from double (in the 0-1 range) to uint8.
rgbImage = uint8(255*rgbImage);
% Display the RGB image.
subplot(2, 2, 3);
imshow(rgbImage);
axis on;
title('RGB Image from Jet Colormap', 'FontSize', fontSize);
% Extract the red, green, and blue channels from the color image.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Create a new color channel images for the output.
outputImageR = grayImage;
outputImageG = grayImage;
outputImageB = grayImage;
% Transfer the colored parts.
outputImageR(binaryImage) = redChannel(binaryImage);
outputImageG(binaryImage) = greenChannel(binaryImage);
outputImageB(binaryImage) = blueChannel(binaryImage);
% Convert into an RGB image
outputRGBImage = cat(3, outputImageR, outputImageG, outputImageB);
% Display the output RGB image.
subplot(2, 2, 4);
imshow(outputRGBImage);
axis on;
title('Output RGB Image', 'FontSize', fontSize);
  1 Comment
Henrique Amaral
Henrique Amaral on 4 Apr 2013
Is it possible to draw more than 1 region ? I mean,is it possible to close a region in the imfreehand() function and than draw another ROI?
Regards
Regards.

Sign in to comment.

More Answers (3)

Jeff E
Jeff E on 22 Mar 2013
I'm not sure your reference to imagesc, but this file in the File Exchange does a nice job of producing color overlays: http://www.mathworks.com/matlabcentral/fileexchange/10502-image-overlay

Image Analyst
Image Analyst on 22 Mar 2013
Here's yet another demo, somewhat different, if you're interested:
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 = 20;
% 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, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Image Analysis Demo','numbertitle','off')
message = sprintf('Draw a box');
uiwait(msgbox(message));
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
x = round([p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)])
y = round([p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)]);
hold on
axis manual
plot(x,y)
croppedImage = grayImage(y(1):y(3), x(1):x(2));
% Display the cropped gray scale image.
subplot(2, 2, 2);
imshow(croppedImage, []);
title('Cropped Image', 'FontSize', fontSize);
% Make them color images.
% Use the jet colormap
rgbCroppedImage =uint8(255 * ind2rgb(croppedImage, jet));
% Display the colorized cropped gray scale image.
subplot(2, 2, 3);
imshow(rgbCroppedImage, []);
title('Colorized Cropped Image', 'FontSize', fontSize);
% Make the original image color.
rgbImage = cat(3, grayImage, grayImage, grayImage);
% Insert the colored portion:
rgbImage(y(1):y(3), x(1):x(2), :) = rgbCroppedImage;
% Display the colored gray scale image.
subplot(2, 2, 4);
imshow(rgbImage, []);
title('Colorized Cropped Image Inserted', 'FontSize', fontSize);
  2 Comments
Gohan
Gohan on 3 May 2017
i want to get only ROI part after imposing on original image ..that is except roi,remaining content of original image need to be turned into white or black ...please someone help me
Image Analyst
Image Analyst on 3 May 2017
See my attached masking demos. Adapt as needed. Show your code if you have problems adapting my code.

Sign in to comment.


Mathijs Dijsselhof
Mathijs Dijsselhof on 10 Apr 2018
This looks great! However does this work with a colour image instead of a grayscale image as well? Meaning superimposing two colour images?
  3 Comments
Mathijs Dijsselhof
Mathijs Dijsselhof on 10 Apr 2018
Edited: Mathijs Dijsselhof on 10 Apr 2018
Above my conversion to two colour images
Image showing the two colours
Image Analyst
Image Analyst on 10 Apr 2018
Not sure what the question is. Yes, you can paste one color image over another, if that's what you're asking.

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!