![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469732/image.png)
Make background of binary image all black
9 views (last 30 days)
Show older comments
Finn Hartley
on 21 Dec 2020
Commented: Finn Hartley
on 23 Dec 2020
Hi,
Im trying to segment an image of an apple such that i can extract the boundary of the apple, but i first need to make all the background of the binary image black, and am not sure on how to do this.
The test photo is this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/467595/image.jpeg)
the resultant output is this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/467600/image.png)
The code:
a = imread('q2.jpg');
b=rgb2gray(a);
bw = imbinarize(b);
bw2 = imcomplement(bw);
c =imfill(bw2,'holes');
imshow(c)
0 Comments
Accepted Answer
Image Analyst
on 23 Dec 2020
Try segmenting based on saturation and then calling bwboundaries():
% Demo by Image Analyst, December, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'apple.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(1, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Mask to find the apple.
hsvImage = rgb2hsv(rgbImage);
mask = hsvImage(:,:,2) > 0.3;
% Fill holes.
mask = imfill(mask, 'holes');
% Display the test image full size.
subplot(1, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Plot the borders of all the blobs in the overlay above the original image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
subplot(1, 2, 1);
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(mask);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is x.
plot(x, y, 'g-', 'LineWidth', 3); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469732/image.png)
3 Comments
Image Analyst
on 23 Dec 2020
Try this:
% Demo by Image Analyst, December, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'apple.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Mask to find the apple.
hsvImage = rgb2hsv(rgbImage);
mask = hsvImage(:,:,2) > 0.3;
% Fill holes.
mask = imfill(mask, 'holes');
% Display the test image full size.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Plot the borders of all the blobs in the overlay above the original image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
subplot(2, 2, 1);
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(mask);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is x.
plot(x, y, 'g-', 'LineWidth', 3); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
% Get a standard deviation image
% Convert to gray scale first
grayImage = rgb2gray(rgbImage);
windowSize = 9;
sdImage = stdfilt(grayImage, ones(windowSize));
% Display the image.
subplot(2, 2, 3);
imshow(sdImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Shrink the mask a little to avoid the edge.
se = strel('disk', 5, 0);
sdMask = imerode(mask, se);
sdboundary = bwboundaries(sdMask);
sdboundary = sdboundary{1};
hold on;
plot(sdboundary(:, 2), sdboundary(:, 1), 'r-', 'LineWidth', 3);
% Show the histogram of SD values
subplot(2, 2, 4);
histogram(sdImage(sdMask));
grid on;
caption = sprintf('Standard Deviation in a %d-by-%d Window', windowSize, windowSize);
xlabel(caption, 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
title('Histogram of Standard Deviations', 'FontSize', fontSize);
% Measure the standard deviation in the mask.
meanStdDev = mean(sdImage(sdMask))
% Put a vertical line there.
xline(meanStdDev, 'LineWidth', 2, 'Color', 'r');
caption = sprintf('Histogram of Standard Deviations. Mean = %.3f', meanStdDev);
title(caption, 'FontSize', fontSize);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469827/image.png)
More Answers (1)
Parth Dethaliya
on 23 Dec 2020
This process totally depends upon the specfic image so it is really hard to generalize the method, but i am showing you the approach to tackle such situations.
By running a function "bwareafilt" on the final image you obtained yo may get this results,
% c is your final image (After filling holes)
largest = bwareafilt(c,1); % This command yeilds first largest component
imshow(largest);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469687/image.jpeg)
largest = bwareafilt(c,2); % This command yeilds first two largest components
imshow(largest);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469692/image.jpeg)
So it is obvious that the region you are interested is second largest, now simply subtracting those two results you can achieve the goal.
largest = bwareafilt(c,2) - bwareafilt(c,1) ;
imshow(largest);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/469697/image.jpeg)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!