In this post, I would like to discuss about how to remove
background using image Segmentation/thresholding technique.
Image segmentation or thresholding
is the process to separate the
foreground region and the background region in the image. The foreground region
is the area of interest which refers to the area which contains the object. The
background region refers to the region which is outside of the interested
areas. The background region does not contain any important or valid
information. For image segmentation we used color thresholding in l*a*b color
space (depends upon you whatever color you want to use). First we covert RGB
color space to l*a*b color space then we defined the minimum and maximum
threshold value for each channel in l*a*b color space. Then we have created a
mask based on threshold values and convert image to black and white format. The
white pixels in black and white image are restored to RGB color format. Now the
output image will consist of object(s) and the background is removed. The
Figure 1(a) shows input image and figure 1(b) shows black and white image that
we got after performing image segmentation, in this image the white region
indicate area of interests and white region indicate uninterested area that
need to be removed. Figure 1(c) shows that the resultant image with background
removed which will help to find the objects in image very easily.
Description:
Fist we create a separate MATLAB file main.m. Inside main.m we
read the input image and display it as shown in the figure 1(a).
I=imread('download.jpg');
imshow(I);
Then we call the backround_removal.m
function and pass the input image as parameter to the given function. The backround_removal.m function then
return two images bw and rgb, the bw is a binary image in which the white region shows area of
interest and the black region shows uninterested area which we want to remove
as shown in the figure 1(b). The rgb is
a color image which contains only the foreground region as shown in the figure
1(c).
bw, rgb]=backround_removal(I);
figure, imshow(bw);
figure, imshow(rgb);
Then we create another file backround_removal.m. Inside backround_removal.m
we create the function named creat_mask
which will return two output which are binary images and the masked RGB
image.
function
[BW,maskedRGBImage] = createMask(RGB)
Inside this function we convert RGB image to chosen color space.
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
Next Define thresholds
for channel 1 based on histogram settings
channel1Min = 0.392;
channel1Max = 99.927;
Similarly we define
thresholds for channel 2 based on histogram settings
channel2Min = 18.196;
channel2Max = 77.530;
Similarly define
thresholds for channel 3 based on histogram settings
channel3Min = -35.069;
channel3Max = 68.943;
Then we create mask
based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min
) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) &
(I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) &
(I(:,:,3) <= channel3Max);
Then we initialize
output masked image based on input image.
maskedRGBImage = RGB;
Then we set background
pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1
3])) = 0;
Results:
Figure 1(a): Input Image
Figure 1(b): Background Removed Image
Figure 1(c): Binary Image with Background Removed
Full MATLAB Code:
1. main.m
I=imread('download.jpg');
imshow(I);
[bw, rgb]=backround_removal(I);
figure, imshow(bw);
figure, imshow(rgb);
2. background_removal.m
function [BW,maskedRGBImage] = createMask(RGB)
% Convert RGB image to chosen color space
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.392;
channel1Max = 99.927;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 18.196;
channel2Max = 77.530;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -35.069;
channel3Max = 68.943;
% Create mask based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% 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;
No comments:
Post a Comment