Color based segment ? extract all color object ?

2 ビュー (過去 30 日間)
Selva Karna
Selva Karna 2021 年 2 月 6 日
コメント済み: Image Analyst 2021 年 2 月 8 日
Color based segment ? extract all color object ?
  3 件のコメント
Selva Karna
Selva Karna 2021 年 2 月 6 日
I need to segment brown color region from above image?
Selva Karna
Selva Karna 2021 年 2 月 6 日
any possible to grouping color objec based on color?

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2021 年 2 月 6 日
Try this:
% Demo by Image Analyst, February, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
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);
uiwait(warndlg(errorMessage));
return;
end
end
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');
drawnow;
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');
drawnow;
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');
drawnow;
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');
drawnow;
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;
end
  2 件のコメント
Selva Karna
Selva Karna 2021 年 2 月 8 日
編集済み: Selva Karna 2021 年 2 月 8 日
Thank you @Image Analyst ,i will check ...is it work for all images ? How to set threshold without color threshold apps ? @Image Analyst
Image Analyst
Image Analyst 2021 年 2 月 8 日
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.

サインインしてコメントする。

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 2 月 6 日
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
rgb_data=imread('color_test.png');
temp=rgb_data;
ycbcr_data=rgb2ycbcr(rgb_data);
% Red Object
mask_red=ycbcr_data(:,:,3)>140 & ycbcr_data(:,:,2)<130;
mask_red=cat(3,mask_red,mask_red,mask_red);
red_object=temp.*uint8(mask_red);
imshow(red_object);
Otherwise, you may have look for Semantic image segmentation using deep learning
  1 件のコメント
Selva Karna
Selva Karna 2021 年 2 月 6 日
I need to extract brown region of image ?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by