Clear Filters
Clear Filters

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?

2 views (last 30 days)
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
Jan
Jan on 17 Aug 2011
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.
Walter Roberson
Walter Roberson on 17 Aug 2011
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

Image Analyst
Image Analyst on 17 Aug 2011
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
somasekar jalari
somasekar jalari on 18 Aug 2011
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)

Jan
Jan on 17 Aug 2011
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);

Daniel Shub
Daniel Shub on 17 Aug 2011
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
Jan
Jan on 17 Aug 2011
"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.


Image Analyst
Image Analyst on 17 Aug 2011
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.

Andrei Bobrov
Andrei Bobrov on 17 Aug 2011
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)

Community Treasure Hunt

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

Start Hunting!