MATLAB Answers


How can I color a certain pixel value in only a region of interest (ROI) in an intensity image

Asked by M
on 2 Oct 2012

I hope somebody help me through this.I want to choose two arbitrary ROIs in an intensity image and change the color of the pixels of them to green. I want to apply this change not to all pixels which exist in the two ROIs but only to those pixels which exist in the ROIs AND their intensity is 81. Suppose the ROI is a rectangle.


c=[460 460 685 685];

r=[164 460 460 164];




No products are associated with this question.

1 Answer

Answer by Image Analyst
on 4 Oct 2012
 Accepted answer

See this demo:

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.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'moon.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);
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]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') 
uiwait(msgbox('Draw the ROI.  Left click to anchor vertices, then right click inside to finish it up.'));
binaryImage =roipolyold();
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Your ROI', 'FontSize', fontSize);
% Find out where image is exactly 81 gray levels.
image81 = grayImage == 81;
% Mask by ROI to get only 81 pixels inside the ROI.
binaryImage = binaryImage & image81
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Value = 81 AND inside Your ROI', 'FontSize', fontSize);
% Construct the RGB image
redChannel = grayImage;
greenChannel = grayImage;
blueChannel = grayImage;
redChannel(binaryImage) = 0;
greenChannel(binaryImage) = 255;
blueChannel(binaryImage) = 0;
% Display it.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 4);
imshow(rgbImage, []);
title('RGB image with green', 'FontSize', fontSize);

  1 Comment

on 4 Oct 2012

Thanx alot.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!