![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/333654/image.png)
How to extract the blob in the middle of a binary image
3 次查看(过去 30 天)
显示 更早的评论
I want to ask how to extract the blob in the middl to measure the area. Because my image has a lot of noise and nonuniform lighting, it is really hard to do so. This is my greyscale image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/333597/image.jpeg)
Here is the code that I used to get the binary image
I = imread("C:\Users\Lenovo\Downloads\Suryajaya_Data2_front\Suryajaya_Data2_front\Suryajaya_Data2_front_ROI465.jpg");
I = adapthisteq(I);
%gmag = imgradient(I);
gmag = imfilter(I, fspecial('average',[5 5]),'replicate');
L = watershed(gmag);
Lrgb = label2rgb(L);
se = strel('disk',50);
Io = imopen(I,se);
Ie = imerode(I,se);
Iobr = imreconstruct(Ie,I);
Ioc = imclose(Io,se);
Iobrd = imdilate(Iobr,se);
Iobrcbr = imreconstruct(imcomplement(Iobrd),imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
fgm = imregionalmax(Iobrcbr);
fgm = not(fgm);
I2 = labeloverlay(I,fgm);
se2 = strel(ones(5,5));
fgm2 = imclose(fgm,se2);
fgm3 = imerode(fgm2,se2);
fgm4 = bwareaopen(fgm3,4500);
I3 = labeloverlay(I,fgm4);
bw = imbinarize(Iobrcbr);
Here is the final binary image(bw)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/333600/image.jpeg)
My guess is that the corners of the image are too dark that it is included in the foreground of the binary image. How do I extract only the blob in the middle?
0 个评论
采纳的回答
Image Analyst
2020-7-18
Use imclearborder(bw). Here is the full demo:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing
fontSize = 15;
I = imread("Suryajaya_Data2_front_ROI465.jpg");
subplot(2, 2, 1);
imshow(I);
I = adapthisteq(I);
subplot(2, 2, 2);
imshow(I, []);
%gmag = imgradient(I);
gmag = imfilter(I, fspecial('average',[5 5]),'replicate');
L = watershed(gmag);
Lrgb = label2rgb(L);
se = strel('disk',50);
Io = imopen(I,se);
Ie = imerode(I,se);
Iobr = imreconstruct(Ie,I);
Ioc = imclose(Io,se);
Iobrd = imdilate(Iobr,se);
Iobrcbr = imreconstruct(imcomplement(Iobrd),imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
fgm = imregionalmax(Iobrcbr);
fgm = not(fgm);
I2 = labeloverlay(I,fgm);
se2 = strel(ones(5,5));
fgm2 = imclose(fgm,se2);
fgm3 = imerode(fgm2,se2);
fgm4 = bwareaopen(fgm3,4500);
I3 = labeloverlay(I,fgm4);
subplot(2, 2, 3);
imshow(I3);
bw = ~imbinarize(Iobrcbr);
% Remove blobs touching the edge of the image.
bw = imclearborder(bw);
% Fill any holes that might be there.
bw = imfill(bw, 'holes');
% Extract only the biggest blob.
bw = bwareafilt(bw, 1);
subplot(2, 2, 4);
imshow(bw);
fprintf('Done running %s.m ...\n', mfilename);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/333654/image.png)
2 个评论
Image Analyst
2020-7-20
You're welcome. Thanks for Accepting this answer. Actually getting a correct segmentation is one of the most common questions in the forum, other than questions about certain errors. https://matlab.fandom.com/wiki/FAQ#Error_Messages
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!