remove backgroung from the image

Can you help me to remove the backgrond from the image?
this is example of image

 Accepted Answer

Image Analyst
Image Analyst on 14 Apr 2021
Try the Color Thresholder. Use HSV color space and adjust the S slider. Or the V slider. Then tell it to export the code.

7 Comments

This code is for image segmentation from removing background - green channel - clahe - canny. But in this code we still input images one by one And I want the code to immediately read all the images in one folder and save the output in another folder. Can you help me? Please, thankyou in advance
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'image.jpg';
folder = pwd;
fullFileNam
e = 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
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% [rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
figure(1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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')
% drawnow;
[mask, maskedRGBImage] = createMask(rgbImage);
% Extract the largest blob only. That will be the hand.
mask = bwareafilt(mask, 1);
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the image.
figure(2);
imshow(maskedRgbImage, []);
% impixelinfo;
% title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
drawnow;
%green channel
green_channel = maskedRgbImage(:,:,2);
figure(3),imshow(green_channel)
%CLAHE
CLAHE = adapthisteq(green_channel);
figure, fig4=imshow(CLAHE);
%Canny
ER = edge(CLAHE, 'canny');
EG = edge(CLAHE, 'canny');
EB = edge(CLAHE, 'canny');
anyedge = ER | EG | EB;
figure,fig5=imshow(anyedge);
% [n, r] = fboxcount(anyedge,'slope');
% df = -diff(log(n))./diff(log(r));
% %Menampilkan hasil dimensi
% disp(['Df= ' num2str(mean(df(4:8)))]);
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 11-Aug-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.215;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.082;
channel3Max = 0.810;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
Thank you sir, i will try
But sir i want to save all the output automatic
What's not automatic about it? Once you've defined the thresholds, you can apply it to image after image automatically with no further user involvement needed.
save the output of segmentation sir
You can use sprintf() or compose() to generate filenames, and call save() passing in the file name and the name of the variable to save.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!