color detection object find centre and bounding box

23 views (last 30 days)
I have 1 object (image 1) have 3 color,i found 3 colors, now how to distinguish photo 1 from photo 2 and 3 by color. and find the center of image 1 and the frame only follows image 1

Answers (2)

Image Analyst
Image Analyst on 7 Oct 2022
Use the Color Thresholder on the Apps tab of the tool ribbon. You can export the code from it if you want.
  2 Comments
hoavinh tran
hoavinh tran on 7 Oct 2022
only follow object (image) how?. can you help me?

Sign in to comment.


Image Analyst
Image Analyst on 7 Oct 2022
What I'd do is to take a background image with no car, then convert the background image and car image to HSV color space. Subtract the Saturation images and threshold at 0.5 to get the vivid colors but no shadows. Then call bwareafilt to get the largest blob. Here is a full demo:
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;
%===============================================================================
% Get the name of the background image the user wants to use.
baseFileName = 'track car background.png';
folder = pwd;
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
%=======================================================================================
% Read in demo image.
backgroundImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(backgroundImage)
% Display image.
subplot(2, 2, 1);
imshow(backgroundImage, []);
impixelinfo;
axis on;
caption = sprintf('Background Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Convert to HSV and get the saturation channel so we can just look at the colored things.
hsvImage = rgb2hsv(backgroundImage);
sBackground = hsvImage(:, :, 2);
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'track car.png';
folder = pwd;
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
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Convert to HSV and get the saturation channel so we can just look at the colored things.
hsvImage = rgb2hsv(rgbImage);
sImage = hsvImage(:, :, 2);
% Display image.
subplot(2, 2, 2);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color Image\n%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.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%=======================================================================================
% Subtract the background image to get the car.
% Note we use the Saturation image instead of color or grayscale images because
% we want to ignore any shadows. They are different in the image but they will
% be gray, with a saturation value of 0 so we can easily ignore shadows if
% we threshold at some level that only vivid colors show up.
differenceImage = abs(sImage - sBackground);
% Display image.
subplot(2, 2, 3);
imshow(differenceImage, []);
impixelinfo;
axis on;
caption = sprintf('Difference in the Saturation Images');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%=======================================================================================
% Threshold the image to create a mask
differenceImage = max(differenceImage, [], 3); % Take max value from any color channel.
mask = differenceImage > 0.5;
% Take only the largest blob.
mask = bwareafilt(mask, 1);
% Fill holes
mask = imfill(mask, 'holes');
% Display the mask image.
subplot(2, 2, 4);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize);
drawnow;
%=======================================================================================
% Plot the borders of all the blobs in the overlay above the original background 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.
% 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.
subplot(2, 2, 1);
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('Background image with %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.
%=======================================================================================
% Find the centroid of the vehicle.
props = regionprops(mask, 'Centroid');
xCentroid = props.Centroid(1)
yCentroid = props.Centroid(2)
%=======================================================================================
% Plot a big red cross over the vehicle.
subplot(2, 2, 2);
hold on;
plot(xCentroid, yCentroid, 'r+', 'LineWidth', 2, 'MarkerSize', 200);
message = sprintf('Done!')
uiwait(helpdlg(message));

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by