error in license plate extraction

1 view (last 30 days)
SHOBA MOHAN
SHOBA MOHAN on 18 Jan 2018
Answered: Walter Roberson on 18 Jan 2018
I used the code given in the link https://github.com/abhinaba-audhya/Automatic-Number-Plate-Detection-and-Recognition to recognize the number plate and got the following error.
Subscript indices must either be real positive integers or logicals.
Error in lpr (line 304)
II=I2(row(p):row(p+1),column(q):column(q+1));
function [text] = lpr(name)
%%INITIALIZATION
clc; % Clear command window.
%clear all; % Delete all variables.
warning off;
name='2222.jpg';
I = imread (name); % Read Image and make a backup
I2=I;
k=1; % k will be used to display figures
figure(k); imshow(I); k=k+1;
%%CONVERT THE IMAGE TO GRAYSCALE
[m1 m2 m3]=size(I);
if m3>1 %if the image is RGB
I = rgb2gray(I); %then convert it to grayscale
end
[rows cols] = size(I);
figure(k); imshow(I); k=k+1;
%%ENHANCE THE CONTRAST
[m1 m2] = size(I);
for i=2:m1-1
for j=2:m2-1
if I(i,j)<50
I(i,j)=0;
end
if I2(i,j)<50
I2(i,j)=0;
end
end
end
for i=1:m1
for j=1:m2
if I(i,j)>200
I(i,j)=255;
end
if I2(i,j)>200
I2(i,j)=255;
end
end
end
figure(k); imshow(I); k=k+1;
Idilate = I;
for i = 1:rows
for j = 2:cols-1
temp = max(I(i,j-1), I(i,j));
Idilate(i,j) = max(temp, I(i,j+1));
end
end
I = Idilate;
%%PROCESS EDGES IN HORIZONTAL DIRECTION
difference = 0;
sum = 0;
total_sum = 0;
difference = uint32(difference);
disp('Processing Edges Horizontally...');
max_horz = 0;
maximum = 0;
for i = 2:cols
sum = 0;
for j = 2:rows
if(I(j, i) > I(j-1, i))
difference = uint32(I(j, i) - I(j-1, i));
else
difference = uint32(I(j-1, i) - I(j, i));
end
if(difference > 20)
sum = sum + difference;
end
horz1(i) = sum;
% Find Peak Value if(sum > maximum) max_horz = i; maximum = sum;
end
total_sum = total_sum + sum;
end
average = total_sum / cols;
figure(k);
% Plot the Histogram for analysis subplot(3,1,1);
plot (horz1);
title('Horizontal Edge Processing Histogram'); xlabel('Column Number ->');
ylabel('Difference ->');
k=k+1;
%%Smoothen the Horizontal Histogram by applying Low Pass Filter
disp('Passing Horizontal Histogram through Low Pass Filter...');
sum = 0;
horz = horz1;
for i = 21:(cols-21) sum = 0;
for j = (i-20):(i+20)
sum = sum + horz1(j);
end
horz(i) = sum / 41;
end
subplot(3,1,2); plot (horz);
title('Horizontal Histogram after passing through Low Pass Filter');
xlabel('Column Number ->'); ylabel('Difference ->');
%%Filter out Horizontal Histogram Values by applying Dynamic Threshold
disp('Filter out Horizontal Histogram...');
for i = 1:cols
if(horz(i) < 1.2*average)
horz(i) = 0;
for j = 1:rows I(j, i) = 0;
end
end
end
subplot(3,1,3); plot (horz);
title('Horizontal Histogram after Filtering');
xlabel('Column Number ->');
ylabel('Difference ->');
figure(k); imshow(I); k=k+1;
%%PROCESS EDGES IN VERTICAL DIRECTION difference = 0;
total_sum = 0;
difference = uint32(difference);
disp('Processing Edges Vertically...');
maximum = 0;
max_vert = 0;
for i = 2:rows
sum = 0;
for j = 2:cols %cols
if(I(i, j) > I(i, j-1))
difference = uint32(I(i, j) - I(i, j-1));
end
if(I(i, j) <= I(i, j-1))
difference = uint32(I(i, j-1) - I(i, j));
end
if(difference > 20)
sum = sum + difference;
end
vert1(i) = sum;
%%Find Peak in Vertical Histogram if(sum > maximum)
max_vert = i; maximum = sum;
end
total_sum = total_sum + sum;
end
average = total_sum / rows;
figure(k);
subplot(3,1,1); plot (vert1);
title('Vertical Edge Processing Histogram'); xlabel('Row Number ->');
ylabel('Difference ->');
k=k+1;
%%Smoothen the Vertical Histogram by applying Low Pass Filter disp('Passing Vertical Histogram through Low Pass Filter...'); sum = 0;
vert = vert1;
for i = 21:(rows-21)
sum = 0;
for j = (i-20):(i+20)
sum = sum + vert1(j);
end
vert(i) = sum / 41;
end
subplot(3,1,2); plot (vert);
title('Vertical Histogram after passing through Low Pass Filter');
xlabel('Row Number ->'); ylabel('Difference ->');
%%Filter out Vertical Histogram Values by applying Dynamic Threshold
disp('Filter out Vertical Histogram...');
for i = 1:rows
if(vert(i) < 1.2*average)
vert(i) = 0;
for j = 1:cols
I(i, j) = 0;
end
end
end
subplot(3,1,3); plot (vert);
title('Histogram after Filtering');
xlabel('Row Number ->'); ylabel('Difference ->');
figure(k); imshow(I); k=k+1;
%%Find Probable candidates for Number Plate j = 1;
for i = 2:cols-2
if(horz(i) ~= 0 && horz(i-1) == 0 && horz(i+1) == 0)
column(j) = i;
column(j+1) = i; j = j + 2;
elseif((horz(i) ~= 0 && horz(i-1) == 0) || (horz(i) ~= 0 && horz(i+1) == 0))
column(j) = i; j = j+1;
end
end
j = 1;
for i = 2:rows-2
if(vert(i) ~= 0 && vert(i-1) == 0 && vert(i+1) == 0)
row(j) = i;
row(j+1) = i; j = j + 2;
elseif((vert(i) ~= 0 && vert(i-1) == 0) || (vert(i) ~= 0 && vert(i+1) == 0))
row(j) = i;
j = j+1;
end
end
[temp column_size] = size (column);
if(mod(column_size, 2))
column(column_size+1) = cols;
end
[temp row_size] = size (row);
if(mod(row_size, 2))
row(row_size+1) = rows;
end
%%Check each probable candidate flagm=0;
for p = 1:2:row_size
for q = 1:2:column_size
II=I2(row(p):row(p+1),column(q):column(q+1));
II2=I(row(p):row(p+1),column(q):column(q+1));
[n1 n2] = size(II);
white=0;
for xx=1:n1
for yy=1:n2
if(II2(xx,yy)>240)
white=white+1;
end
end
end
ratio=white/(n1*n2);
if (n1>20 && n2>60 && ratio<0.8)
figure(k);
imshow(II);
k=k+1;
imwrite(II,'plate.jpg');
k=crop(k);
[k,text,flagm] = OCR(k);
end
if flagm
break;
end
end
end
Please help me to resolve the issue.

Answers (1)

Walter Roberson
Walter Roberson on 18 Jan 2018
Your code in the section "%% Find Probable candidates for Number Plate j = 1;" uses the structure
if ...
assign to indexed variable
elseif ...
assign to indexed variable
end
The problem with that is that it is possible that neither condition is true: in that circumstance then you do not assign anything to that indexed variable, which would leave it as 0. You then try to use the indexed variable as a subscript without first testing whether it is 0, leading to errors about subscripts out of range.
When-ever you code an if/elseif/end code block, you need to be certain that you have initialized the variable to an appropriate value for the case where neither condition is true. If it is always the case that one of the two cases are guaranteed to be true, then do not use if/elseif/end : use if/else/end instead.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!