Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
907 views
in Technique[技术] by (71.8m points)

matlab - how to extract each characters from a image?with using this code

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
', 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:UsersOmmDownloads', 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...