Template Matching for Invoice Recognition with MATLAB Implementation
Template matching is a computer vision technique used to locate predefined patterns with in images. In invoice recognition, this method identifies key fields such as invoice numbers, dates, and amounts by comparing them with stored templates. The process involves preprocessing, sliding window comparison, similarity calculation, and information extraction.
Invoice documents contain transactional details essential for financial management and tax reporting. Automating data extraction through template matching improves efficiency and accuracy. This approach relies on predefined templates representing common invoice layouts and field positions.
Template Matching Algorithm The algorithm operates through these steps:
- Preprocessing: Convert template and target images to grayscale and apply binarization.
- Sliding Window: Move the template across the target image pixel by pixel.
- Similairty Calculation: Compute correlation or difference metrics between template and current window.
- Maximum Similarity: Identify the window with highest similarity as the match location.
Application to Invoice Recognition For invoice processsing:
- Template Library Construction: Collect sample invoices and extract templates for each field type.
- Template Matching: Apply the algorithm to locate fields in new invoice images.
- Information Extraction: Retrieve text or values from matched regions using OCR or direct reading.
Performance Evaluation Testing on a dataset of 1000 invoice images demonstrated recognition rates above 95%, with accuracy and recall exceeding 90%. These results indicate robust performance for standardized invoice formats.
Limitations and Future Directions Template matching depends on predefined templates, requiring updates when invoice formats change. Performance may degrade with image noise, distortion, or variations in lighting. Future research could explore adaptive template matching, robustness enhancements through advanced preprocessing, and integration with deep learning methods for improved generalization.
MATLAB Code Example
function extractedText = recognizeInvoice(invoiceImage)
% Check if image is RGB and convert to grayscale
if size(invoiceImage, 3) == 3
invoiceImage = rgb2gray(invoiceImage);
end
% Binarize and invert image
binaryImage = imbinarize(invoiceImage);
binaryImage = ~binaryImage;
% Apply morphological dilation with vertical line structuring element
se = strel('line', 12, 85);
dilatedImage = imdilate(binaryImage, se);
% Remove small objects
cleanedImage = bwareaopen(dilatedImage, 35);
% Initialize text storage
extractedText = '';
remainingImage = cleanedImage;
% Open file for results
resultFile = fopen('output.txt', 'a+');
% Load template library
load('templateLibrary.mat');
templateCount = size(templateLibrary, 2);
while true
% Segment characters
[characterLine, remainingImage] = segmentLine(remainingImage);
lineImage = characterLine;
% Label connected components
[labeledImage, componentCount] = bwlabel(lineImage);
for compIdx = 1:componentCount
[rowPos, colPos] = find(labeledImage == compIdx);
% Extract character region
charRegion = lineImage(min(rowPos):max(rowPos), min(colPos):max(colPos));
% Resize to standard dimensions
resizedChar = imresize(charRegion, [40 22]);
% Display for verification
figure(1);
imshow(resizedChar);
pause(0.5);
% Match with templates
matchedChar = matchCharacter(resizedChar, templateCount);
extractedText = [extractedText matchedChar];
end
% Write to file
fprintf(resultFile, '%s\n', extractedText);
extractedText = '';
% Exit loop if no remaining image
if isempty(remainingImage)
break;
end
end
fclose(resultFile);
end