Cholavendhan
Cholavendhan

Reputation: 347

How to display a Gray scale image using boundary defined in another binary image

I have a original gray scale image(I m using mammogram image with labels outside image). I need to remove some objects(Labels) in that image, So i converted that grayscale image to a binary image. Then i followed the answer method provided in How to Select Object with Largest area

Finally i extracted an Object with largest area as binary image. I want that region in gray scale for accessing and segmenting small objects within that. For example. Minor tissues in region and also should detect its edge.

Extracted Binary imageOriginal Gray Scale Image

**

How can i get that separated object region as grayscale image or anyway to get the largest object region from gray scale directly without converting to binary or any other way.?

**

(I am new to matlab. I dono whether i explained it correctly or not. If u cant get, I ll provide more detail)

Upvotes: 1

Views: 1464

Answers (2)

Divakar
Divakar

Reputation: 221594

If I understood you correctly, you are looking to have a gray image with only the biggest blob being highlighted.

Code

img = imread(IMAGE_FILEPATH);
BW = im2bw(img,0.2); %%// 0.2 worked to get a good area for the biggest blob

%%// Biggest blob
[L, num] = bwlabel(BW);
counts = sum(bsxfun(@eq,L(:),1:num));
[~,ind] = max(counts);
BW = (L==ind);

%%// Close the biggest blob
[L,num] = bwlabel( ~BW );
counts = sum(bsxfun(@eq,L(:),1:num));
[~,ind] = max(counts);
BW = ~(L==ind);

%%// Original image with only the biggest blob highlighted
img1 = uint8(255.*bsxfun(@times,im2double(img),BW));

%%// Display input and output images
figure,
subplot(121),imshow(img)
subplot(122),imshow(img1)

Output

enter image description here

Upvotes: 1

rayryeng
rayryeng

Reputation: 104515

If I understand your question correctly, you want to use the binary map and access the corresponding pixel intensities in those regions.

If that's the case, then it's very simple. You can use the binary map to identify the spatial co-ordinates of where you want to access the intensities in the original image. Create a blank image, then copy over these intensities over to the blank image using those spatial co-ordinates.

Here's some sample code that you can play around with.

% Assumptions:
% im - Original image
% bmap - Binary image 

% Where the output image will be stored
outImg = uint8(zeros(size(im)));

% Find locations in the binary image that are white
locWhite = find(bmap == 1);

% Copy over the intensity values from these locations from
% the original image to the output image.
% The output image will only contain those pixels that were white
% in the binary image
outImg(locWhite) = im(locWhite);

% Show the original and the result side by side
figure;
subplot(1,2,1);
imshow(im); title('Original Image');
subplot(1,2,2);
imshow(outImg); title('Extracted Result');

Let me know if this is what you're looking for.

Method #2

As suggested by Rafael in his comments, you can skip using find all together and use logical statements:

outImg = img; 
outImg(~bmap) = 0;

I decided to use find as it less obfuscated for a beginner, even though it is less efficient to do so. Either method will give you the correct result.


Some food for thought

The extracted region that you have in your binary image has several holes. I suspect you would want to grab the entire region without any holes. As such, I would recommend that you fill in these holes before you use the above code. The imfill function from MATLAB works nicely and it accepts binary images as input.

Check out the documentation here: http://www.mathworks.com/help/images/ref/imfill.html

As such, apply imfill on your binary image first, then go ahead and use the above code to do your extraction.

Upvotes: 1

Related Questions