CLICK HERE FOR FREE BLOGGER TEMPLATES, LINK BUTTONS AND MORE! »

Tuesday, March 27, 2012

Week 7 ( 5. March. 2012 )

Assalamualaikum

Title of Activity :
- Extracting the image of license plate

Objective :
- To locate the vehicle license`s plate.

Content / Procedure :
- Adding some source code to eliminating the noise.

Result / Analysis :
- The process contains each phase performs a segmentation process on the grey image to eliminate the redundant pixels that don`t belong to a plate region.

clear all; close all; clc;

% Threshold
LLimit_Density = 0.20;
HLimit_Density = 1.50;

LLimit_Area = 30;
HLimit_Area = 2000;

LLimit_HWratio = 0.3;
HLimit_HWratio = 9.5;

LLimit_TotalArea = 400;
HLimit_TotalArea = 3000;


% Display result
DisplayGrayImage = 1;
DisplayMedianFiltering = 1;
DisplayHorizontalProfile = 1;
DisplayVerticalProfile = 1;

% Get a list of all the images name in folder images
InputImg = imread('images\499.JPG');
InputImg = im2double( InputImg );

% Convert To Grayscale Using the NTSC standard
red = immultiply( InputImg(:,:,1), 299 );
green = immultiply(  InputImg(:,:,2), 587 );
blue = immultiply( InputImg(:,:,3), 114 );

ProcessImg = red + green + blue;
ProcessImg = imdivide( ProcessImg, 1000 );
if DisplayGrayImage == 1
    figure; hold on; imshow( ProcessImg );
end

% Perform median filtering to remove noise
ProcessImg = medfilt2(ProcessImg, [5 5]);
if DisplayMedianFiltering == 1
    figure; hold on; imshow( ProcessImg );
end

