How to do Image Segmentation on multiple digit?
2 views (last 30 days)
Show older comments
I want segment all the image into individual character and i manage to do so but the problem is with number 1 the final product is not what i want. Below is the image that i want to segment it and with the result:
This is the imagae after segmented witch is in the size of 28 by 28
As you can see digit 1 does not process correctly as this will affect CNN to recognize it.
So can anyone tell me how to solve this problem?
Below is the code:
%% Input Image
% Here input image which is a RGB image is preprocessed, converted to
% Binary Image, followed by noise removal
[filename,filepath]=uigetfile({'*.jpg'},'Select and image');
originalImage = imread(strcat(filepath, filename));
% Showing Original image
% figure(1);
imshow(originalImage);
title('Original Image');
% Conversion to grayScale image
grayImage = rgb2gray(originalImage);
% Conversion to binary image
threshold = graythresh(grayImage);
binaryImage = ~im2bw(grayImage,threshold);
% Removes all object containing fewer than 30 pixels
moddedImage = bwareaopen(binaryImage,30);
pause(1)
% Showing binary image
figure(2);
imshow(moddedImage);
title('Modified Image');
% Labelling connected components
[L,Ne] = bwlabel(moddedImage);
% Measuring properties of image regions
propied = regionprops(L,'BoundingBox');
hold on
% Plot Bounding Box
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',2)
end
hold off
pause (1)
%% Image Segmentation
%figure; % Create a new figure window.
% Maximize the figure window.
%set(gcf, 'Units','Normalized','OuterPosition',[0 0 1 1]);
for n=1:Ne
[r,c] = find(L==n);
n1 = moddedImage(min(r):max(r),min(c):max(c));
n1 = imresize(n1,[128 128]);
n1 = imgaussfilt(double(n1),1);
n1 = padarray(imresize(n1,[20 20],'bicubic'),[4 4],0,'both');
subplot(3, 4, n);
imshow(n1);
segmentedImages1 = fullfile('segmentedImages1', sprintf('image%d.png', n));
imwrite(n1, segmentedImages1);
pause(1)
end
%% feed image
myFolder = 'D:\CNN test\segmentedImages1';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg');
theFiles = dir(filePattern);
storedStructure = load('test2.mat');
net = storedStructure.net;
%i = 0;
%outcome = zeros(1,1);
%outcome = [];
for k = 2 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
I = imread(fullFileName);
subplot(3, 4, k);
imshow(I); % Display image.
drawnow; % Force display to update immediately.
label=(classify(net,I));
title([' Recognized Digit is ' char(label)])
fprintf( 'student id: %s\n',label)
end
%% Displaying Detected Text
fprintf( 'student id: %s\n',label)
3 Comments
Accepted Answer
DGM
on 4 Jul 2021
Don't rescale the characters in a way that changes their aspect ratio. Resize based only on one dimension, then make up for it when you pad.
minpadsize = 4;
outsize = [28 28];
nr = imresize(n1,[outsize(1)-2*minpadsize NaN],'bicubic');
hpad = (outsize(2)-size(nr,2))/2;
np = padarray(nr,[minpadsize floor(hpad)],0,'pre');
np = padarray(np,[minpadsize ceil(hpad)],0,'post');
4 Comments
DGM
on 5 Jul 2021
What I meant is to isolate the code doing the image transformation and make sure it works by itself.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows 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!