MATLAB Answers

Color based segment ? extract all color object ?

41 views (last 30 days)
Color based segment ? extract all color object ?

Accepted Answer

Image Analyst
Image Analyst on 6 Feb 2021
Try this:
% Demo by Image Analyst, February, 2021.
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;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Read in image.
folder = [];
baseFileName = 'room.png';
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);
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');
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';
% Threshold image.
[mask, maskedRGBImage] = createMask(rgbImage);
% Display masked image.
subplot(2, 2, 2);
imshow(maskedRGBImage, []);
axis('on', 'image');
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display initial mask image.
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
title('Initial Mask', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the biggest blob.
mask = bwareafilt(mask, 1);
% Display mask image.
subplot(2, 2, 4);
imshow(mask, []);
axis('on', 'image');
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
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 06-Feb-2021
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.703;
channel1Max = 0.164;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.246;
% 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;
Image Analyst
Image Analyst on 8 Feb 2021
No. That had thresholds for that image. If your image changes, then what you consider "brown" may change and so the thresholds may need to change. Or they might not. You just have to try it and see. If you don't use the Color Thresholder app then you can just get the histograms of each color channel (H, S, and V) separately and try to guess where to set the thresholds based on those histograms.

Sign in to comment.

More Answers (1)

As the color is always subjective. Considering the simmilar images (image modalities), you may get the respective color models easily in YCBCR color model. Here I have shown the code (hard thresholding) for red object only, hope you can replicate the same for others coloers objects
% Red Object
mask_red=ycbcr_data(:,:,3)>140 & ycbcr_data(:,:,2)<130;
Otherwise, you may have look for Semantic image segmentation using deep learning

Community Treasure Hunt

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

Start Hunting!