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

Tuesday, March 27, 2012

Week 8 ( 12.March.2012 )

Assalamualaikum.

Title of Activity :
- Character Segmentation.


Objective :
To get a more accurate binarization results, this process uses several local threshold values since the illumination of the plates.


Content / Procedure
- Adding some code to ease the process of identifying the characters.
- Divide the extracted plate into different images, each containing one isolated character.

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;
                
                x1 = ceil(stats(cnt).BoundingBox(1));
                y1 = ceil(stats(cnt).BoundingBox(2));
                x2 = ceil(stats(cnt).BoundingBox(1) + stats(cnt).BoundingBox(3));
                y2 = ceil(stats(cnt).BoundingBox(2) + stats(cnt).BoundingBox(4));
                
                if x1 > size(BW3,2)
                    x1 = size(BW3,2);
                end


                if x2 > size(BW3,2)
                    x2 = size(BW3,2);
                end
                
                if y1 > size(BW3,1)
                    y1 = size(BW3,1);
                end


                if y2 > size(BW3,1)
                    y2 = size(BW3,1);
                end
                
                GradientMagnitude = size(find(edge(BW3(y1:y2, x1:x2),'log') == 1),1);
                
                if(density > LLimit_Density) && (density < HLimit_Density) ...
                        && (area > LLimit_Area) && (area < HLimit_Area) ...
                        && (HWratio > LLimit_HWratio) && (HWratio < HLimit_HWratio) ...
                        && (GradientMagnitude > LLimit_GradientMagnitude) && (GradientMagnitude < HLimit_GradientMagnitude)
                    
                    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)
                
                % Find the distance between each character
                stats = regionprops(BW4, 'Centroid', 'BoundingBox');
                CenterOfCharacter = [];
                for cnt = 1:size(stats,1)
                    CenterOfCharacter(cnt,:) = stats(cnt).Centroid;                    
                end
                [B, ind] = sort(CenterOfCharacter(:,1), 1);
                SortedCharacter = CenterOfCharacter(ind,:);
                dist = [];
                for cnt = 1:size(SortedCharacter,1)-1
                    dist(cnt,:) = norm(SortedCharacter(cnt,:)-SortedCharacter(cnt+1,:));                    
                end
                                
                for cnt = 1:size(dist,1)-5
                    space_between_character = dist(cnt+2);
                    dist_segment = [dist(cnt:cnt+1); dist(cnt+3:cnt+5)];
                    avg_dist = mean(dist_segment); std_dist = std(dist_segment);


                    if((space_between_character / avg_dist) > RatioBetweenSpaceDistAndAvrDist) ...
                            && (std_dist < MaxStdOfCharDist)
                        
                        BW5 = ismember(labelmatrix(cc), [cnt:cnt+6]);
                        cc = bwconncomp(BW5);
                                                                      
                        for seg_cnt = cnt:cnt+6
                            BW6 = imcrop(BW5, stats(seg_cnt).BoundingBox);
                            ImgSegment{AllowedSegmentCnt} = imresize(BW6, [20 10]);
                            AllowedSegmentCnt = AllowedSegmentCnt + 1; 
                                                   
                        end % for seg_cnt = cnt:cnt+6
                        
                    end % if((space_between_character / avg_dist) > 1.5) && (std_dist < 3)
                       
                end % for cnt = 1:size(dist,1)-5
                                
            end


        end % for j=1:size(VProSegment{i}.Profile,1)
    end 
end


% Show image of each character
if (isempty(ImgSegment) == 0)


    for cnt = 1:size(ImgSegment,2)    
        figure; imshow(ImgSegment{cnt});
    end
    
end


Result / Analysis
- Stretch the contrast of the image to extend over the entire range of grey levels available.
- Resize each character to the standard height and width in order to be used in the following recognition process.




1 comment: