Detect background of RGB image

36 次查看(过去 30 天)
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:

回答(2 个)

Image Analyst
Image Analyst 2016-12-18
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 个评论
Malan Jayanka
Malan Jayanka 2016-12-20

What about this kind of image?? I want to select the middle leaf only and others should be removed is it possible?

Image Analyst
Image Analyst 2016-12-20
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
Image Analyst 2016-12-18
Here's a quick demo I whipped up. Modify as needed.
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 个评论
Image Analyst
Image Analyst 2017-1-18
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
@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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by