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:
close all;
clear all;
clc;
rgbimage=imread('RGBimage.jpg');
whos rgbimage
b=imread('foreg.jpg');
whos b
[m n]=size(b)
for i=1:n
for j=1:m
if (b(i,j)==0)
b(i,j)=rgbimage(i,j)
end
end
end
imshow(b)

Answers (3)

Meghana Dinesh
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;
  1 Comment
Meghana Dinesh
Meghana Dinesh on 4 Dec 2015
Or you can use immultiply instead of the .* operator, provided both inputs have same data-type.

Sign in to comment.


Geoff Hayes
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
if (b(i,j)==1)
bIn3Dims(i,j,:)=rgbimage(i,j,:)
end
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.)
  3 Comments
Guillaume
Guillaume on 4 Dec 2015
The best way to avoid this sort of confusion (is i iterating over the rows or columns?) is to use meaningful names for your variables (as you did for rgbimage). The following is self-documenting because the variable names have meaning:
binaryimage = imread('foreg.jpg');
[height, width] = size(binaryimage);
for column = 1 : width
for row = 1 : height
binaryimage(row, column) = ...

Sign in to comment.


Image Analyst
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.
  12 Comments
Image Analyst
Image Analyst on 3 Jul 2020
Again, not sure what you mean. It would be helpful if you were more exact in your wording.
Case 1: masking with bsxfun(): If the mask was created such that white was foreground and black was background, or vice versa, then yes, bsxfun() can give you each separately.
Case 2: segmenting with Color Thresholder. If you're talking about color thresholding, then using that won't segment into foreground and unless the foreground and background have different ranges of colors, like background is all black or green or blue or whatever, and none of the foreground is. If there is a little bit of one color in the other's region (like a small amount of black in the foreground) then those can be cleaned up with morphological operations like hole filling or size filtering.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!