can u make black color region in to some other color say red or green in the given image but dont change the white color?

here i have attached the image in which i want green or red color instead of black color region for my academic work. please give me accurate answer.
https://mail.google.com/mail/?ui=2&ik=bcbccaefc4&view=att&th=131d66f90017f805&attid=0.1&disp=inline&realattid=f_grfxa06t0&zw
thanks in advance

9 Comments

I can't access the image. Please upload it somewhere else.
https://mail.google.com/mail/?ui=2&ik=bcbccaefc4&view=att&th=131d66f90017f805&attid=0.1&disp=inline&realattid=f_grfxa06t0&zw
copy and paste the above address , we ll get the image. still if you fail to get image let me inform the procedure to upload the image please.
thanks
The above link requieres a google mail account. You can use http://tinypic.com/ to upload pictures.
hello sir, i upload image according to ur suggestion.if u failed to get my image please let me know ur mail id so that i can send to u. bec this very imp to my project work
thanks in advance
If your project is important (for you), take the time to post the link to the image here...
Which format does your picture have? A DOUBLE or UINT8 RGB array? A greyscale image?
Do you want to replace only exact black pixels with the value [0,0,0] or all very dark colors?
@native speakers: Is "please give me accurate answer" polite? Is it inpolite to give an inaccurate answer?
I didn't take it as impolite. I took it as just typical, normal non-native wording like you often see from non-native speakers.
i have binary image sir.i detected some portion from the original image now i want to make black color region to pure red or green color.help me sir.
thanks in advance
Please post more details by editing your original question: Does "binary image" mean a LOGICAL array? If you "detect some portion", in which format is this portion stored?
The more details, the more likely is a meaningful answer. Otherwise we waste time with guessing.
@Image Analyst: Thanks. Converting a message twice by non-native speakers with different mother-tongues leads to strange results, usually.
http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers

Sign in to comment.

 Accepted Answer

OK, you say in a comment that you already have the binary image and you want the black parts of it to be red (or green). Something like this should do it:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Make the black parts pure red.
redChannel(~binaryImage) = 255;
greenChannel (~binaryImage) = 0;
blueChannel (~binaryImage) = 0;
% Make the above lines 0, 255, and 0 if you want green instead.
% Now recombine to form the output image.
rgbImageOut = cat(3, redChannel, greenChannel, blueChannel);

1 Comment

thanks a lot sir. i executed your code sir but its working for rgb images+binary.but i want only for binary image.
i have binary image and in that i want to make black region into red or green color sir. please advice me.

Sign in to comment.

More Answers (4)

If you have an UINT8 RGB image and want to replace [0,0,0] pixels:
rgb = uint8(floor(rand(640, 480, 3) * 10));
isBlack = all(rgb == uint8(0), 3);
r = rgb(:, :, 1);
r(isBlack) = 1;
g = rgb(:, :, 2);
g(isBlack) = 0;
b = rgb(:, :, 3);
b(isBlack) = 0;
rgb2 = cat(3, r, g, b);
While there are faster and better ways ...
x = rand(100, 100, 3);
x(1:10, 1:10, :) = 0;
y = logical([100, 100]);
z = x;
for i = 1:100
for j = 1:100
y(i, j) = all(x(i, j, :) == [0, 0, 0]);
end
end
figure
image(x)
figure
image(z)

1 Comment

"all(x(i, j) == [0,0,0])" is not correct. This checks only the red channel of the 3D image array. I assume you want: "all(x(i, j, :) == [0,0,0])". But then a comparison with a scalar 0 is enough and faster. Vectorization is efficient here: replace the loops by: "all(x==0, 3)".

Sign in to comment.

See my color segmentation demos: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862. With the proper method of segmentation you should be able to achieve the color replacement you want, regardless if the color is a single [r,g,b] triplet or a more spread out gamut.
rgb = uint8(rand(5,5,3)<.25)
rgb(rgb~=0) = uint8(randi([1 255],nnz(rgb),1))
bk = im2uint8(all(rgb==0,3))
rgb2 = rgb;
choose one from variants
% red
rgb2(:,:,1) = bk + rgb2(:,:,1)
% green
rgb2(:,:,2) = bk + rgb2(:,:,2)
% blue
rgb2(:,:,3) = bk + rgb2(:,:,3)
OR
rgb = +(rand(5,5,3)<.25)
rgb(rgb~=0) = rand(nnz(rgb),1)
bk = all(rgb==0,3)
rgb2 = rgb
rgb2(:,:,1) = rgb2(:,:,1)+bk
image(rgb2) % red
ADD MORE (about binary image)
d = +(rand(10)<.3) %binary image
d1 = d(:,:,[ 1 1 1])
d1(:,:,1)=ones(size(d)) % red
image(d1)

1 Comment

thanks a lot sir. ur code working for binary image and got the exact result what i expected.
once again thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!