gusucode.com > matlab程序语言实现的水准网平差程序,使用于测绘人员 > 一种基于极大值稳定区域的文本定位方法matlab源码程序/毕业设计/代码/OCR.m

    clear all

[filename,pathname]=uigetfile('*.*','choose a picture');
path = [pathname filename];
colorImage = imread(path);
figure;imshow(colorImage);

grayImage = rgb2gray(colorImage);%转化成灰度图像
[mserRegions] = detectMSERFeatures(grayImage,'regionAreaRange',[70 8000],'ThresholdDelta',2.6);%提取MSER特称


figure
imshow(grayImage)
hold on;
plot(mserRegions);
title('MSER regions');
hold off;

%first , convert the x,y pixel location data within mserRegions into linear
%indices as required by regionprops.
%联通区像素索引列表
pixelIdxList=cellfun(@(xy)sub2ind(size(grayImage),xy(:,2),xy(:,1)),mserRegions.PixelList,'UniformOutput',false);

% 创建结构体mserConnComp存储MSER区域
mserConnComp.Connectivity = 8;
mserConnComp.ImageSize = size(grayImage);
mserConnComp.NumObjects = mserRegions.Count;
mserConnComp.PixelIdxList = pixelIdxList;

% 利用regionprops获取MSER区域相关参数存入结构体mserStats
mserStats = regionprops(mserConnComp, 'BoundingBox','Eccentricity', 'Solidity', 'Extent', 'Euler', 'Image');
%'BoundingBox' 包含相应区域的最小矩形
%'Eccentricity' 与区域具有相同标准二阶中心矩的椭圆的离心率
%'Solidity' 同时在区域和其最小凸多边形中的像素比例
%'Extent' 同时在区域和其最小边界矩形中的像素比例
%'EulerNumber' 几何拓扑中的一个拓扑不变量——欧拉数
%'Image' 与某区域具有相同大小的逻辑矩阵
% 
% % 计算宽高比
% bbox = vertcat(mserStats.BoundingBox);
% w = bbox(:,3);
% h = bbox(:,4);
% aspectRatio = w./h;
% 
% % Threshold the data to determine which regions to remove. These thresholds% may need to be tuned for other images. 
% filterIdx = aspectRatio'>10;
% filterIdx = filterIdx | [mserStats.Eccentricity] > .997
% filterIdx = filterIdx | [mserStats.Solidity] < .13;
% filterIdx = filterIdx | [mserStats.Extent] < 0.08 | [mserStats.Extent] > 0.8;
% filterIdx = filterIdx | [mserStats.EulerNumber] < -5;
% 
% % Remove regions  
% mserStats(filterIdx) = [];
% mserRegions(filterIdx) = [];
% %想办法把一加上
% 
% figure
% imshow(grayImage);
% hold on;
% plot(mserRegions, 'showPixelList', true,'showEllipses',false)
% title('After Removing Non-Text Regions Based On Geometric Properties')
% hold off;
% 
% % Get a binary image of the a region, and pad it to avoid boundary effects
% % during the stroke width computation.
% regionImage = mserStats(20).Image;
% regionImage = padarray(regionImage, [1 1]);
% 
% % Compute the stroke width image.
% distanceImage = bwdist(~regionImage);
% skeletonImage = bwmorph(regionImage, 'thin', inf);
% strokeWidthImage = distanceImage;
% strokeWidthImage(~skeletonImage) = 0;
% 
% % Show the region image alongside the stroke width image.
% figure
% subplot(1,2,1)
% imagesc(regionImage)
% title('Region Image')
% 
% subplot(1,2,2)
% imagesc(strokeWidthImage)
% title('Stroke Width Image')
% 
% % Compute the stroke width variation metric
% strokeWidthValues = distanceImage(skeletonImage);
% strokeWidthMetric =  std(strokeWidthValues)/mean(strokeWidthValues);
% 
% % Threshold the stroke width variation metric 
% strokeWidthThreshold = 0.5;  
% strokeWidthFilterIdx = strokeWidthMetric > strokeWidthThreshold;
% 
% % Process the remaining regions
% for j = 1:numel(mserStats)
% 
% 	regionImage = mserStats(j).Image;
% 	regionImage = padarray(regionImage, [1 1], 0);
%      
% 	distanceImage = bwdist(~regionImage);
% 	skeletonImage = bwmorph(regionImage, 'thin', inf);
%      
% 	strokeWidthValues = distanceImage(skeletonImage);
% 
% 	strokeWidthMetric =  std(strokeWidthValues)/mean(strokeWidthValues);
%    
% 	strokeWidthFilterIdx(j) = strokeWidthMetric > strokeWidthThreshold;
% end
% 
% % Remove regions based on the stroke width variation
% mserRegions(strokeWidthFilterIdx) = [];
% mserStats(strokeWidthFilterIdx) = [];
% 
% % Show remaining regions
% figure
% imshow(grayImage)
% hold on
% plot(mserRegions, 'showPixelList', true,'showEllipses',false) 
% title('After Removing Non-Text Regions Based On Stroke Width Variation') 
% hold off
% 
% % Get bounding boxes for all the regions
% bboxes = vertcat(mserStats.BoundingBox);
% 
% %画框
% % Convert from the [x y width height] bounding box format to the [xmin ymin% xmax ymax] format for convenience.
% xmin = bboxes(:,1);
% ymin = bboxes(:,2);
% xmax = xmin + bboxes(:,3) - 1;
% ymax = ymin + bboxes(:,4) - 1;
% % Expand the bounding boxes by a small amount.
% expansionAmount = 0.02;
% xmin = (1-expansionAmount) * xmin;
% ymin = (1-expansionAmount) * ymin;
% xmax = (1+expansionAmount) * xmax;
% ymax = (1+expansionAmount) * ymax;
% % Clip the bounding boxes to be within the image bounds
% xmin = max(xmin, 1);
% ymin = max(ymin, 1);
% xmax = min(xmax, size(grayImage,2));
% ymax = min(ymax, size(grayImage,1));
% % Show the expanded bounding boxes
% expandedBBoxes = [xmin ymin xmax-xmin+1 ymax-ymin+1];
% IExpandedBBoxes =  insertShape(colorImage,'Rectangle',expandedBBoxes,'LineWidth',3);
% figure
% imshow(IExpandedBBoxes)
% title('Expanded Bounding Boxes Text')
% 
% % Compute the overlap ratio
% overlapRatio = bboxOverlapRatio(expandedBBoxes, expandedBBoxes);
% 
% % Set the overlap ratio between a bounding box and itself to zero to% simplify the graph representation.
% n = size(overlapRatio,1);
% overlapRatio(1:n+1:n^2) = 0;
% 
% % Create the graph
% g = graph(overlapRatio);
% 
% % Find the connected text regions within the graph
% componentIndices = conncomp(g);
% 
% % Merge the boxes based on the minimum and maximum dimensions.
% xmin = accumarray(componentIndices', xmin, [], @min);
% ymin = accumarray(componentIndices', ymin, [], @min);
% xmax = accumarray(componentIndices', xmax, [], @max);
% ymax = accumarray(componentIndices', ymax, [], @max);
% 
% % Compose the merged bounding boxes using the [x y width height] format.
% textBBoxes = [xmin ymin xmax-xmin+1 ymax-ymin+1];
% 
% % Remove bounding boxes that only contain one text region
% numRegionsInGroup = histcounts(componentIndices);
% textBBoxes(numRegionsInGroup == 1, :) = [];
% 
% % Show the final text detection result.
% ITextRegion = insertShape(colorImage, 'Rectangle', textBBoxes,'LineWidth',3);
% figure
% imshow(ITextRegion)
% title('Detected Text')
% 
% 
% 
% 
% 
% 
% 
% 
%