Traffic Warning Sign Recognition Matlab Code

Matlab Demo Model 

Figure Below Shows the model to implement the Warning Sign Detection and Recognition Using Matlab (Version 7.5 or Above).

Matlab Demo Model

Matlab Demo Model

Traffic Warning Sign Templates 

Templates used for Matching sign recognition are build in present in Matlab you can access these templates using this command

vipwarningsigns_templates.mat.

There are total 9 templates which are shown as follows :

  • Detection templates are low in resolution to achieve high performance detection speed from video frames and Remember size of frame and size of image extracted from frame should be same.
  • Red pixel is a distinguish characteristic to detect the red traffic sign this demo uses this technique for sign detection.
  • There’s a problem of accuracy during detection so three templates are used for each sign detection which are oriented in different directions to detect the sign.
templates

templates

Detection of  Traffic Sign 

  • For the detection of sign each frame is analysed.
  • First the image is Converted into gray scale image and then threshold is performed.
  • Then by using blob analysis portion  of red part is extracted.
  • Then this extracted part is compared with each template.
  • If the template is matched with extracted part then a rectangle is drawn at the location of extracted part in that frame.
  • In this way rectangle is drawn at all the frames and at the end all frames are played using implay video command.

Recognition and display the Name of Traffic Sign 

  • Now when the template is  matched with extracted blob this is done by using correlation.
  • Each template is matched with extracted blob by correlating each template and extracted part.If there’s exactly or 50% match then a check is assigned that its completely matching with the extracted blob sign.
  • Now template is recognized Now the name of recognized sign is concatenated with the red sign.
  • Image of Sign is Deigned and will be provided With the Source Code.

traffic warning sign recognition

Matlab Code

function main
readerobj = mmreader(‘stoprd.avi‘);
vidFrames = read(readerobj);

numFrames = get(readerobj, ‘numberOfFrames’); %get number of frames
implay(vidFrames)                                        %Play Input Video Files
for k = 1 : numFrames-1
mov(k).cdata = vidFrames(:,:,:,k);      %Extract Frame & save in Struc
mov(k).colormap = [];
mov(k).cdata=blobAnalysis(mov(k).cdata);%Call Blob Analysis Function
vidFrames(:,:,:,k)=mov(k).cdata;           %Save Output Data
end
implay(vidFrames)                                                                                  

  • In the Main Function Video as input is read in readerobj  and then frames are extracted from video input.
  • These frames are passed into blobAnalysis function one by one
  • Output image after processed by blobAnalysis function is saved in vidFrames
  • And At last when all frames are received in vidFrames output video is played using implay which is last command in above code.

 

 Blob Analysis Function 

function x=blobAnalysis(a)
x=a;
a(:,:,1) = medfilt2(a(:,:,1), [3 3]);
a(:,:,2) = medfilt2(a(:,:,2), [3 3]);
a(:,:,3) = medfilt2(a(:,:,3), [3 3]);
[r c d]=size(a);
check=1;
i1=0;j1=0;
i2=0;j2=0;

for i=1:r
for j=1:c

if(a(i,j,1)>125&&a(i,j,2)<90&&a(i,j,3)<90)
if(check)
i1=i;j1=j;
check=0;
else
i2=i;j2=j;
end

end

end

end

i1=i1-2;
if(i1<1) i1=1; end i2=i2+2; if(i2>r)
i2=r;
end

if(c>500)

j1=j1-40;
else
if(r<150)
j1=j1-10;
else
j1=j1-30;
end
end
if(j1<1) j1=1; end j2=j2+20; if(j2>c)
j2=c;
end
i2-i1;

yieldtag=imread(‘yieldtag1.jpg’);
stptag=imread(‘stoptag1.jpg’);
[r1 c1 d1]=size(stptag);
z=a(i1:i2,j1:j2,1:3);

