Is there a way to threshold out only the blue elements in this picture?

4 vues (au cours des 30 derniers jours)
Hi folks,
I have an image as attached. Is it possible to only get the blue parts of the image? It is an image made from the subtraction of two other images, so there are some unwanted areas when using k-means segmentation (for example), which I've attached below.

Réponse acceptée

Image Analyst
Image Analyst le 27 Août 2022
Try this:
% Demo by Image Analyst, August, 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 = 'blue stripes.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~isfile(fullFileName)
% 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
fullFileName = fullFileNameOnSearchPath;
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
[r, g, b] = imsplit(rgbImage);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
impixelinfo;
caption = sprintf('RGB 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';
%--------------------------------------------------------------------------------------------------------
% Segment (mask) the image.
[mask, maskedRGBImage] = createMask(rgbImage);
% Display the image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
impixelinfo;
title('Initial Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the image.
subplot(2, 2, 3);
imshow(maskedRGBImage, []);
axis('on', 'image');
impixelinfo;
title('Initial Masked image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Merge nearby blobs to account for noise.
se = strel('disk', 1, 0);
mask = imclose(mask, se);
% Clean up the mask by taking only the largest one.
mask = bwareafilt(mask, 1);
% Fill holes.
mask = imfill(mask, 'holes');
% Display the image.
subplot(2, 2, 4);
imshow(mask, []);
axis('on', 'image');
impixelinfo;
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
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 27-Aug-2022
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.431;
channel1Max = 0.776;
% 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 = 1.000;
% 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 commentaires
Teshan Rezel
Teshan Rezel le 28 Août 2022
Great stuff, thank you! may I please ask why you have specified
se = strel('disk', 1, 0);
the "0" at the end?
Image Analyst
Image Analyst le 28 Août 2022
I just always do. For small structuring elements it doesn't matter much but for larger ones a 0 gives a more accurate, rounder circle than if you specified some number, though it might take longer to computer, but normally it's so fast that I'd rather go for accuracy that speed.

Connectez-vous pour commenter.

Plus de réponses (1)

Abderrahim. B
Abderrahim. B le 26 Août 2022
Hi!
Segmentation based color should work properly in this case. Use the app color thresholder to intercatively segment the image, then export to the workspace and/ or generate a script.
Hope this helps
  1 commentaire
Teshan Rezel
Teshan Rezel le 27 Août 2022
@Issa thank you. I already tried the colour thresholder but unfortunately It doesn't work very well, so I was hoping to find an alternative solution on here...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Image Processing Toolbox dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by