Detect background of RGB image
29 views (last 30 days)
Show older comments
Hi,
I have leaf images set with backgrounds. I want to detect background and convert these background pixels to white. Can we do it using matlab.
eg:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159064/image.png)
0 Comments
Answers (2)
Image Analyst
on 18 Dec 2016
You could try a texture filter like stdfilt() and you can assume that the background is blurry and will have low signal. Then threshold. Otherwise you could use imfreehand to trace it out by hand and mask it out like in the attached demo.
4 Comments
Image Analyst
on 20 Dec 2016
How is the algorithm supposed to know which leaf you want to use? You might just as well have decided on the left or right one? So unless there is something special about only the leaves you want to extract, you will have to use imfreehand. I'm attaching a demo for that.
Image Analyst
on 18 Dec 2016
Here's a quick demo I whipped up. Modify as needed.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176139/image.png)
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = '877.png'; % Assign the one on the button that they clicked on.
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale, it's not color.
end
% Display the image.
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Grayscale Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Perform a filter on it.
neighborhood = ones(3); % Look in a square window of this size.
% PLAY AROUND WITH THIS PARAMETER TO OPTIMIZE IT.
filteredImage = stdfilt(grayImage, neighborhood);
subplot(2, 3, 2);
imshow(filteredImage, []);
axis on;
caption = sprintf('Filtered Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Get the histogram of the std image
subplot(2, 3, 3);
histogram(filteredImage);
axis on;
grid on;
caption = sprintf('Histogram of Filtered Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
lowThreshold = 3.5;
% Let the user visually adjust it using
% http://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image
[lowThreshold, highThreshold, lastThresholdedBand] = threshold(lowThreshold, 255, filteredImage)
% Binarize/threshold the image
binaryImage = filteredImage > lowThreshold;
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Take largest blob only.
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 3, 4);
imshow(binaryImage, []);
axis on;
caption = sprintf('Filtered Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(binaryImage, 'like', rgbImage));
subplot(2, 3, 5);
imshow(maskedRgbImage, []);
axis on;
caption = sprintf('Masked Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Take convex hull of image
convexHullMask = bwconvhull(binaryImage);
% Mask the image using bsxfun() function
maskedRgbImage2 = bsxfun(@times, rgbImage, cast(convexHullMask, 'like', rgbImage));
subplot(2, 3, 6);
imshow(maskedRgbImage2, []);
axis on;
caption = sprintf('Masked with Convex Hull');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
10 Comments
Image Analyst
on 18 Jan 2017
That second error occurred because the first error got thrown, causing the rest of the function to not get executed, and that skipped code contained crucial stuff. The first error occurred because you have an old version of MATLAB that does not have the bwareafilt() function in it. You can replace it with the function in the attached file to extract the N largest or smallest blobs. Better yet, upgrade your version of MATLAB.
Malini Bakthavatchalam
on 28 May 2020
@ImageAnalyst: I have a question on the same thing, the above code gives histogram of the filtered image, what if I want to have the histogram of the masked image, and do my further analysis with that?
See Also
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!