<< Chapter < Page | Chapter >> Page > |
function stopfind(stopcolor,name)
ticstopcolor = imread(stopcolor); % import the picture
stopcolor = imresize(stopcolor,[500,NaN]); % resize the picture
stop = rgb2gray(stopcolor); % make it grayscalestop = edge(stop);
% edge detector to get rid of extraneous elements and convert to 1's and 0'sstop = bwareaopen(stop,30); % get rid of all blobs too small to be important
bounds = regionprops(stop, 'boundingbox'); % for all regions, find their boundscenters = regionprops(stop, 'centroid'); % for all regions, find their centre of mass
areas = regionprops(stop, 'area');numBlobs = length(bounds); % find the number of retions
regions = zeros(numBlobs,4); % create a matrix to hold all the region dataheight = zeros(numBlobs,1);
centerpts = zeros(numBlobs,2);area = zeros(numBlobs,1);
for n = 1:numBlobs %regions(n,:) = bounds(n).BoundingBox; % populate the matrix for each region
height(n) = regions(n,4);centerpts(n,:) = centers(n).Centroid; % populate the center matrix for each blob
area(n,:) = areas(n).Area;end %
numsamecenters=1;for i = 1:numBlobs
for j = i+1:numBlobsif(abs(centerpts(i,1)-centerpts(j,1))+abs(centerpts(i,2)-centerpts(j,2)) 10)
%add a less than symbol in front of the 10 [cnx would not let me import the actual line]samecenters(numsamecenters) = i;
numsamecenters=numsamecenters+1;end
endend
figureaxis ij
imshow(stopcolor)hold on
title(name);for n = 1:numsamecenters-1
samecenter = floor(centerpts(samecenters(n),:));abovept = [samecenter(1),floor(samecenter(2)-height(samecenters(n)))];
belowpt = [samecenter(1),floor(samecenter(2)+height(samecenters(n)))];
rightpt = [samecenter(1)-floor(regions(samecenters(n),3)/3),samecenter(2)];
left_pt = [samecenter(1)+floor(regions(samecenters(n),3)/3),samecenter(2)];
if checkColor2(abovept(1),abovept(2),stopcolor)if checkColor2(belowpt(1),belowpt(2),stopcolor)
plot(samecenter(1)-regions(samecenters(n),3)/2,samecenter(2),'go',...'markersize',4.5*regions(samecenters(n),3))
endendend
hold offtoc
end
function findAll
% Import the Templates%--------------------------------------------------------------------------
S = imread('Templates/S.jpg'); % S templateT = imread('Templates/T.jpg'); % T template
O = imread('Templates/O.jpg'); % O templateP = imread('Templates/P.jpg'); % P template
STOP_W = imread('Templates/STOP_W.jpg'); % stop sign with white backgroundSTOP_B = imread('Templates/STOP_B.jpg'); % stop sign with black background
SPEED = imread('Templates/SPEED.jpg'); % speed limit signONEWAY = imread('Templates/ONEWAY.jpg'); % one way sign
DNE = imread('Templates/DNE.jpg'); % do not enter sign%--------------------------------------------------------------------------
for k = 1:3sign = imread(strcat('Do_Not_Enter/',num2str(k),'.jpg')); % import the picturesign1 = rgb2gray(sign); % make it grayscale
sign1 = edge(sign1); % edge detector to get rid of extraneous elements and convert to 1's and 0'ssign1 = bwareaopen(sign1,30); % get rid of all blobs too small to be important
bounds = regionprops(sign1, 'boundingbox'); % for all regions, find their boundsnumBlobs = size(bounds); % find the number of regions
numBlobs = numBlobs(1); % make that number a handy variableregions = zeros(numBlobs,4); % create a matrix to hold all the region datafor n = 1:numBlobs %
regions(n,:) = bounds(n).BoundingBox; % populate the matrix for each regionend
checkS = zeros(1,numBlobs); % matrix holding the correlations of each blob with the S templatecheckT = zeros(1,numBlobs); % ditto for the T template
checkO = zeros(1,numBlobs); % ditto for the O templatecheckP = zeros(1,numBlobs); % ditto for the P template
checkSTOP_W = zeros(1,numBlobs); % ditto for the white stop sign templatecheckSTOP_B = zeros(1,numBlobs); % ditto for the black stop sign template
checkSPEED = zeros(1,numBlobs); % ditto for the speed limit templatecheckONEWAY = zeros(1,numBlobs); % ditto for the one way template
checkDNE = zeros(1,numBlobs); % ditto for the do not enter templatefor n = 1:numBlobs
cropped = imcrop(sign, regions(n,:)); % choose one of the regions and crop itcropped = imresize(cropped, [57 35]); % resize the image for correlation testingcheckR = corr2(cropped(:,:,1), S(:,:,1)); % check red correlation between cropped image and S templatecheckG = corr2(cropped(:,:,2), S(:,:,2)); % check green correlation between cropped image and S template
checkB = corr2(cropped(:,:,3), S(:,:,3)); % check blue correlation between cropped image and S templatecheckS(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), T(:,:,1)); % red for T template
checkG = corr2(cropped(:,:,2), T(:,:,2)); % green for T templatecheckB = corr2(cropped(:,:,3), T(:,:,3)); % blue for T template
checkT(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), O(:,:,1)); % red for O templatecheckG = corr2(cropped(:,:,2), O(:,:,2)); % green for O template
checkB = corr2(cropped(:,:,3), O(:,:,3)); % blue for O templatecheckO(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), P(:,:,1)); % red for P template
checkG = corr2(cropped(:,:,2), P(:,:,2)); % green for P templatecheckB = corr2(cropped(:,:,3), P(:,:,3)); % blue for S template
checkP(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), STOP_W(:,:,1)); % red for white stop sign templatecheckG = corr2(cropped(:,:,2), STOP_W(:,:,2)); % green for white stop sign template
checkB = corr2(cropped(:,:,3), STOP_W(:,:,3)); % blue for white stop sign templatecheckSTOP_W(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), STOP_B(:,:,1)); % red for black stop sign template
checkG = corr2(cropped(:,:,2), STOP_B(:,:,2)); % green for black stop sign templatecheckB = corr2(cropped(:,:,3), STOP_B(:,:,3)); % blue for black stop sign template
checkSTOP_B(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), SPEED(:,:,1)); % red for speed limit templatecheckG = corr2(cropped(:,:,2), SPEED(:,:,2)); % green for speed limlit template
checkB = corr2(cropped(:,:,3), SPEED(:,:,3)); % blue for speed limit templatecheckSPEED(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), ONEWAY(:,:,1)); % red for one way template
checkG = corr2(cropped(:,:,2), ONEWAY(:,:,2)); % green for one way templatecheckB = corr2(cropped(:,:,3), ONEWAY(:,:,3)); % blue for one way template
checkONEWAY(1,n) = (checkR + checkG + checkB)/3; % sum the correlationscheckR = corr2(cropped(:,:,1), DNE(:,:,1)); % red for one way templatecheckG = corr2(cropped(:,:,2), DNE(:,:,2)); % green for one way template
checkB = corr2(cropped(:,:,3), DNE(:,:,3)); % blue for one way templatecheckDNE(1,n) = (checkR + checkG + checkB)/3; % sum the correlations
end% Nice display (uncomment all)
%fprintf(strcat(num2str(k),':\n'));%fprintf(strcat('S: ', num2str(max(checkS)), '\n'));
%fprintf(strcat('T: ', num2str(max(checkT)), '\n'));%fprintf(strcat('O: ', num2str(max(checkO)), '\n'));
%fprintf(strcat('P: ', num2str(max(checkP)), '\n'));%fprintf(strcat('STOP_W: ', num2str(max(checkSTOP_W)), '\n'));
%fprintf(strcat('STOP_B: ', num2str(max(checkSTOP_B)), '\n'));%fprintf(strcat('SPEED: ', num2str(max(checkSPEED)), '\n'));
%fprintf(strcat('ONEWAY: ', num2str(max(checkONEWAY)), '\n'));%fprintf(strcat('DNE: ', num2str(max(checkDNE)), '\n\n'));% Useful display for copying and pasting into a table (uncomment one line at a time)
%fprintf(strcat(num2str(max(checkS)), '\n'));%fprintf(strcat(num2str(max(checkT)), '\n'));
%fprintf(strcat(num2str(max(checkO)), '\n'));%fprintf(strcat(num2str(max(checkP)), '\n'));
%fprintf(strcat(num2str(max(checkSTOP_W)), '\n'));%fprintf(strcat(num2str(max(checkSTOP_B)), '\n'));
%fprintf(strcat(num2str(max(checkSPEED)), '\n'));%fprintf(strcat(num2str(max(checkONEWAY)), '\n'));
%fprintf(strcat(num2str(max(checkDNE)), '\n'));endend
Notification Switch
Would you like to follow the 'Street sign detection with template matching and edge detection methods' conversation and receive update notifications?