How to change particular pixel color of a binary image to other color?

Sir,
I have a binary image and i want to change the color of the black pixel to green. How do i do that ? Please help. Thanks in advance.
This is the binary image:

 Accepted Answer

Try this:
%grayImage = imread('cameraman.tif');
%binaryImage = grayImage > 128;
%subplot(1,2,1);
%imshow(binaryImage);
redAndBlueChannel = 255 * uint8(binaryImage);
greenChannel = 255 * ones(size(binaryImage), 'uint8'); % Green Everywhere.
rgbImage = cat(3, redAndBlueChannel, greenChannel, redAndBlueChannel);
%subplot(1,2,2);
%imshow(rgbImage);
Remove the % if you want to demo it using a standard MATLAB demo image.

8 Comments

If you want to do it via a colormap instead of creating an RGB image, do it like this:
grayImage = imread('cameraman.tif');
binaryImage = grayImage > 128;
imshow(binaryImage);
myColorMap = [0,1,0;1,1,1]; % 0 = green, 1 = white.
colormap(myColorMap)
colorbar;
Thanks for reply sir but it gives me full green image all i want is that only the vessel line will be green. How do i change it? And it's a binary image where 0 represents vessels..
It does not. You must not have a binary image. Maybe it's a RGB image. Please attach your original binary variable in a .mat file, not the JPG image that you did, which of course is 24 bit color, NOT binary.
Sir,
If i am not wrong it's a binary image and i am attaching the mat file. Waiting for your reply.
MATLAB says this mat file has a structure in it with no fields. Please save the binary image in a mat file and reattach.
save('test.mat', 'binaryImage'); % binaryImage is your binary image.
The quotes are important.
It works just fine. Here's proof:
s = load('binaryimage.mat')
binaryImage = s.show;
subplot(1,2,1);
imshow(binaryImage);
redAndBlueChannel = 255 * uint8(binaryImage);
greenChannel = 255 * ones(size(binaryImage), 'uint8'); % Green Everywhere.
rgbImage = cat(3, redAndBlueChannel, greenChannel, redAndBlueChannel);
subplot(1,2,2);
imshow(rgbImage);

Sign in to comment.

More Answers (1)

Change colormap?
clf
im = round(rand(10));
imshow(im)
colormap([0 1 0; 1 1 1])

Community Treasure Hunt

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

Start Hunting!