1. 程式人生 > >非粘連字元投影分割

非粘連字元投影分割

對於二值化後非粘連的字元分割,一般採用連通域分割和投影分割,連通域分割是採用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

這裡寫圖片描述