How to get the both uncontiguous objects in an image as just an object

i want to loop a word in image, so that i can take every character in that image. but if i use the third dimension of bwlabel as my limit loop, the character like "i" will assume as 2 characters. because it has 2 uncontiguous object in that image. i think i have found how to get all characters of "Nick" by cropped those character automatically. but i confuse how to loop it so i can take every character and compare them with my training data.
Thanks before.

3 Commenti

You forgot to post your image and code. Please do so after reading this link
You might want to use ocr() in the Computer Vision System Toolbox instead of doing it low level like you are. Or use deep learning.
i pologize for didn't attach the code and testing image before. and also i apologize because my slow response, because i haven't gotten any notification yet in mail.
Here the files are.
the "i" as my example case in above is the ninth character from the left in Adok_karo1_biner.jpg.
thanks for your respone @Image Analyst
Why are you doing edge detection and dilation??? That certainly doesn't make it easier to separate the characters. Don't do that!

Accedi per commentare.

 Risposta accettata

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage =imread('Adok_karo1_biner.jpg');
% Show image
figure(1)
h1 = subplot(4, 12, 1:12);
imshow(grayImage);
impixelinfo
title('INPUT IMAGE WITH NOISE')
%% Convert to gray scale
if size(grayImage, 3) == 3 % RGB image
grayImage=rgb2gray(grayImage);
end
%% Convert to binary image
threshold = graythresh(grayImage);
binaryImage = im2bw(grayImage, threshold);
% Remove all object containing fewer than 15 pixels
binaryImage = bwareaopen(binaryImage,15);
imshow(binaryImage);
axis('image', 'on'); % Display tick marks.
title('Binary Image', 'FontSize', fontSize);
% Find horizontal profile
h2 = subplot(4, 12, 13:24);
horizontalProfile = sum(binaryImage, 1);
plot(horizontalProfile, 'b-');
title('Horizontal Profile', 'FontSize', fontSize);
grid on;
% Find dividing lines between the characters.
props = regionprops(horizontalProfile == 0, 'Centroid');
xyCentroids = [props.Centroid];
dividingLines = xyCentroids(1:2:end)
for k = 1 : length(dividingLines)
thisX = dividingLines(k);
line(h1, [thisX, thisX], ylim(h1), 'Color', 'r');
line(h2, [thisX, thisX], ylim(h2), 'Color', 'r');
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
drawnow;
% Extract each letter.
fontSize = 12;
for k = 1 : length(dividingLines) - 1
thisX = round(dividingLines(k));
nextX = round(dividingLines(k+1));
subplot(4, 12, 24 + k);
thisLetter = binaryImage(:, thisX:nextX);
imshow(thisLetter);
caption = sprintf('Letter #%d', k);
title(caption, 'FontSize', fontSize);
end
0000 Screenshot.png
Note that if you have kerning, like with characters 3 and 5, this algorithm won't work and you will have to do a little more work, like label the sub image and if there are two blobs, find their centroids. If there centroids are horizontally separated, then they're two kerned characters and you can nsplit them apart with ismember(). If the centroids are vertically aligned or close to it, then you might consider it as one character that has two parts like an i or j.

9 Commenti

thank you for your respone sir.
The charater 7 of cropped characters actually is 1I.jpg instead of 2I.jpg sir. this character is the one pf my biggest problem sir, i can't use bwlabel for identify all characters in the document because of this character.
And then the character 11. I'd tried opening to separate those object (input is a document), but didnt work, because when i use opening this character is succesfully separated, but another character got effect as well, whichs is an object was seperated into two objects. But if i do opening when on the cropped characters, i guess, i can't solve character 3 and 5, because they are in a process.
for the first problem i guess i've found how to solve it by finding the gapses (spaces) horizontally to find the line and then vertically to find the character. But there is a problem in my tes2_2.m file. First ran this file worked properly (i guess). but if i ran it once again this code didn't show any input at all, and the process couldn't terminate by me. So that i forced close my matlab application.
would you help me sir, please.
i've attached my files.
Just remove the bwareaopen() line() that we put in there for noise removal. The smallest of those bars was only 8 pixels and so got removed. When I got rid of that line, it found all 3 bars. See attached.
0000 Screenshot.png
@Image analyst: Sorry for my late respone sir. i'd run your code but i got this error :
Error using line
Vectors must be the same length.
Error in test3_ImageAnalyst (line 42)
line(h1, [thisX, thisX], ylim(h1), 'Color', 'r');
i'd tried to figure it out, by looking for all of parameters length, but havn't solved it yet sir.
Maybe try this
yl = ylim(h1)
line(h1, [thisX, thisX], yl, 'Color', 'r');
I just tried it again and it works beautifully. You must have changed something. Attach your m-file that you are running, and your image so I can test it.
In the meantime, make sure that your old release can take a handle as the first argument for line(). If it can't, then just delete the h1 in the call to line().
yes it works sir, but the line on first row of subplot that should be shown, it didn't show sir.
for letter 11, i think i will be able to crop it based on its profile .
and for letter 3 and 5 i think i will separate them based on number of labelled object that are found in cropped image, like this:
% Extract each letter.
fontSize = 12;
for k = 1 : length(dividingLines) - 1
thisX = round(dividingLines(k));
nextX = round(dividingLines(k+1));
subplot(4, 12, 24 + k);
thisLetter = binaryImage(:, thisX:nextX);
[L, numObject] = bwlabel(thisLetter);
numObject2 = length(numObject);
if (numbObject2 == 2)
L1 = find(L == 1);
L2 = find(L == 2);
imshowpair(L1,L2, 'montage');
caption = sprintf('Letter #%d', k);
title(caption, 'FontSize', fontSize);
end
end
i will try this and hopefully would works.
Thanks a lot Image Analyst
You should use ismember() as I showed in my Image Segmentation Tutorial in my File Exchange:
L1 = ismember(L, 1);
L2 = ismember(L, 2);
because I think find() may just return a 1-D array of linear indexes, not an image.
thank you Image Analyst it worked perfectly. do you wanna help me to answer my new question sir. the question related with this question. This the link is :
thanks a lot sir.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Deep Learning Toolbox in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by