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

### M (view profile)

on 2 Oct 2012
Accepted Answer by Image Analyst

### Image Analyst (view profile)

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];

B=roipoly(A,c,r);

## Products

No products are associated with this question.

### Image Analyst (view profile)

Answer by Image Analyst

### Image Analyst (view profile)

on 4 Oct 2012

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);
uiwait(warndlg(errorMessage));
return;
end
end
% 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);
```

M

### M (view profile)

on 4 Oct 2012

Thanx alot.

#### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today