基於MATLAB實現圖片文字識別並輸出為TXT文件


一、核心實現流程

% 步驟1:讀取圖像並預處理
img = imread('test_image.jpg');
grayImg = rgb2gray(img); % 灰度化
bwImg = imbinarize(grayImg, 'adaptive', 'Sensitivity', 0.6); % 自適應二值化
cleanImg = bwareaopen(bwImg, 50); % 去噪

% 步驟2:文本檢測(CRAFT深度學習模型)
[bboxes, scores] = detectTextCRAFT(cleanImg); % 檢測文本區域
cleanedBboxes = mergeTextRegions(bboxes, scores); % 合併重疊區域

% 步驟3:字符分割
characters = segmentCharacters(cleanImg, cleanedBboxes); % 基於連通域分割

% 步驟4:文字識別
recognizedText = '';
for i = 1:numel(characters)
    charImg = imresize(characters{i}, [40, 30]); % 標準化尺寸
    ocrResults = ocr(charImg, 'Language', 'English+Chinese'); % 多語言識別
    recognizedText = [recognizedText, ocrResults.Text];
end

% 步驟5:保存結果
fid = fopen('output.txt', 'w');
fprintf(fid, '%s', recognizedText);
fclose(fid);

二、關鍵模塊詳解

1. 圖像預處理優化

function processedImg = advancedPreprocess(img)
    % 自動對比度增強
    enhanced = adapthisteq(img, 'ClipLimit', 0.02); % CLAHE算法
    
    % 傾斜校正
    [theta, rho] = hough(enhanced);
    peaks = houghpeaks(rho, theta, 1);
    lines = houghlines(enhanced, theta, rho, peaks);
    angle = mean([lines(theta>0).theta]);
    rotated = imrotate(enhanced, -angle, 'crop');
    
    % 多尺度增強
    pyramid = imagePyramid(rotated, 'Scale', 1.5);
    processedImg = imresize(pyramid{2}, size(rotated));
end

2. 文本區域檢測(CRAFT模型)

% 加載預訓練模型
if isempty(gcp('nocreate'))
    parpool; % 啓動並行池加速
end
net = load('craft_text_detection.mat'); % 下載預訓練模型

% 檢測函數
function bboxes = detectTextCRAFT(I)
    inputSize = [32, 128, 3];
    augmentedImg = imresize(I, inputSize(1:2));
    data = augmentedImg(:,:,:,1);
    bboxes = detect(net, data);
end

3. 字符分割算法

function chars = segmentCharacters(bwImg, bboxes)
    chars = {};
    for i = 1:size(bboxes,1)
        roi = imcrop(bwImg, bboxes(i,:));
        stats = regionprops(roi, 'BoundingBox');
        for j = 1:numel(stats)
            charBbox = stats(j).BoundingBox;
            charImg = imresize(imcrop(roi, charBbox), [40,40]);
            chars{end+1} = charImg;
        end
    end
end

三、完整應用案例

工業儀表讀數識別

function readInstrument(imgPath)
    % 預處理
    img = imread(imgPath);
    processed = advancedPreprocess(img);
    
    % 區域定位(儀表盤ROI)
    roi = [100, 200, 50, 80]; % 根據實際儀表調整座標
    digitImg = imcrop(processed, roi);
    
    % 數字識別
    digits = detectNumbers(digitImg);
    result = num2str(digits);
    
    % 輸出結果
    fid = fopen('instrument_read.txt', 'w');
    fprintf(fid, '儀表讀數:%s', result);
    fclose(fid);
end

參考代碼 識別圖片中的文字,轉化為txt www.youwenfan.com/contentcno/96148.html

四、結果驗證與調試

  1. 可視化調試
function visualizeProcessing(img)
    figure;
    subplot(2,2,1); imshow(img); title('原圖');
    subplot(2,2,2); imshow(preprocessedImg); title('預處理');
    subplot(2,2,3); imshow(bboxes); title('文本區域');
    subplot(2,2,4); imshow(recognizedText); title('識別結果');
end
  1. 錯誤分析
function errorAnalysis(groundTruth, ocrResult)
    confMat = confusionmat(groundTruth, ocrResult);
    figure;
    confusionchart(confMat);
    title('字符識別混淆矩陣');
end

五、擴展功能建議

  1. 批量處理
function batchProcess(folderPath)
    files = dir(fullfile(folderPath, '*.jpg'));
    for i = 1:numel(files)
        processImage(fullfile(folderPath, files(i).name));
    end
end
  1. Web服務部署
webApp = matlab.web.app.ServerApp;
webApp.addSourceFiles('app.m');
webApp.start();

六、注意事項

  1. 環境配置 安裝Computer Vision Toolbox和Deep Learning Toolbox 下載CRAFT預訓練模型(需MATLAB R2022a+)
  2. 性能瓶頸 大尺寸圖像建議先降採樣(imresize) 使用parfor實現並行計算加速

七、參考文獻

  1. 基於模板匹配的OCR實現
  2. MATLAB圖像文字識別優化指南
  3. CRAFT文本檢測模型使用説明
  4. 字符分割與匹配算法實現