Mu Az
Mu Az

Reputation: 83

MATLAB overlaying white area of binary image with rgb image

I am using MATLAB 2012b.

I was able to get the objects outline in an image: image
using method of Active Contour Segmentation and the result is in binary mask:

How can I fill the white area of the binary image with the original rgb image?

Basically what I want is to make the background completely black.

Here is my code:

gambarOri = imread(pathGambar);
A = rgb2gray(gambarOri ); 
mask = zeros(size(A)); mask(10:end-10,10:end-10) = 1; 
BW = activecontour(A, mask, 500);
figure, subplot(1, 2, 1), imshow(A), title('Grayscale'); 
subplot(1, 2, 2), imshow(BW), title('Segmented image in Binary');

Upvotes: 2

Views: 1272

Answers (2)

Shai
Shai

Reputation: 114796

You can use :

blackBG = uint8(bsxfun(@times, double(gambarOri), BW));

Upvotes: 0

Erfan
Erfan

Reputation: 1927

You cannon overlay an RGB on a binary image, since the data types do not match. What you can do is to modify your RGB image according to the binary image. For instance you can replace values of the RGB image in false areas of BW with zeros:

% separating RGB channels:
R = gambarOri(:, :, 1);
G = gambarOri(:, :, 2);
B = gambarOri(:, :, 3);
% placing zeros where BW is FALSE:
R(~BW) = 0;
G(~BW) = 0;
B(~BW) = 0;
% concatenate color layers into final result:
blackBG = cat(3, R, G, B);
% figure; imshow(blackBG)

With this result for your provided image: enter image description here

Upvotes: 1

Related Questions