Reputation: 1044
I have a binary image which is the segmented form of another color image .
As you know , a binary image is 2-d but an rgb image is 3-d , how can i multiply them together ?
i tried this code which resulted in a strange pic
function skinCrop(bwSkin,colorSkin)
for i = 1:size(colorSkin,1)
for j = 1:size(colorSkin,1)
if bwSkin(i,j) == 0
colorSkin(i,j,:) = 0;
end
end
end
imshow(colorSkin);
end
The original image was
The resulting image was :
I expected it to be a hand against a dark background , so why do the right part appear that way ?
Upvotes: 1
Views: 4950
Reputation: 114926
A more efficient way to do the multiplication is
function mult = skinCrop( bwSkin, colorSkin )
%
% multiplying 3D color image with 2D mask
%
mult = nsxfun( @times, bwSkin, colorSkin );
% show the result
imshow( mult ); title('skin cropped image');
As @zenopy noted, you might need to cast your variable to double
type.
Upvotes: 0
Reputation: 20136
You should avoid loops when not needed in matlab:
mask = cat(3,bwSkin,bwSkin,bwSkin);
output = mask.*colorSkin;
You might have to change the types in order for the multiplication to succeed:
output = uint8(mask).*colorSkin;
Or:
output = double(mask).*colorSkin;
Upvotes: 2
Reputation: 47082
You are using the wrong dimension length for your second dimension:
for j = 1:size(colorSkin,1)
should be
for j = 1:size(colorSkin,2)
Upvotes: 1