Show a ROI over the original image
Show older comments
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
More Answers (3)
Jeff E
on 22 Mar 2013
0 votes
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
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
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
on 3 May 2017
See my attached masking demos. Adapt as needed. Show your code if you have problems adapting my code.
Mathijs Dijsselhof
on 10 Apr 2018
0 votes
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
on 10 Apr 2018
grayImage = im2double(VisualPhoto); % Visual photo of skin
binaryImage = ThermalPhoto; % Previously acquired region of interest in thermal photo of skin
binaryImage(isnan(binaryImage))=0; % Converted NaN to 0 in my case
binaryImage = logical(binaryImage); % Converted to logical from intensity
% 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 = double2rgb(ThermalPhoto,jet);
% Scale and convert from double (in the 0-1 range) to uint8.
% My image was already in double
% 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(:,:,1);
outputImageG = grayImage(:,:,2);
outputImageB = grayImage(:,:,3);
% 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);
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
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.
Categories
Find more on Blue 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!