如何使用此代码从图像中提取每个字符? [英] how to extract each characters from a image?with using this code

查看:176
本文介绍了如何使用此代码从图像中提取每个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  %%水平直方图
H = sum(rotateImage,2);
darkPixels = H< 100; %Threshold
%label
[labeledRegions,numberOfRegions] = bwlabel(darkPixels);
fprintf('区域数=%d \ n',numberOfRegions);
%查找质心
测量= regionprops(labeledRegions,'Centroid');
%将它们放入数组
allCentroids = [measurements.Centroid];
xCentroids = int32(allCentroids(1:2:end));
yCentroids = int32(allCentroids(2:2:end));
%现在你可以将你感兴趣的一些文字裁剪成一个单独的图像:
hold off;
plotLocation = 8;
for band = 1:numberOfRegions-1
row1 = yCentroids(band);
row2 = yCentroids(band + 1);
thisLine = rotatingImage(row1:row2,:);
subplot(7,2,plotLocation)
imshow(thisLine,[])
%%让我们计算并显示直方图。
verticalProjection = sum(thisLine,2);
set(gcf,'NumberTitle','Off')
t = verticalProjection;
t(t == 0)= inf;
mayukh = min(t);
%0哪里有背景,1哪里有字母
letterLocations = verticalProjection> mayukh;
%查找上升和下降边缘
d = diff(letterLocations);
startingRows = find(d> 0);
endingRows = find(d <0);
%提取每个地区
y = 1;
表示k = 1:length(startingRows)
%获取一个字符的子图像...
subImage = thisLine(:,startingRows(k):endingRows(k));
[L,num] = bwlabel(subImage);
表示z = 1:num
bw = ismember(L,z);
%构造此特定图像的文件名。
baseFileName = sprintf('templates%d.png',y);
y = y + 1;
%预先添加文件夹以生成完整的文件名。
fullFileName = fullfile('C:\ Users \Omm \Downloads \',baseFileName);
%写入磁盘。
imwrite(bw,fullFileName);
暂停(2);
imshow(bw);
暂停(5)
结束;
y = y + 2;
结束;
plotLocation = plotLocation + 2;
结束

但不分割整行

解决方案

为什么不简单地使用


i have to extract each characters from a image here i am uploading the code it is segmenting the horizontal lines but not able to segment the each characters along with the horizontal line segmentation loop. some1 please help to correct the code this is the previous code:

%%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

but not segmenting the whole lines

解决方案

Why don't you simply use regionprops with 'Image' property?

img = imread('http://i.stack.imgur.com/zpYa5.png');  %// read the image
bw = img(:,:,1) > 128;  %// conver to mask

Use some minor morphological operations to handle spurious pixels

dbw = imdilate(bw, ones(3)); 
lb = bwlabel(dbw).*bw;  %// label each character as a connected component

Now you can use regionprops to get each image

st = regionprops( lb, 'Image' );

Visualize the results

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

这篇关于如何使用此代码从图像中提取每个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