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 want to multiply them together ? i tried this code but an error is generated: function skinCrop(bwSkin,colorSkin)
for i = 1:size(colorImage,1)
for j = 1:size(colorImage,2)
if bwImage(i,j) == 0
colorImage(i,j,:) = 0;
end
end
end
imshow(colorImage);
end
and here there is the error:
Index exceeds matrix dimensions.

3 件のコメント

Jan
Jan 2016 年 3 月 3 日
Please post the complete error message. I guess, that bwImage is smaller than the colorImage?
bay rem
bay rem 2016 年 3 月 3 日
its the complete error,thank you, i'll check the sizes
Sanzhar Askaruly
Sanzhar Askaruly 2018 年 10 月 31 日
I tried your code, it worked, so it seems sizes are the problem.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2016 年 3 月 3 日

1 投票

There are several ways to do what you want, though bsxfun() is the method I usually use (though it's more cryptic than the .* straightforward method). I first learned that from Sean DeWolski of the Mathworks. However, if bwImage is not the same integer type as colorImage, you will have to modify it.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, colorImage, cast(bwImage, 'like', colorImage));
If they're already both the same data type, then having cast in there won't hurt so I always put it in there regardless.

その他の回答 (1 件)

Jan
Jan 2016 年 3 月 3 日

1 投票

if both images have the same number of pixels:
result = colorImage .* cat(3, bwImage, bwImage, bwImage);

7 件のコメント

Adam
Adam 2016 年 3 月 3 日
編集済み: Adam 2016 年 3 月 3 日
Alternatively:
result = colorImage .* repmat( bwImage, [1 1 3] );
or
result = bsxfun( @times, colorImage, bwImage );
I did a quick test of timings for my own interest. Jan's answer and the repmat approach were close to identical and the bsxfun approach, which is what I would tend to use myself if I don't need to optimise it, is faster or slower depending on how big your images are. For a 10*10 image it was ~3 times slower. For a 1000*1000 image it took ~63% of the time of the other two approaches.
It is always useful to know alternative syntaxes to achieve the same result though as in different situations different ones may serve you better (and in Matlab the speed ordering of different approaches like repmat and bsxfun can differ depending on the circumstances).
bay rem
bay rem 2016 年 3 月 3 日
thank you sooooooooo much
swathi
swathi 2018 年 3 月 17 日
Hello Image Analyst, I have tried the same on my color image, to remove the object. It works. But my problem is, I want color image as it is, with the object removed, I mean in place of object, white background should be there. Can you help?
Image Analyst
Image Analyst 2018 年 3 月 17 日
swathi, try this using plus in bsxfun(). Adding 255 to uint8 images will clip them to 255 (white). If you have 16 bit images, use 65535 instead of 255:
rgbImage = imread('peppers.png');
subplot(2, 2, 1);
imshow(rgbImage);
% Make mask
mask = rgbImage(:,:,1) > 160;
subplot(2, 2, 2);
imshow(mask);
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@plus, rgbImage, cast(255*mask, 'like', rgbImage));
subplot(2, 2, 3);
imshow(maskedRgbImage);
swathi
swathi 2018 年 3 月 19 日
Ok.. Thank you. I will try this.
swathi
swathi 2018 年 3 月 19 日
Thank you very much sir. It worked. Can I extend me query a little more, if you don mind(Sorry for it). Can I use this code to remove object from all frames in which it(or part of it) is visible? I need some advice, as I am working 1st time on videos. Thanks in advance

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Convert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by