Code covered by the BSD License  

Highlights from
Simple DIP demos

image thumbnail

Simple DIP demos

by

 

03 Sep 2002 (Updated )

Simple DIP demo using MATLAB 6.1, Image Processing Toolbox and NN Toolbox.

[pic_NNinput]=crop5box(x,thresh)
function [pic_NNinput]=crop5box(x,thresh)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load image (Image in gray scale format) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% S=imread(x);

%%%%%%%%%%%%%%%%%%
% Pre-processing %
%%%%%%%%%%%%%%%%%%


if ndims(x) == 3
    pic_gray=rgb2gray(x);
else
    pic_gray = x;
end

if thresh == -1
    level = graythresh(pic_gray);           % Check for threshould value; (2) graythrest algo
else
    level = thresh;
end

pic_binary1 = im2bw(pic_gray,level);    % Convert to binary image; (3) im2bw algo 
pic_binary2=~pic_binary1;               % Complement of the image (black backgound and white component)


% % Show result
% imshow(S);
% figure;imshow(pic_gray);
% figure;imshow(pic_binary1,[0 1]);clear c
% figure;imshow(pic_binary2,[0 1]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check for the connectivity of the component and label the components accordingly %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pic_labeled=bwlabel(pic_binary2,8);     % 8-connected method (4) bwlabel algo
% figure;image(pic_labeled);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Find the basic properties of all labels components %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pic_data=regionprops(pic_labeled,'basic');  % (5) regionprops concept



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Searching for the size of Bounding Box for each components %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for cnt = 1:length(pic_data)
pic_Box(cnt,:)=pic_data(cnt).BoundingBox;
end

BoxIndex = find(pic_Box(:,3)>100 & pic_Box(:,4)>20 & pic_Box(:,3)<125 & pic_Box(:,4)<30 );


%%%%%%%%%%%%%%%%
% Crop the Box %
%%%%%%%%%%%%%%%%
pic_roi=imcrop(pic_binary2,pic_data(BoxIndex).BoundingBox);

%%%%%%%%%%%%%%%%%%%%%%%
% Crop each character %
%%%%%%%%%%%%%%%%%%%%%%%
[row col]=size(pic_roi);
box_line = 4;
width = (col-6*box_line)/5;
heigth = (row-2*box_line);

for cnt = 1:5
pic_roi2(:,:,cnt)=imcrop(pic_roi,round([(cnt-1)*width+cnt*box_line,box_line,width,heigth]));    
%figure;
%imshow(pic_roi2(:,:,cnt));
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Normalize Image and resize image %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for cnt = 1:5

[maxY maxX] = size(pic_roi2(:,:,cnt));
xx = sum(pic_roi2(:,:,cnt));
yy = sum(pic_roi2(:,:,cnt),2);
[XX Ix] = max(xx);
[YY Iy] = max(yy);

X1 = Ix;
X2 = Ix;
Y1 = Iy;
Y2 = Iy;

while xx(X1)>=1
    if X1>1
        X1=X1-1;
    else
        X1=-1;
        break
    end
end
    X1=X1+1;

while xx(X2)>=1
    if X2<maxX
        X2=X2+1;
    else
        X2=maxX+1;
        break
    end
end
    X2=X2-1;

while yy(Y1)>=1
    if Y1>1
        Y1=Y1-1;
    else
        Y1=-1;
        break
    end
end
    Y1=Y1+1;

while yy(Y2)>=1
    if Y2<maxY
        Y2=Y2+1;
    else
        Y2=maxY+1;
        break
    end
end
    Y2=Y2-1;
    
ttt = imcrop(pic_roi2(:,:,cnt),[X1,Y1,(X2-X1),(Y2-Y1)]);
pic_NNinput(:,:,cnt) = imresize(ttt,[70,50]);
% figure;
% imshow(pic_NNinput(:,:,cnt));

end




% for cnt = 1:5
% [temp NumOfObj] = bwlabel(pic_roi2(:,:,cnt))
% pic_localdata = regionprops(temp,'basic');
%     if NumOfObj == 1;
%         LocalBox = pic_localdata.BoundingBox;
%         pic_temp = imcrop(pic_roi2(:,:,cnt),LocalBox);
%         pic_NNinput(:,:,cnt) = imresize(pic_temp,[35,25]);
%     elseif NumOfObj > 1;
%         for cnt2 = 1: NumOfObj
%             pic_localarea = pic_localdata(cnt2).Area;
%         end
%             [temp I] = max(pic_localarea);
%             LocalBox = pic_localdata(I).BoundingBox;
%             pic_temp = imcrop(pic_roi2(:,:,cnt),LocalBox);
%             pic_NNinput(:,:,cnt) = imresize(pic_temp,[35,25]);
%     else  
%         break
%     end
%     
% end
%     
% for cnt = 1:5
%     figure;
%     imshow(pic_NNinput(:,:,cnt));
% end
%     
% 










Contact us