2017-12-16 4 views
1

印刷されたテキストのイメージからテキストを抽出しようとしています。テキスト自体に加えて、単語間のスペースも検出することに興味があります。単語間のスペースは一貫性がなく(意図的に)、それは検出したいものです。単語間の距離に応じてイメージからテキストを抽出するにはどうすればよいですか?

これを達成するには、最初にテキスト行を抽出する必要がありました。私は、添付された投影プロファイルコード(ImageAnalystの答えの1つからコピーされたコード)を使用してそれを達成しました。 enter image description here

これを実現する方法の1つは、単語間の白いピクセルの数をカウントすることでした。単一のスペース(たとえばn)で撮影されたピクセルの数を知っていれば、単語の間の白いピクセルをこの 'n'で除算してスペースの数を得ることによって計算される。

私はそれを試みましたが、計画どおりに進まなかったため、既知のグランド真理値と比較しても結果は非常に矛盾しています。すべてのテキスト行のベースラインを決定することは困難であることが証明されています.2つの単語の間に1つのスペースがあるため、別のピクセルカウントが取得されています。これは、文字dからbまでの白のピクセルがcからsの白のピクセルを数えているとは異なるためです(cの曲線内の白いピクセルも時々カウントされます)。

感謝。

はあなたに私が使用し

コードをありがとう:

clc; 
close all; 
clear; 
fontSize = 16; 
img = imread('Lines.png'); 

[rows, columns, dim] = size(img); 
if dim > 1 
    grayImage = img(:, :, 2); 
end 

% Display the original gray scale image. 
subplot(3, 3, 1); 
imshow(grayImage, []); 
title('Original image', 'FontSize', fontSize); 
axis on; 

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]); % Enlarge figure to full screen. 

% Threshold the image. 
binaryImage = grayImage < 210; 

% Get rid of small areaas of 14 pixels or less 
binaryImage = ~bwareaopen(binaryImage, 15); 
subplot(2, 3, 2); 
imshow(binaryImage); 
title('Binary Image', 'FontSize', fontSize); 
axis on; 

% Vertical Profile 
verticalProfile = sum(binaryImage, 2); 
subplot(3, 3, [2 3]); 
plot(verticalProfile, 'b'); 
grid on; 
title('Vertical Profile', 'FontSize', fontSize); 

rowsWithText = verticalProfile < 600; 

% Find top and bottom lines 
topLines = find(diff(rowsWithText) == 1); 
bottomLines = find(diff(rowsWithText) == -1); 

for j = 1 : length(topLines) 
    topRow = topLines(j); 
    bottomRow = bottomLines(j); 
    thisLine = binaryImage(topRow:bottomRow, :); 
    subplot(3, 3, [4 5 6]); 
    imshow(thisLine, []); 
    axis on; 
    caption = sprintf('Line %d of the text', j); 
    title(caption, 'FontSize', fontSize); 

    % Horizontal profile 
    horizontalProfile = sum(thisLine, 1); 
    subplot(3, 3, [7 8 9]); 
    plot(horizontalProfile); 
    grid on; 
    caption = sprintf('Horizontal Profile of Line %d of the text', j); 
    title(caption, 'FontSize', fontSize); 

    promptMessage = sprintf('lines %d', j); 
    titleBarCaption = 'Continue?'; 
    button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue'); 
    if strcmpi(button, 'Cancel') 
     return; 
    end 
end 
msgbox('Done!'); 

写真:Image of my program

+0

どのように正確にあなたが水平方向のプロファイルを定義します?データは非常にきれいであるように見えるので、1つの解決策は、水平プロファイルをより鮮明に定義することである。単一行画像 'L'を黒色= 0、白色= 1でブール値に変換し、まだなければ水平プロファイルを' min(L、[]、1) 'に設定します。 – verbatross

+0

@verbatross私の質問を編集して、自分のプログラムと私が働いている写真を追加しました。ありがとう –

答えて

1

このような何かで遊んで試してみてください:

% define min length of gap between words; play around with this value 
min_gap_length = 5; 

% define wordStarts/wordEnds like you've defined topLines/bottomLines 
wordStarts = find(diff(horizontalProfile) == -1); 
wordEnds = find(diff(horizontalProfile) == 1); 
if wordStarts(1) > wordEnds(1) 
    wordStarts = [1, wordStarts]; 
end 
if wordEnds(end) < wordStarts(end) 
    wordEnds = [wordEnds, length(horizontalProfile)]; 
end 

% restrict wordStarts/wordEnds to gaps of over min_gap_length 
I = wordStarts(2:end) - wordEnds(1:end-1) > min_gap_length; 
wordStarts = wordStarts([true, I]); 
wordEnds = wordEnds([I, true]); 
関連する問題