I = ProcessImg;
if ~(isa(I,'double') || isa(I,'single')); I = im2single(I); end
gh = imfilter(I,fspecial('sobel') ,'replicate');
gv = imfilter(I,fspecial('sobel')','replicate');
EdgeImg = abs(gh) + abs(gv);  
%figure, imshow(EdgeImg);

% Create horizontal profile
HProfile = sum(EdgeImg,2);
H_thres = mean(HProfile);  

[ Filtered_HProSegment ] = ProfileSegmentation( HProfile, H_thres*0.75, 15);

if DisplayHorizontalProfile == 1

    clear rgb_img;
    rgb_img(:,:,1) = uint8(EdgeImg * 255);
    rgb_img(:,:,2) = uint8(EdgeImg * 255);
    rgb_img(:,:,3) = uint8(EdgeImg * 255);

    for i=1:size(Filtered_HProSegment,1)
        for j=1:size(rgb_img,2)
            rgb_img(Filtered_HProSegment(i,1),j,1) = 255;
            rgb_img(Filtered_HProSegment(i,1),j,2) = 0;
            rgb_img(Filtered_HProSegment(i,1),j,3) = 0;

            rgb_img(Filtered_HProSegment(i,2),j,1) = 255;
            rgb_img(Filtered_HProSegment(i,2),j,2) = 0;
            rgb_img(Filtered_HProSegment(i,2),j,3) = 0;          
        end
    end

    figure; imshow(rgb_img);

end

% Find the Horizontal Density Vector
HDV = zeros(size(Filtered_HProSegment,1), size(EdgeImg,2));
M = 150;

for k=1:size(Filtered_HProSegment,1)
    SlidingWindowsArea = (Filtered_HProSegment(k,2) - Filtered_HProSegment(k,1)) * M;
    for width_cnt = 1:size(EdgeImg,2)          

        min_w = width_cnt - M;
        max_w = width_cnt + M;

        if min_w < 1
            min_w = 1;
        end

        if max_w > size(EdgeImg,2)
           max_w = size(EdgeImg,2);
        end

        %SlidingWindowsArea = (max_w - min_w )* M;
        SumG = sum(sum(EdgeImg(Filtered_HProSegment(k,1):Filtered_HProSegment(k,2), min_w:max_w)));
        HDV(k,width_cnt) = SumG/SlidingWindowsArea;

    end

    V_thres = mean(HDV(k,:));  
    FoundProfile = ProfileSegmentation( HDV(k,:)', V_thres, 100);
    VProSegment{k}.Profile = [];
    if size(FoundProfile,1) > 0
        VProSegment{k}.Profile = FoundProfile;
    end
end

if DisplayVerticalProfile == 1
    clear rgb_img;
    rgb_img(:,:,1) = uint8(EdgeImg * 255);
    rgb_img(:,:,2) = uint8(EdgeImg * 255);
    rgb_img(:,:,3) = uint8(EdgeImg * 255);

    figure; imshow(rgb_img); hold on;  
    for i=1:size(Filtered_HProSegment,1)
        if isempty(VProSegment{i}.Profile) == 0
            for j=1:size(VProSegment{i}.Profile,1)
                x = VProSegment{i}.Profile(j,1);
                y = Filtered_HProSegment(i,1);          
                w = VProSegment{i}.Profile(j,2) - VProSegment{i}.Profile(j,1);
                h = Filtered_HProSegment(i,2) - Filtered_HProSegment(i,1);          
                rectangle('Position',[x,y,w,h], 'EdgeColor', 'Red', 'LineWidth',2)          
            end
        end
    end
end

% Auto-skew correction
level = graythresh(InputImg);
BW_ProcessImg = im2bw(InputImg,level);

for i=1:size(Filtered_HProSegment,1)
    if isempty(VProSegment{i}.Profile) == 0
        for j=1:size(VProSegment{i}.Profile,1)

            x1 = VProSegment{i}.Profile(j,1);
            y1 = Filtered_HProSegment(i,1);
            x2 = VProSegment{i}.Profile(j,2);
            y2 = Filtered_HProSegment(i,2);

            ProcessImgSegment = BW_ProcessImg(y1:y2, x1:x2);
            EdgeSegment = EdgeImg(y1:y2, x1:x2);                                

            level = graythresh(EdgeSegment);
            BW = im2bw(EdgeSegment,level*0.2);

            BW1 = imfill(BW,'holes');

            cc = bwconncomp(BW1);
            stats = regionprops(BW1, 'Area', 'FilledImage');
            SumBW = 0;
            for cnt =1:size(stats,1)
                SumBW = SumBW + stats(cnt).Area;
            end
            MeanBW = SumBW/size(stats,1);
            idx = find([stats.Area] > MeanBW*0.2);
            BW2 = ismember(labelmatrix(cc), idx);
            se = strel('disk',5);
            BW2 = imclose(BW2,se);
            %figure; imshow(BW2);


            % Find the skew angle by using hough transform
            %BW22 = edge(BW2,'canny');
            [H,T,R] = hough(BW2);
            P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
            lines = houghlines(BW2,T,R,P,'FillGap',5,'MinLength',7);

            DetectedSkew = 0;
            if isempty(fieldnames(lines)) == 0
                max_len = 0;
                max_ind = 0;
                for cnt =1:size(lines,2)                  
                    len = norm(lines(cnt).point1 - lines(cnt).point2);
                    if ( len > max_len)
                      max_len = len;
                      max_ind = cnt;
                   end
                end
                DetectedSkew = lines(max_ind).theta;
            end

            % Deskew the car plate image
            if DetectedSkew == 0
               tf = [1 0 0; 0 1 0; 0 0 1];                  
            else
               tf = [1 tand(90 - DetectedSkew) 0; 0 1 0; 0 0 1];                  
            end
            tform = maketform('affine', tf);
            BW3 = imtransform(BW2 & ProcessImgSegment, tform);            
            se = strel('disk',1);      
            BW3 = imerode(BW3,se);
            %figure; imshow(BW3);
         
            cc = bwconncomp(BW3);
            stats = regionprops(BW3, 'ConvexHull', 'Area', 'BoundingBox');

         
            % Check the properties
            fig_BW_disp = figure;
            for cnt = 1:size(stats,1)
                BW_disp = BW3;
                clf(fig_BW_disp);
                figure(fig_BW_disp); imshow(BW_disp); hold on;
                rectangle('Position',stats(cnt).BoundingBox,'EdgeColor','red','LineWidth',2);

                HWratio = (stats(cnt).BoundingBox(4)/stats(cnt).BoundingBox(3))
                density = stats(cnt).Area / (stats(cnt).BoundingBox(3)*stats(cnt).BoundingBox(4))
                area = stats(cnt).Area
                pause
            end
       
            % Condition: Check the density
            TotalArea = 0;
            idx = [];
            for cnt = 1:size(stats,1)
                HWratio = (stats(cnt).BoundingBox(4)/stats(cnt).BoundingBox(3));
                density = stats(cnt).Area / (stats(cnt).BoundingBox(3)*stats(cnt).BoundingBox(4));
                area = stats(cnt).Area;
                if(density > LLimit_Density) && (density < HLimit_Density) ...
                        && (area > LLimit_Area) && (area < HLimit_Area) ...
                        && (HWratio > LLimit_HWratio) && (HWratio < HLimit_HWratio)
                    idx = [idx cnt];
                    TotalArea = TotalArea + area;
                end
            end        
         
            BW4 = ismember(labelmatrix(cc), idx);          
            cc = bwconncomp(BW4);
         
            if (TotalArea > LLimit_TotalArea) && (TotalArea < HLimit_TotalArea) ...
                    %&& (cc.NumObjects >= LLimit_NumOfCharacter) && (cc.NumObjects <= HLimit_NumOfCharacter)
                figure; imshow(BW4);
            end


        end
    end
end

- Get the illustrates an edge detection process.


Conclusion.
- Through this process, I can detect few factors contribute to this process failed which is :

a) Shining of surface plate.
b) A dirty plate surface.
c) Damaged of surface plate.




- Need more magic image to get successful result.

No comments:

Post a Comment