在图像文件中获取文本行以创建新的图像文件MATLAB

时间:2014-12-05 20:34:39

标签: matlab image-processing

我需要从图像文件中获取每行文本,并为每行创建一个新的单独图像。我已经有办法计算图像文件中有多少行。

如果有人有任何建议,这将是一个巨大的帮助,因为我对图像不太好。我不允许使用图像处理工具箱。

这是代码:

function out = countLines( imgFile )
% count the number of lines in the image file
im = imread(imgFile);
im = 255 - im;
imbw = uint8(0.33*im(:,:,1) + 0.34*im(:,:,2) + 0.33*im(:,:,3)) > 127;
imwrite(imbw, 'temp.jpg');
rvec = sum(imbw');
rvec1 = [0 rvec 0];
svec = [rvec 0 0];
out = sum(rvec1 == 0 & svec ~= 0); 

1 个答案:

答案 0 :(得分:1)

我在互联网上找到的测试图像上尝试了这种方法,如果文字是直的,它似乎没问题。基本上,您在rvec向量中查找其相邻条目都小于它们的条目。也就是说,您要查找本地最大条目(第一张图像)。之后,将线簇组合在一起以决定图像的分割位置(第二张图像)。

enter image description here enter image description here

clear; clc; 

im = imread('text.png');        % load the image
[Nrows,Ncols,~] = size(im);     % get the size
imgray = rgb2gray(im);          % convert to grayscale

vec = mean(imgray,2);           % average intensities of each row
localMax=[];                    % list of local maximum entries

for i = 2:Nrows-1               % accept local max rows that do not have text
    hasText = sum( imgray(i,:)<100 )>0;
    if vec(i)>vec(i-1) && vec(i)>vec(i+1) && ~hasText
        localMax = [localMax;i];
    end
end


numMax = length(localMax);      
                                % get distances between local max rows
distances = localMax(2:end) - localMax(1:end-1);
thresh = mean(distances);       % the average distance will be our threshold

splitRows = [];                 % rows where we will split the image
cluster = localMax(1);          % start a cluster with the first local max row 

for i = 1:numMax-1;             
    if distances(i) < thresh    % if rows are close together, keep them in a cluster
        cluster = [cluster;localMax(i+1)];
    else                        % average the cluster to get the row where we split the image
        newSplit = round(mean(cluster)); 
        splitRows = [ splitRows ; newSplit ];
        cluster = localMax(i+1);
    end
end
newSplit = round(mean(cluster)); % add the last cluster as well
splitRows = [ splitRows ; newSplit ];