z=rgb2gray(z);
z=im2double(z);
z=im2bw(z,0.5);
[row col]=size(z);
if(row~=0 && col~=0)
z=imresize(z, [18 18], ‘nearest’);
load vipwarningsigns_templates.mat;
for k=1:9
temp=template_recognition(:,:,k);
s(k)=corr2(temp,z);
end
mx=max(s);
ind=1;
for i=1:9
if(s(i)==mx)
ind=i;
end
end
if(isnan(s)==0)
if(i2-i1>20)
if(mx>0.15)
for l1=i1:i2
a(l1,j1,1)=255;
a(l1,j1,2)=255;
a(l1,j1,3)=0;

a(l1,j2,1)=255;
a(l1,j2,2)=255;
a(l1,j2,3)=0;
end

for l2=j1:j2
a(i1,l2,1)=255;
a(i1,l2,2)=255;
a(i1,l2,3)=0;

a(i2,l2,1)=255;
a(i2,l2,2)=255;
a(i2,l2,3)=0;
end
if(r<150)

scalex=i2+r1-1;
scaley=j1+c1-1;
a(i2:scalex,j1:scaley,:)=yieldtag;
else
scalex=i2+r1-1;
scaley=j1-c1+1;
a(i2:scalex,scaley:j1,:)=stptag;
end

end
end
end
end
x=a;

  • Now our frames are coming in variable a and from a to x.
  • Now applying median filter on the frame on all three dimensions that’s red blue and green using MEDFILT2  for smoothing as some frames are blurry.
  • Now doing BLOB ANALYSIS to extract red region from frame by by checking intensity values for red when intensity values for dimention1>125 && dimention2 <90 && dimention3<90 then its mean we are in red region so when loop enter into this red region we note this pixel in variable (i1,j1) and in the same way when loop leave this this red part we note this pixel in variables (i1,i2).
  • Now we know entry and ending points of this region now added a little offset in entry and end red pixel to extract that red part from the frame and now our extracted traffic sign is in variable z.
  • Now reading images that’s tags to display the recognized sign that’s yieldtag and stptag.
  • Now converting the extracted red sign which was in z variable into gray ,double and Thresholding  using RGB2GRAY , IM2DOUBLE , IM2BW Command.
  • Now resizing the extracted image size to the size template for matching using IMRESIZE command.
  • Now loading template sign to match with the extracted sign which is in variable z by this command load vipwarningsigns_templates.mat;
  • Now Reading each template in temp variable and finding correlation of each template with red extracted sign from frame which is in variable z.
  • Now s variable contains all correlation result now find max of correlation  now we found template which is matching with the traffic sign.
  • Now adding a image tag with the recognized sign.
  • Now finally drawing rectangle on the frame and frame is returned

Output

traffic sign recognition video

traffic sign recognition video

traffic sign recognition video Results

traffic sign recognition video Results

Source Code

Like , share this post on Facebook , Twitter by using left moving Social Sharing block and follow us on Facebook Page , Twitter and do comment and enter your email. After you have done you will automatically get source code in your mail. Note : This will be auto generated mail you can’t get until you follow above method.

 

 

 

Advertisement

