Matlab 如何从图像中提取每个字符?使用此代码

Matlab 如何从图像中提取每个字符?使用此代码,matlab,image-processing,image-segmentation,Matlab,Image Processing,Image Segmentation,我必须从图像中提取每个字符,这里我正在上传代码,它正在分割水平线,但不能随着水平线分割循环分割每个字符。some1请帮助更正代码 这是前面的代码: %%horizontal histogram H = sum(rotatedImage, 2); darkPixels = H < 100; % Threshold % label [labeledRegions, numberOfRegions] = bwlabel(darkPixels); fprintf('Number of region

我必须从图像中提取每个字符,这里我正在上传代码,它正在分割水平线,但不能随着水平线分割循环分割每个字符。some1请帮助更正代码 这是前面的代码:

%%horizontal histogram
H = sum(rotatedImage, 2);
darkPixels = H < 100; % Threshold
% label
[labeledRegions, numberOfRegions] = bwlabel(darkPixels);
fprintf('Number of regions = %d\n', numberOfRegions);
% Find centroids
measurements = regionprops(labeledRegions, 'Centroid');
% Get them into an array
allCentroids = [measurements.Centroid];
xCentroids = int32(allCentroids(1:2:end));
yCentroids = int32(allCentroids(2:2:end));
% Now you can just crop out some line of text you're interested in, into a separate image:
hold off;
plotLocation = 8;
for band = 1 : numberOfRegions-1 
    row1 = yCentroids(band);        
    row2 = yCentroids(band+1);        
    thisLine = rotatedImage(row1 : row2, :);
    subplot(7, 2, plotLocation)
    imshow(thisLine, [])
    %% Let's compute and display the histogram.
    verticalProjection = sum(thisLine, 2);
    set(gcf, 'NumberTitle', 'Off') 
    t = verticalProjection;
    t(t==0) = inf;
    mayukh=min(t);
    % 0 where there is background, 1 where there are letters
    letterLocations = verticalProjection > mayukh; 
    % Find Rising and falling edges
    d = diff(letterLocations);
    startingRows = find(d>0);
    endingRows = find(d<0);
    % Extract each region
    y=1;
    for k = 1 : length(startingRows)
        % Get sub image of just one character...
        subImage = thisLine(:, startingRows(k):endingRows(k)); 
        [L,num] = bwlabel(subImage);
        for z= 1 : num
            bw= ismember( L, z);
            % Construct filename for this particular image.
            baseFileName = sprintf('templates %d.png', y);
            y=y+1;
            % Prepend the folder to make the full file name.
            fullFileName = fullfile('C:\Users\Omm\Downloads\', baseFileName);
            % Do the write to disk.
            imwrite(bw, fullFileName);
            pause(2);
            imshow(bw);
            pause(5)
        end;
        y=y+2;
    end;
    plotLocation = plotLocation + 2;
end
%%水平直方图
H=总和(旋转图像,2);
暗像素=H<100;%门槛
%标签
[labeledRegions,numberOfRegions]=bwlabel(暗像素);
fprintf('Number of regions=%d\n',numberOfRegions);
%找到质心
测量=区域属性(标记区域,“质心”);
%把他们排成一列
所有质心=[measurements.Centroid];
xCentroids=int32(所有质心(1:2:end));
质心=int32(所有质心(2:2:end));
%现在,您只需将感兴趣的一行文本裁剪成单独的图像:
拖延;
plotLocation=8;
对于波段=1:numberOfRegions-1
row1=中心线(带);
row2=中心线(带+1);
thisLine=rotateImage(第1行:第2行,:);
子地块(7、2、绘图位置)
imshow(此行,[]))
%%让我们计算并显示直方图。
垂直投影=总和(该直线,2);
设置(gcf、编号、关闭)
t=垂直投影;
t(t==0)=inf;
mayukh=最小值(t);
%有背景的0,有字母的1
字母位置=垂直投影>mayukh;
%找到上升和下降的边缘
d=差异(字母位置);
startingRows=find(d>0);
endingRows=find(d为什么不直接使用
的“Image”
属性

img = imread('http://i.stack.imgur.com/zpYa5.png');  %// read the image
bw = img(:,:,1) > 128;  %// conver to mask
使用一些较小的形态学操作来处理伪像素

dbw = imdilate(bw, ones(3)); 
lb = bwlabel(dbw).*bw;  %// label each character as a connected component
现在,您可以使用
regionprops
获取每个图像

st = regionprops( lb, 'Image' );
将结果可视化

figure;
for ii=1:numel(st),  
    subplot(4,5,ii);
    imshow(st(ii).Image,'border','tight');
    title(num2str(ii));
end

这个问题是关于什么的?请记住,我们不知道你在做什么。你应该写这个问题,以便新用户能够全面了解正在发生的事情。@Parag s.Chandakkar现在我已经更正了,你能告诉我怎么做吗?我使用了这个问题,但它不起作用,并显示一个空白数字作为输出:(