how can i replace white pixel (in binary image) with a color pixel of another image?

조회 수: 7 (최근 30일)
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)

답변 (3개)

Meghana Dinesh
Meghana Dinesh 2015년 12월 4일
편집: Meghana Dinesh 2015년 12월 4일
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
Meghana Dinesh
Meghana Dinesh 2015년 12월 4일
Or you can use immultiply instead of the .* operator, provided both inputs have same data-type.

댓글을 달려면 로그인하십시오.


Geoff Hayes
Geoff Hayes 2015년 12월 4일
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
Geoff Hayes
Geoff Hayes 2015년 12월 4일
Look closely at your for loops
[m n]=size(b)
for i=1:n
for j=1:m
if (b(i,j)==0)
m is the number of rows and n is the number of columns of b. Notice how i is used to index the row of b and j is used to index the column of b but the upper bound on i is the number of columns in b (and similarly, the upper bound on j is the number of rows of b). So you just have a mismatch and the code should be
for i=1:m
for j=1:n
As an aside, it is good practice to avoid using i and j as indexing variables since MATLAB uses these to represent the imaginary number.
Guillaume
Guillaume 2015년 12월 4일
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) = ...

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2015년 12월 4일
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
Sandeep parajuli
Sandeep parajuli 2020년 7월 3일
can we use same code to separate the background and foreground too?
Image Analyst
Image Analyst 2020년 7월 3일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by