how can i replace white pixel (in binary image) with a color pixel of another image?
67 views (last 30 days)
i have two images , an RGB image(rgbimage) and it's binary image(b), i wanna know how to replace white pixel in (b) with an RGB pixel at same position as the position of RGB image.
i tried this code:
Meghana Dinesh on 4 Dec 2015
Edited: Meghana Dinesh on 4 Dec 2015
I do this all the time. This is what I do:
mask is the binary image mask (2D) (converted to logical values). rgbImg is the RGB Image (3D). Assuming the number of rows and columns are same in both these matrices:
output (:,:,3) = rgbImg(:,:,3) .* mask;
output (:,:,2) = rgbImg(:,:,2) .* mask;
output (:,:,1) = rgbImg(:,:,1) .* mask;
Geoff Hayes on 4 Dec 2015
bay rem - what are the dimensions of rgbimage and b? Presumably the former is mxnx3 (since RGB) but is the latter two- or three-dimensional? Note that it will have to be converted into three dimensions so that you can copy over the colour pixel from rgbimage.
bIn3Dims = repmat(b,1,1,3);
The above should create a three dimensional equivalent of your binary image. Now, when you find a white pixel
you will copy over the RGB pixel from rgbimage into your new matrix.
Also, why is 0 considered to be white? Isn't 0 usually considered to be black? )That is why I replaced the 0 in the condition with 1.)
Image Analyst on 4 Dec 2015
You can cast b into a color image then mask
mask = b; % Make a copy of b and save it in mask
b = rgbimage; % b is now the original image.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
No double for loop is needed.