필터 지우기
필터 지우기

Show a ROI over the original image

조회 수: 24 (최근 30일)
Henrique Amaral
Henrique Amaral 2013년 3월 22일
댓글: Image Analyst 2018년 4월 10일
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

채택된 답변

Image Analyst
Image Analyst 2013년 3월 22일
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
Henrique Amaral
Henrique Amaral 2013년 4월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jeff E
Jeff E 2013년 3월 22일
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 2013년 3월 22일
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
Gohan
Gohan 2017년 5월 3일
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 2017년 5월 3일
See my attached masking demos. Adapt as needed. Show your code if you have problems adapting my code.

댓글을 달려면 로그인하십시오.


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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by