198 Responses

  1. Vishnu

    Hai Khan,

    Thanks for the help.
    I would like to know more about the solution; especially on how to recognize more specific traffic signs.

    Regards

    Reply
    • ammadkhan

      Yea sure let me know if you need the source code i will send it to you in your mail and to recognize other object you have to make templates of that object but here i am just using Matlab built in templates using this commannd load vipwarningsigns_templates.mat;

      Reply
      • thuong nguyen van

        hi ammadkhan
        do you have an code to detect and track color in live camera? I using matlab
        or do you have any suggestion.
        thank you very much

      • ammadkhan

        In this sign recognition I detected red color if you read the whole article you will get know how to detect red color. In the same way you can detect any color of your choice If you do effort to understand the algorithm. First try this on a video clip instead of testing it on live camera.

      • swati

        hi ammad
        i wanna find out the convex hull of any object as i took a palm picture in my project there was a code that i get from internet for impcon.bmp image when same code i m trying to run for my image it is giving an error of Index exceeds matrix dimensions so please help me to find the convex hull of any binary image using the set of structuring elements …..PLEASE

      • Mehmet Gür

        Hi khan. I need to study on money recognition on matlab. Could you give me your sign recognition’s source code ? And please let me know if you know anything about money recognition on matlab :)

      • Pratiti Doshi

        can you please send me the complete source code asap??

      • kim

        please send me your source code! I want to refer it!
        Thank you so much!

      • Sky high

        hi sir,
        you are working on which version of matlab ,as its not working for MATLABr2008b

      • Libi

        Please could you send me the source code and documentation part to my mail .

      • sadiq

        Hi khan….
        Can you send me the same source code please. I want to use this for gesture recognition which is my research in final year…

      • Mihai

        Hi, can you please help me with the source code for Round Traffic signs?

        Regards

  2. dhinagaran

    u done this project in good manner

    i need to understand it briefly

    could u send this source code ?…

    thanks in advance

    Reply
    • ammadkhan

      Thanks for your Appreciation first i would suggest you to open matlab
      1) click on start on left below go into Blocksets —>Video And Image Processing —->Demo
      2) After this in this category click on Traffic warning sign recognition

      Now in this Demo first understand the whole procedure and then come towards the code section to understand authentically then let me know if you have any problem
      Check your mail for Source code

      Reply
  3. Eli

    Thanks for the source code. There is an error like
    Error using load
    Unable to read file vipwarningsigns_templates.mat: No such file or directory.

    How can I have “vipwarningsigns_templates.mat”?

    Reply
    • ammadkhan

      Use this command to load these files First check this command on console command window to know the variables
      load vipwarningsigns_templates.mat
      who

      variables will be as follows i am copying here OUTPUT OF Above Command

      Name Size Bytes Class Attributes

      template_detection 12x12x3 1728 single
      template_detection_display 12×36 1728 single
      template_ids 1×9 36 single
      template_names 3×12 36 uint8
      template_recognition 18x18x9 11664 single
      template_recognition_display 18×162 11664 single

      Reply
  4. Eli

    But it can not be loaded.
    the error is :
    Error using load
    Unable to read file vipwarningsigns_templates.mat: No such file or directory.
    You mean before running the code I should use this command?

    load vipwarningsigns_templates.mat
    who

    Reply
  5. Thanh

    Do you code processing image for Identify traffic signs using Support Vector Machines ?
    I need document and code.can you help me?
    Thank you very much!

    Reply
    • ammadkhan

      Right Now i am very busy in my Engineering exams but soon i will do some research on Support Vector Machines and make a new tutorial regarding Recognition algorithm

      Reply
  6. minh tien

    Do you code processing image for Identify traffic signs using Gabor Filter ?
    I need document and code. can you help me?
    Thank you very much!

    Reply
    • ammadkhan

      It would take time now a days i am really busy i have to prepare my self for exams you can examine this code this is developed using blob analysis .If you can understand this just alter the code insert your technique and if do you need it ? i will mail you just inform me

      Reply
      • minh tien

        I run main.m but I see only input video. Can you help me to see output video please?

      • ammadkhan

        Both video are there just hold and drag the input video to left or right ouput video is behind it its hidden when you drag input video you will see output video behind it

  7. minh tien

    Please send me source code Traffic Warning Sign Recognition Matlab Code.
    Thank you very much!

    Reply
  8. Nhut Bui

    Could you please send me your project Traffic Warning Sign Recognition Matlab Code. I want to develope it on Android base on your project.
    Thank you so much.

    Reply
      • Nhut Bui

        I received your project. Just wanna say many thanks! I’ll share my Android project base on that to you (if successful)

  9. Shakhil Dhotah

    hi Mr Khan,
    Traffic sign detection and recognition is quite difficult to implement on matlab if one is not really familiar to this programming tool. well , actually part of my thesis is on that, and i will have to use that to make use of traffic speed signs detection. Can you please send me the code please.

    Reply
      • ammadkhan

        IN blob analysis we need to extract red part from the image to see the RGB intensities of image Use IMTOOL command and move your mouse cursor over red area then you will get know intensities and these intensities you will use here
        if(a(i,j,1)>125&&a(i,j,2)<90&&a(i,j,3)<90)
        now when this condition is true this means loop reaches to red part and you will insert a check on start of red part and when this condition becomes false we note these pixels in this we get know starting and ending pixels of red traffic sign which will be in variables
        starting = (i1,j) starting means start of red pixel in image
        ending = (i2,j2) ending means end of red pixel in image

        now we add an offset and insert a rect for more understanding please read all post and try to understand the flow and if you do not understand any logic comment about specific line to understand

      • Hafsah

        I am also working on speed limit sign detection. Can you kindle send me the code please?

      • minh tien

        Please send me some document about Blob Analyzics.
        Thank you so much!

      • ammadkhan

        Minh tien i didn’t studied about any document i just used ( Matlab 7.5 help ) to read about Demo Project Traffic Warning Sign Recognition

      • ammadkhan

        After coorelating with all avaialable templates MX contains the result of coorelation which means that the template which is perfectly matching with extracted frames part.Here in this line we are checking % of match if the result matching template(co-relation) is greater than this value then perform specific operation

    • ammadkhan

      first you have to make template just like template for the stop sign by extracting frame from the video and from frame crop the part which you want to recognize in the .avi video after then use that template in the code and apply the corresponding functions which were applied for the demo traffic warning sign Recognition

      Reply
    • ammadkhan

      Brother that’s totally different logics let’s see what i can do for you give me some to do programming on this problem

      Reply
    • ammadkhan

      Now I want you to tell me what are your requirements

      1)want a matlab code which detects cross walk on a road by inserting a rectangle around it ? please let me know…..

      2) any other ?

      Reply
      • Jon

        1) Yes, just it. Just identify the crosswalk with a rectangle around.

      • Jon

        Hi ammadkhan, did you have some progress in recognizing the crosswalk?

      • ammadkhan

        I recognized crosswalk in an image i developed a program which can recognize crosswalk but input is image now i was converting it into video recognizer i got stucked some where i will mail you that code and soon i will convert it into video recognizer

      • ammadkhan

        And one thing more i forgot to tell you VIDEO you provided is not correct although its .avi format there is some thing a kind of frame size problem i need a crosswalk video .in .avi format with low sized image please search another cross walk video .avi format i need video to complete this

      • Jon

        Hi,

        Did you have any advance to detect crosswalk? Can you show me the code?

        Thanks

      • ammadkhan

        i just detected crosswalk from an image but i think the algorithm is not so efficient it’s not working for the video let me show you the code which i have for the image

  10. Hafsah

    Hello Ammad,
    I have detected a speed limit traffic sign but to recognised it i have to use OCR. My detected image contains some noise along with traffic sign. How can i completely detect the circular sign using “regionprops” command?

    Reply
    • ammadkhan

      you can do blob analysis just like i did blob analysis recognizes round red part and coorelation is used to recognize sign that is coorelate with the template and for noise smooth the image by applying averaging filter

      Reply
  11. minh tien

    Please explain for me why you use if(mx>0.15) in BlobFunction?
    How can you find 0.15?
    Thank you so much!

    Reply
  12. Ryan Wong

    Hi, could you forward me your source code? I’m also experiencing some difficulties on this topic. Thanks!

    Reply
  13. unknownx

    hi, we are also working on traffic sign detection. Can you send us the source code please?

    Reply
      • unknownx

        But we interest in only speed signs. First, we find red areas in the picture or video. Then we want detecting areas which are circle. In this way, we detect the traffic speed sign. Then we will detect amount of speed with using character recognition.

      • ammadkhan

        Yes you are at right place to find the red areas you have to do analysis to find that red part so i am sending you the source code in which used the same technique of detecting red areas which are in circle you can modify this source code but i want you to keep me updating about your project and also after completing this we will be thankful if you share this with us thanks keep visiting and if you want to keep your self upto date join this page https://www.facebook.com/MatlabProgramming @crunchmodo

      • unknownx

        thanks a lot :) we already found red areas before your project. Is this area circle, we couldn’t. Metric value or correlation, we tried but unfortunatelly. How can we detect circle areas, what do you tkink?

  14. Ann M

    I am working on a traffic sign board recognition system. It should perform canny edge detection, hough transform for circle detection and LDA for comparison of the input image with the images in the database. Can you please help me?

    Reply
    • ammadkhan

      You should first check this project in this project we do analysis to detect edges of red circular board using blob analysis and then comparison is made with image using Co Relation do you need this code ?

      Reply
      • Ann M

        I have completed the edge detection portion using Canny. Now I am working on Hough transform for detecting the circular shapes in the edge detected image. I know that your code will work. But I want to try some new combination of algorithm for the same problem. I want to use LDA instead of Co-relation. Is that possible? I am actually confused in case of LDA, how to give input, what will be the output etc. Can you help me on this?

      • ammadkhan

        I will look through it stay in touch until i do research subscribe to us to keep your self up to date.You can subcribe find it on page above comment section thanks @Crunchmodo

  15. Elrick Terry

    Hi!
    I’m starting to research about traffic recognition.
    I’m very interested on your project to have an idea on how to start to study this.
    In the next two months I want to detect signales only on static images and tag the signal
    Thank you in advance!

    Reply
      • Elrick Terry

        I’m studying at the university a computer vision course and I think that your project is perfect to learn with some that I can run before get books.

        My requirement is learn how to recognize traffic signs in photographs and how to tag it.

    • Sky high

      hi Elrick,
      m doing project,can you please share some ideas for static images .i have completed till detecting the cricle and triangle shape n connected components.after this how to crop that circle from image to do template matching to recognize the sign..

      Reply
  16. alain

    hi
    i am a computer and communication engineering student, i working on a project similar to yours i am trying to recognize road signs and compare them with saved ones in matlab so if you may send me the source code of your program i will be thankful.

    thanks

    Reply
      • alain

        what i am unable to do is to compare two pictures i have already succeeded to import picture from the web cam and turn in into the grey (intenstity) color what i missed is how to compare this picture with a saved ones on simuink

      • ammadkhan

        you can compare two pictures by using co-relation co-relation check the matchings of pixel use this
        R = CORR2(A,B) computes the correlation coefficient between A
        and B, where A and B are matrices or vectors of the same size.
        Example :
        if ur image is is not good apply median filter other wise its fine
        ——-
        I = imread(‘anyimage.tif’);
        J = medfilt2(I);
        R = corr2(I,J)

  17. paresh

    hi khan, im paresh. im trying to run this code, but get an error msg.

    code.
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    a = imread(‘image1.jpg’); %reading images as array to variable ‘a’ & ‘b’.
    b = imread(‘image2.jpg’);
    c = corr2(a,b); %finding the correlation btwn two images
    if c==1
    disp(‘The images are same’)%output display
    else
    disp(‘the images are not same’)
    end;

    error.
    /////////////////////////////////////////////////////////////
    Error using ==> mtimes
    Input arguments must be 2-D.

    Error in ==> corr2 at 26
    r = sum(sum(a.*b))/sqrt(sum(sum(a.*a))*sum(sum(b.*b)));

    Error in ==> correkation at 3
    c = corr2(a,b); %finding the correlation btwn two
    images

    ??? Error using ==> mtimes
    Input arguments must be 2-D.

    Error in ==> corr2 at 26
    r = sum(sum(a.*b))/sqrt(sum(sum(a.*a))*sum(sum(b.*b)));

    Error in ==> correkation at 3
    c = corr2(a,b); %finding the correlation btwn two
    images

    ??? Error using ==> mtimes
    Input arguments must be 2-D.

    Error in ==> corr2 at 26
    r = sum(sum(a.*b))/sqrt(sum(sum(a.*a))*sum(sum(b.*b)));

    Error in ==> correkation at 3
    c = corr2(a,b); %finding the correlation btwn two
    images

    >>

    Reply
  18. shakhil

    hey mate, hope you’re good.. well don’t you have any algorithm for Circular/ellipse hough transform for video processing… actually i’m trying to make a speed limit sign recognition, i did convert RGB to Grayscale ,also extract the red part (since i am planning to detect only the prohibition signs), performed morphological process, but now i’m stuck with the hough transform… Any help please

    Reply
  19. Peter Chang

    Hello Khan,

    Excellent work! Would you kindly send me the source code? I am working on a similar problem.

    Thanks.

    Regards,
    Peter Chang

    Reply
  20. david81

    Hi, good work I’m interesting by the road signs detection and recognition. Could you send me the source code please ?

    Thank you,

    david81

    Reply
  21. mide david

    Hi Khan, this is great to finally find some one using matlab for traffic sign recognition. My research has made me find mostly face recognitions and other types. I am a student of Information engineering and I am working on a project for ANN used for road sign recognition.Please canI have the source codes for the blob analysis method you used?..I was tryinng to work with the PCA for face recognition and tried to adjust for road signs, but because i am just learning matlab, its difficult. Could you help out?

    Reply
  22. mide david

    Hi Khan, I am quite happy to find a place where someone talks about traffic sign recognition. Please can i get the source codes? I am a student on information engineering and I am working on a project on road sign recognition using NN’s.I am also very new to matlab but started getting my way around. could you help/

    Reply
  23. santosh

    hi i got ur code and it was working fine plz tell me how to add new traffic sign to existing project…

    Reply
  24. adnan

    i copied the code above and made it run but its not running giving errors such as
    Error: File: main.m Line: 5 Column: 28
    The input character is not valid in MATLAB statements or expressions.
    kindly help

    Reply
  25. Andrea

    Hi Ammadkhan,

    First I want to help you for sharing your project and knowledge.
    I am very interested in this subject and I would like to know more about this.
    Can you please send me the source code and I would be very happy if you could tell me which tools do I need and which versions.

    Thank you in advance!
    Andrea

    Reply
  26. varsha

    hey ammadkhan, can u pease send me the source code for moving vehicle recognition in a video..

    Reply
  27. Adeel Rafiq

    Hi Ammad Khan,

    Hopefully you are doing well.

    I did as u said. Kindly email me this source code.

    I am working on Foggy Sign board.
    In pre-processing i am working on MSRCR.
    After pre-processing your code will help to complete my task.

    Kindly email me and suggest me if u have pre-processing MSRCR source code.

    rafiq.adeel@yahoo.com

    Thanks

    Reply
  28. mrvgnce

    Hi, I liked the page. I also need the source code. I am trying to write a code for speed limit sign detection. Hope, it will help. my e-mail mrvgnce@gmail.com
    Thanks.

    Reply
  29. asadullah

    hello when i run this code i get an error saying
    File “yieldtag1.jpg” does not exist.
    and also
    Error in ==> project at 10
    mov(k).cdata=blobAnalysis(mov(k).cdata);%Call Blob Analysis Function

    what is the reason behind this?

    Reply
  30. Supreeth

    Hi ammad. Can you send me the source code of this. I am working on this kind of project. It will be very helpful if you send.

    Reply
  31. sadiq

    Hi khan…
    I need the source code of your project. I am using this for hand gesture recognition using template matching. And if you can further guide me in this regard..thanks
    My email is
    engr.sadiq597@yahoo.com

    Reply
  32. Samantha

    Hello. I am working on Road sign detection and recognition project for my final year, and trying to use SVM for classification. Can you please email me your code? It would prove to be a very useful reference.

    Reply
  33. Phanindra

    hello i have an error in when i compile
    “Subscripted assignment dimension mismatch.

    Error in main (line 11)
    vidFrames(:,:,:,k)=mov(k).cdata;”
    can you please help me

    Reply
  34. Guillermo Choque

    HI Khan,

    is it possible to add a special template to vipwarningsigns_templates.mat …. or a answer how to do that by myself. Couldnt find any help anywhere.
    Thx.
    Guillermo

    Reply
  35. Nur Kamala Paily

    Hi, i was run this coding on Matlab 2012b (vipwarningsigns_templates.mat) but the error state that me need to change the directory file. Because it reserved for matlab.

    Reply

Leave a Reply