image processing-counting the number of objects based on their color
16 次查看(过去 30 天)
显示 更早的评论
Hello, Im kinda new with Image processing. I'm trying to count the number of objects based on one colors and I always end up counting all the objects in my image.
I'm eyeing on counting the number of blue objects in this image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/840130/image.jpeg)
this is my code and i'm still trying to figure out how to isolate the blue color
clear all
%% Read in image
I = imread('toys.jpg');
% imshow(I);
%%Thresholding the image on each color pane
Im=I;
rmat=Im(:,:,1);
gmat=Im(:,:,2);
bmat=Im(:,:,3);
% subplot(2,2,1), imshow(rmat);
% title('Red Plane');
% subplot(2,2,2), imshow(gmat);
% title('Green Plane');
% subplot(2,2,3), imshow(bmat);
% title('Blue Plane');
% subplot(2,2,4), imshow(I);
% title('Original Image');
%%
levelr = graythresh(rmat);
levelg = graythresh(gmat);
levelb = graythresh(bmat);
%instead of manual thresholding, trying Otsu's
%for manual, there's always im2bw(image, level)
i1=im2bw(rmat,levelr);
i2=im2bw(gmat,levelg);
i3=im2bw(bmat,levelb);
Isum = (i1&i2&i3);
Isum1 = (i1|i2|i3);
% Plot the data
% subplot(3,2,1), imshow(i1);
% title('Red Plane');
% subplot(3,2,2), imshow(i2);
% title('Green Plane');
% subplot(3,2,3), imshow(i3);
% title('Blue Plane');
% subplot(3,2,4), imshow(Isum);
% title('Sum of all the planes');
% subplot(3,2,5), imshow(Isum1);
% title('Sum of all the planes - MOD');
%% Complement Image and Fill in holes
Icomp = imcomplement(Isum);
Ifilled = imfill(Icomp,'holes'); % check doc for function; fills holes
% figure, imshow(Ifilled);
%%
se = strel('disk', 4);
Iopenned = imopen(Ifilled,se);
% figure,imshowpair(Iopenned, I, 'blend');
% imshow(Iopenned);
%% Extract featurescentroid
Iregion = regionprops(Iopenned, 'centroid');
[labeled,numObjects] = bwlabel(Iopenned,4); %only used to identify number; labeled wont be reqd as regionprops can use BW as well
stats = regionprops(Iopenned,'Eccentricity','Area','BoundingBox');
areas = [stats.Area];
eccentricities = [stats.Eccentricity];
%% Use feature analysis to count skittles objects
idxOfSkittles = find(eccentricities);
statsDefects = stats(idxOfSkittles);
figure, imshow(I);
hold on;
for idx = 1 : length(idxOfSkittles)
h = rectangle('Position',statsDefects(idx).BoundingBox,'LineWidth',2);
set(h,'EdgeColor',[.75 0 0]);
hold on;
end
title(['There are ', num2str(numObjects), ' blue objects in the image!']);
hold off;
回答(2 个)
yanqi liu
2021-12-21
编辑:yanqi liu
2021-12-21
yes,sir,the base color should be consider,such as
clc; close all;
clear all
%% Read in image
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/840130/image.jpeg');
Im=I;
rmat=Im(:,:,1);
gmat=Im(:,:,2);
bmat=Im(:,:,3);
%%
levelr = graythresh(rmat);
levelg = graythresh(gmat);
levelb = graythresh(bmat);
i1=im2bw(rmat,levelr);
i2=im2bw(gmat,levelg);
i3=im2bw(bmat,levelb);
Isum = (i1&i2&i3);
Isum1 = (i1|i2|i3);
%% Complement Image and Fill in holes
Icomp = imcomplement(Isum);
Ifilled = imfill(Icomp,'holes');
%%
se = strel('disk', 4);
Iopenned = imopen(Ifilled,se);
% figure,imshowpair(Iopenned, I, 'blend');
% imshow(Iopenned);
%% Extract featurescentroid
Iregion = regionprops(Iopenned, 'centroid');
[labeled,numObjects] = bwlabel(Iopenned,4); %only used to identify number; labeled wont be reqd as regionprops can use BW as well
stats = regionprops(Iopenned,'Eccentricity','Area','BoundingBox', 'Centroid');
areas = [stats.Area];
eccentricities = [stats.Eccentricity];
%% Use feature analysis to count skittles objects
idxOfSkittles = find(eccentricities);
statsDefects = stats(idxOfSkittles);
% base color
base = [255 0 0
0 102 0
0 0 255
255 255 0]/255;
counts = zeros(size(base, 1), 1);
figure, imshow(I);
hold on;
for idx = 1 : length(idxOfSkittles)
cen = round(statsDefects(idxOfSkittles(idx)).Centroid);
% get color vector
bwi = labeled==idxOfSkittles(idx);
rm = rmat(bwi); gm = gmat(bwi); bm = bmat(bwi);
color_idx = [mean(double(rm)) mean(double(gm)) mean(double(bm))]/255.0;
% get color info
dis = pdist2(base,color_idx);
[~, ind] = min(dis);
counts(ind) = counts(ind)+1;
% a box only for the blue colored object
if ind == 3
hold on;
h = rectangle('Position',statsDefects(idxOfSkittles(idx)).BoundingBox,'LineWidth',2);
set(h,'EdgeColor',color_idx);
end
end
title(['There are ', num2str(counts(3)), ' blue objects in the image!']);
hold off;
for i = 1 : size(base, 1)
fprintf('\ncolor[%.1f, %.1f, %.1f] has %d objects in the image\n', base(i,1), base(i,2), base(i,3), counts(i));
end
4 个评论
Image Analyst
2021-12-21
@yanqi liu you might like to know about imsplit(), so instead of
rmat=Im(:,:,1);
gmat=Im(:,:,2);
bmat=Im(:,:,3);
you can do
[rmat, gmat, bmat] = imsplit(Im);
Image Analyst
2021-12-21
You can try the Color Threshold to define what colors are "blue" to you. Then just call it to segment the blue regions and pass the blue region mask into regionprops.
% Demo by Image Analyst.
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 = 'toys.jpeg';
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 image.
subplot(2, 2, 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';
%-----------------------------------------------------------------------------------------------------------------------------------
[mask, maskedRGBImage] = createMask(rgbImage);
% Find area of things
subplot(2, 2, 2);
props = regionprops(mask, 'Area')
allAreas = sort([props.Area], 'Descend')
histogram([props.Area], 100)
% Display the image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Clean up the mask.
% Fill holes
mask = imfill(mask, 'holes');
% Get rid of small things. Looks like things we're interested in are bigger than about 1000.
mask = bwareaopen(mask, 1000);
% Display final mask
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Find the properties of all the blobs.
props = regionprops(mask, 'BoundingBox', 'Centroid', 'Image')
numRegions = length(props)
%-----------------------------------------------------------------------
% Plot the borders of all the blobs in the overlay above the original image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
subplot(2, 2, 4);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Objects Outlined', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(mask);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is y.
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
%-----------------------------------------------------------------------
% Plot bounding boxes over all blue blobs.
for k = 1 : numRegions
thisBB = props(k).BoundingBox;
rectangle('Position', thisBB, 'EdgeColor', 'r', 'LineWidth', 2)
end
%-----------------------------------------------------------------------
% Crop out individual images.
plotRows = ceil(sqrt(numRegions));
figure;
outputFolder = fullfile(pwd, 'Individual Images');
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
for k = 1 : numRegions
thisImage = imcrop(rgbImage, props(k).BoundingBox);
% Get the mask for this cropped image.
thisMask = props(k).Image;
% For some reason, thisMask is one pixel narrower and taller than thisImage. Expand it.
thisMask(end+1, end+1) = false;
% Mask out background and other objects.
thisImage = bsxfun(@times, thisImage, cast(thisMask, 'like', rgbImage)); % R2019b and earlier
% Display this cropped, masked image.
subplot(plotRows, plotRows, k);
imshow(thisImage)
baseFileName = sprintf('Image #%2.2d.png', k);
fullFileName = fullfile(outputFolder, baseFileName);
imwrite(thisImage, fullFileName);
end
msgbox('Done!');
%========================================================================================================
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 20-Dec-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.558;
channel1Max = 0.765;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.251;
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/840190/image.png)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!