Array indices must be positive integers or logical values.
13 views (last 30 days)
Show older comments
Hello everyone
Ive been trying to make a project that recognize letters from handwriting
although i am really stuck because of an error that has caused
i am using neural network that i am training with data base from the internet
but then i am trying to separate each letter that has been recognized and feed it to the neural network but i am getting an error while doing that
i would be glad if someone could help me with this one . i have seen other posts with same problem but couldnt understand the solution
thanks in adavance
the error:
Array indices must be positive integers or logical values.
Error in Network (line 106)
outputMatrix=net(segImage);
the code :
close all
clear
clc
digitDatasetPath = fullfile('C:\Users\user\Downloads\MatlabCentralUpload (1)\MatlabCentralUpload\HandWriting Recognition Project\archive (1)');
imds = imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames');
labelCount = countEachLabel(imds);
img = readimage(imds,1);
size(img);
numTrainFiles = 300;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([34 34 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(26)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation);
%%Read Image
imagen=imread('patna.png');
%%space
sp=10;
%%Show image
figure(1)
imshow(imagen);
title('INPUT IMAGE WITH NOISE')
%%Convert to gray scale
if size(imagen,3)==3 % RGB image
imagen=rgb2gray(imagen);
end
%%Convert to binary image
threshold = graythresh(imagen);
imagen =~im2bw(imagen,threshold);
%imagen = imclose(imagen, strel('rectangle',[3 ceil(Sp/2)]));
%%Remove all object containing fewer than 30 pixels
imagen = bwareaopen(imagen,15);
pause(1)
%%Show image binary image
figure(2)
imshow(~imagen);
title('INPUT IMAGE WITHOUT NOISE')
%%Label connected components
[L Ne]=bwlabel(imagen);
%%Measure 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)
%%Objects extraction
figure
for n=1:Ne
[r,c] = find(L==n);
n1=imagen(min(r):max(r),min(c):max(c));
imshow(~n1);
fullFileName = fullfile('segmentedImages', sprintf('image%d.png', n));
imwrite(n1, fullFileName);
pause(0.5)
end
%% Feeding to Neural Network and Detecting Text
for i=1:Ne
segImage=double(imread(fullfile('segmentedImages', sprintf('image%d.png', i))));
outputMatrix=net(segImage);
row=find(ismember(outputMatrix, max(outputMatrix(:)))); % returns the row number which has highest probability
% figure(i);
% colormap(gray)
character = double(imread(fullfile('segmentedImages', sprintf('image%d.png', i))));
% imagesc(~character)
% title(imageLabeler(row))
detectedWord(1,i)=imageLabeler(row);
end
0 Comments
Answers (1)
yanqi liu
on 23 Feb 2022
yes,sir,if use this cnn net to classify,may be use
[YPred,scores] = classify(net,segImage)
3 Comments
yanqi liu
on 24 Feb 2022
yes,sir,if possible,may be upload your data to debug
when use classify,the return value is its class result,so may be not need use find(ismember) to get the index row
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!