How to extract a digit from an image?

9 views (last 30 days)
Hey there,
i have this not so much working code that i wrote on how to extract the 2 numbers that appear on the image into 2 diffrent images containing only 1 digit each, the code i wrote is very inconsistent and not always working, if thers someone who can maybe send me their code or help me improve mine would be wonderfull.. thanks!!
%%
Speed = rgb2grey(speed2);
binSpeed = im2bw(Speed, 0.4); %%making the pic a binary image
%% identifying digit number 1
it = 0;
sz = size(Speed, 2);
for k=2:sz-1
if (sum(binSpeed(:,k) == 0) > 0) && (sum(binSpeed(:,k-1) == 0) == 0) %% checking the index value of the start of the digit
dig = k+1; %% in case the if was true, assembeling the index to dig
end
if (sum(binSpeed(:,k) == 0) > 5) && (sum(binSpeed(:,k+1) == 0) < 7) %% checking the index value of the end of the digit
it = k+1; %% in case the if was true, assembeling the index to it
end
if (it ~= 0) %% stopping the for loop so that the indexes of the digits wont change
break
end
end
digit1 = binSpeed( :, (dig:it));
%% identifying digit number 2
it2 = 0;
for g=it+1:sz-1
if (sum(binSpeed(:,g) == 0) > 0) && (sum(binSpeed(:,g-1) == 0) == 0) %% checking the index value of the start of the digit
dig2 = g; %% in case the if was true, assembeling the index to dig
end
if (sum(binSpeed(:,g) == 0) > 5) && (sum(binSpeed(:,g+1) == 0) < 7) %% checking the index value of the end of the digit
it2 = g; %% in case the if was true, assembeling the index to it
end
if (it2 ~= 0) %% stopping the for loop so that the indexes of the digits wont change
break
end
end
digit2 = binSpeed( :, (dig2:it2));
%% cutting the first digit
sz2 = size(digit1, 1);
for ik=2:sz2-1
if(sum(digit1(ik,:) == 0) > 0) && (sum(digit1(ik-1,:) == 0) == 0)
st = ik;
end
if(sum(digit1(ik,:) == 0) > 2) && (sum(digit1(ik+1,:) == 1) > 2)
nd = ik;
end
end
digit1 = digit1((st:nd), :);
%% cutting the second digit
sz3 = size(digit2, 1);
for s=2:sz3-1
if(sum(digit2(s,:) == 0) > 0) && (sum(digit2(s-1,:) == 0) == 0)
st2 = s;
end
if(sum(digit2(s,:) == 0) > 5) && (sum(digit2(s+1,:) == 1) > 10)
nd2 = s;
end
end
digit2 = digit2((st2:nd2), :);

Accepted Answer

Image Analyst
Image Analyst on 2 May 2020
Try using regionprops() to get each digits bounding box, then use imcrop to extract it into its own image:
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 = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'Speed2.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
hFig = figure;
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION
% Binarize the image
binaryImage = ~imbinarize(grayImage);
% Take the largest 2 blobs only.
binaryImage = bwareafilt(binaryImage, 2);
% Display the segmented image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
%--------------------------------------------------------------------------------------------------------
% EXTRACTION OF EACH BLOB
props = regionprops(binaryImage, 'BoundingBox');
% Display them
for k = 1 : length(props)
thisBB = props(k).BoundingBox;
croppedImage{k} = imcrop(binaryImage, thisBB);
subplot(2, 2, k+2);
imshow(croppedImage{k});
caption = sprintf('Digit #%d', k);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
end
  5 Comments
Image Analyst
Image Analyst on 2 May 2020
Of course. Just cut out any lines like the second group of 4 lines you posted in your above comment. You know what subplot(), imshow(), sprintf(), and title() do, don't you? If you don't want them then don't call them. If that solves your question, could you "Accept this answer"? Thanks in advance.
Shoval  Matzner
Shoval Matzner on 2 May 2020
yes, thank you so much for your help!! you da real mvp

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!