非粘连字符投影分割

对于二值化后非粘连的字符分割,一般采用连通域分割和投影分割,连通域分割是采用4连通域或8连通域进行标记,然后分割单个字符;投影分割是利用字符的间隔,分割单个字符。前提是字符已经做了倾斜校正和切变校正。

clear;clc;

srcImg=imread('223.bmp');
srcImg=rgb2gray(srcImg);
binImg=im2bw(srcImg,20/255);
figure,imshow(srcImg);
title('原图');

[height width]=size(binImg);

%%水平投影分割
hValArry=zeros(1,height); %%统计每行黑素像素点个数
hProjectionMat=ones(height,width);  %投影直方图
for row=1:height
    for col=1:width
        perPixelValue = binImg(row, col);
        if (perPixelValue == 0)
            hValArry(row)=hValArry(row)+1;
        end
    end
end
for i=1:height    %%水平投影直方图
    for j=1:hValArry(i)
        perPixelValue = 0;
        hProjectionMat(i,width - 1 - j)=perPixelValue;
    end
end
figure,imshow(hProjectionMat);
title('水平投影');

startIndex = 0;  %%分割初始坐标
endIndex = 0;    %%分割终止坐标
inBlock = 0;     %%是否遍历到字符区域内
hIndexArr=zeros(10,2); %%存放分割坐标
k1=1;
for i = 1:height
    if((inBlock==0)&&(hValArry(i)> 0))  %%进入字符区域
        inBlock = 1;
        startIndex = i;
    elseif ((hValArry(i) == 0) && (inBlock==1)) %进入空白区
        endIndex = i;
        inBlock = 0;
        %             roiImg=srcImg(0:height,startIndex:endIndex + 1);
        hIndexArr(k1,1)=startIndex;
        hIndexArr(k1,2)=endIndex + 1;
        k1=k1+1;
    end
end
 figure,imshow(srcImg(startIndex:endIndex +1,:));
title('水平切分');


srcImg=srcImg(startIndex:endIndex +1,:);

%%垂直投影分割
vValArry=zeros(1,width); %%统计每列黑素像素点个数
verticalProjectionMat=ones(height,width);
for col=1:width
    for row=1:height
        perPixelValue = binImg(row, col);
        if (perPixelValue == 0)    %%白底黑字
            vValArry(col)=vValArry(col)+1;
        end
    end
end
for i=1:width   %%垂直投影直方图
    for j=1:vValArry(i)
        perPixelValue = 0;
        verticalProjectionMat(height - 1 - j, i)=perPixelValue;
    end
end

figure,imshow(verticalProjectionMat);
title('垂直投影');

startIndex = 0;  %%分割初始坐标
endIndex = 0;    %%分割终止坐标
inBlock = 0;     %%是否遍历到字符区域内

IndexArr=zeros(10,2);  %%存放分割坐标
k1=1;
figure;

for i = 1:width
    if((inBlock==0)&&(vValArry(i)> 0))   %%进入字符区域
        inBlock = 1;
        startIndex = i;
    elseif ((vValArry(i) == 0) && (inBlock==1)) %进入空白区
        endIndex = i;
        inBlock = 0;
        %             roiImg=srcImg(0:height,startIndex:endIndex + 1);
        IndexArr(k1,1)=startIndex;
        IndexArr(k1,2)=endIndex + 1;
        subplot(3,3,k1);
        k1=k1+1;
        imshow(srcImg(:,startIndex:endIndex + 1));
        title('垂直切分');
    end
end

非粘连字符投影分割_第1张图片

你可能感兴趣的:(图像处理)