how to search for a red circle in an image?
8 views (last 30 days)
Show older comments
Shoval Matzner
on 7 Dec 2019
Answered: Shoval Matzner
on 7 Dec 2019
so i need to detect the red circle in a speed limit sign image, can someone suggest me an easy way to do that? the program will return the borders of the red circle
Note that i cant use any stored functions that matlab has..
thanks
an example for a pic you can find here -> https://4.imimg.com/data4/RU/BH/MY-24696381/traffic-sign-boards-500x500.jpg
0 Comments
Accepted Answer
Image Analyst
on 7 Dec 2019
Edited: Image Analyst
on 7 Dec 2019
Sorry, I can't do it without using any built-in functions, though the functions I use are pretty low level, like imread(), imshow(), bwboundaries(), etc.
Try this:
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;
%=======================================================================================
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
% startingFolder = pwd; % or 'C:\wherever';
% if ~exist(startingFolder, 'dir')
% % If that folder doesn't exist, just start in the current folder.
% startingFolder = pwd;
% end
% % Get the name of the file that the user wants to use.
% defaultFileName = fullfile(startingFolder, 'n*.*');
% [baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
% if baseFileName == 0
% % User clicked the Cancel button.
% return;
% end
folder = pwd;
baseFileName = 'traffic-sign-boards-500x500.jpg';
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName);
% rgbImage = decorrstretch(rgbImage,'tol',0.01);
% imwrite(rgbImage, 'test.png');
% Get the dimensions of the image.
[rows1, columns1, numberOfColorChannels1] = size(rgbImage)
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image\n"%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.
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 Image Analyst', 'NumberTitle', 'Off')
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
mask = redChannel > 128 & greenChannel < 128 & blueChannel < 128;
% Display the mask image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Color Segmentation');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Fill holes, and take the largest blob.
subplot(2, 2, 3);
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1);
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Find the border
boundaries = bwboundaries(mask);
boundaries = cell2mat(boundaries); % Convert from cell to double. Format is [rows, columns], which is [y, x]
% Get x and y coordinates.
xBoundary = boundaries(:, 2);
yBoundary = boundaries(:, 1);
% Display the original image with a blue border around it.
subplot(2, 2, 4);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image\nwith Boundary in Blue');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Plot boundary in blue.
darkGreen = [0, 0.5, 0];
hold on;
plot(xBoundary, yBoundary, 'b-', 'LineWidth', 3);
This works just because your image is pure computer graphics, not a real world scene.
See lots of papers on traffic sign detection here: VisionBib
0 Comments
More Answers (1)
See Also
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!