How to separate pixels with same RGB value in cell array?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
sr9497
il 5 Mar 2022
Commentato: Image Analyst
il 19 Mar 2022
I have an image "example" with different RGB color values. I have identified the RGB values of each pixel and have stored the coordinates, called x and y, in cell arrays of different sizes. I need to find the center of each shape that is created by pixels with a certain RGB value. By storing the values in cells I know how many pixels belong to a certain color area and I can take the mean of the coordinates.
However, one of the RGB values occurs twice in the image, in shapes that are not connected. This means that the center point is not correct. Can my current method be changed so I can correctly identify the center point of both the shapes?
Any help or suggestion will be appreciated.
0 Commenti
Risposta accettata
Image Analyst
il 5 Mar 2022
Why complicate things with a cell array? No way you should do that.
Simply use the ColorThresholder (use HSV color space, it's on the Apps tab of the tool ribbon) to identify yellow, then export the function. Call the function to get the mask, and then call regionprops() to get the centroid.
3 Commenti
Image Analyst
il 19 Mar 2022
See full demo below:
% Demo by Image Analyst to find centroids of blue blobs.
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 = pwd;
baseFileName = 'coloredchips.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 original RGB image.
subplot(2, 1, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('Original RGB Image : "%s"\n%d rows by %d columns', baseFileName, rows, columns);
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';
%-----------------------------------------------------------------------------------------------------------------------------------
% Get the blue chips.
mask = createMask(rgbImage);
% Display the mask image.
subplot(2, 1, 2);
imshow(mask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%-----------------------------------------------------------------------------------------------------------------------------------]
% Measure centroids
props = regionprops(mask, 'Centroid');
xy = vertcat(props.Centroid)
xCentroids = xy(:, 1);
yCentroids = xy(:, 2);
% Display the centroids over the image
hold on;
for k = 1 : length(props)
thisx = xCentroids(k);
thisy = yCentroids(k);
str = sprintf(' Blob #%d at (%.1f, %.1f)', k, thisx, thisy)
plot(thisx, thisy, 'r+', 'LineWidth', 2, 'MarkerSize', 20);
text(thisx, thisy, str, 'HorizontalAlignment','left', 'VerticalAlignment','middle', ...
'FontSize', 18,'FontWeight','bold', 'Color', 'r')
end
hold off;
%====================================================================================================================================
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 19-Mar-2022
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.501;
channel1Max = 0.787;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.754;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.692;
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
Più risposte (0)
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!