Index exceeds the number of array elements

1 次查看(过去 30 天)
How to solve this?
Index exceeds the number of array elements (1208).
Error in Tablica (line 53)
if suma(i)== 0
  12 个评论
Dave B
Dave B 2021-11-30
The first line in this section doesn't make sense, it says to take the rows of im using the contents of im, and the columns using [p_kolumna,1,kolumna,wiersz]
im = im(im, [p_kolumna,1,kolumna,wiersz]);
im_neg = imcrop(im_neg, [p_kolumna,1,kolumna,wiersz]);
I3 = ~im_neg;
I4 = sum(I3,1);
I5 = (I4>1);
tabhist=(1:1:size(im,2));
figure(9), imshow(im_neg), title ('Tablica');
figure (10), plot(tabhist,I5); ylim([0 2])
The code suggested be Yusuf also doesn't mae much sense:
im = [p_kolumna,1,kolumna,wiersz];
As im is used only for its width, to compute tabhist which is the x axis for the plot in figure 10.
The way I read this code, the intent is for tabhist to be 1:length(I5)
Which means the im = line can be left out altogether.
In fact, when you call plot with a vector for y, and you want the x to be 1:length(y), you can drop the x and MATLAB will do it for you:
% im = im(im, [p_kolumna,1,kolumna,wiersz]);
im_neg = imcrop(im_neg, [p_kolumna,1,kolumna,wiersz]);
I3 = ~im_neg;
I4 = sum(I3,1);
I5 = (I4>1);
figure(9), imshow(im_neg), title ('Tablica');
figure (10), plot(I5); ylim([0 2])
Blazej Staniak
Blazej Staniak 2021-11-30
If now I have changed what you told me to do, the error is still displayed
Array indices must be positive integers or logical values.
Error in Untitled11 (line 47)
if suma(im1)== 0 || i >= size(im_neg,2)-1
while 1
if suma(im1)== 0 || i >= size(im_neg,2)-1
p_kolumna = i;
break;
end
i=i+1;
end

请先登录,再进行评论。

回答(2 个)

Yusuf Suer Erdem
Yusuf Suer Erdem 2021-11-29
Hi could you try these codes?
close all;
clear;
clc;
format long g;
format compact;
im1 = imread('a.jpg'); %wczytanie obrazu
%%figure(1); imshow(im1)
im2 = rgb2gray(im1); %zmiana koloru
%%figure(2); imshow(im2)
im3 = imbinarize(im2); % binaryzacja obrazu
%%figure(3); imshow(im3)
im4 = edge(im2, 'prewitt'); %znalezienie krawedzi
%%figure(4); imshow(im4)
%odnalezienie tablicy rejestracyjnej
Iprops=regionprops(im4,'BoundingBox','Area', 'Image');
area = Iprops.Area;
count = numel(Iprops);
maxa= area;
boundingBox = Iprops.BoundingBox;
for i=1:count
if maxa<Iprops(i).Area
maxa=Iprops(i).Area;
boundingBox=Iprops(i).BoundingBox;
end
end
im = imresize(im4, [240 NaN]);
im = imopen(im, strel('rectangle', [4 4]));
im = imcrop(im3, boundingBox);%wyciecie tablicy
%%figure(5); imshow(im)
im = bwareaopen(~im, 1000); %usuniece obiektow jesli szerokosc jest za dluga lub za krotka niz 500
im_neg = imcomplement(im);
figure(6), imshowpair(im,im_neg,'montage');
labeledImage= bwlabel(im);
propied=regionprops(labeledImage,'BoundingBox');
im = imresize(im_neg, [240 NaN]);
im = imopen(im, strel ('rectangle', [4 4]));
im = bwareaopen(~im, 1000);
figure(34), imshow(im);
figure(7), imshow(im_neg), title ('Tablica');
[wiersz, kolumna] = size(im);
i=1;
suma=sum(im);
while i<84
if suma(1:i)== 0
p_kolumna = i;
break;
end
i=i+1;
end
p_kolumna = i;
im = [p_kolumna,1,kolumna,wiersz];
im_neg = imcrop(im_neg, [p_kolumna,1,kolumna,wiersz]);
I3 = ~im_neg;
I4 = sum(I3,1);
I5 = (I4>1);
tabhist=(1:1:size(im,2));
figure(9), imshow(im_neg), title ('Tablica');
figure (10), plot(tabhist)
figure(11), plot(I5)
ylim([0 2]);
  6 个评论
Yusuf Suer Erdem
Yusuf Suer Erdem 2021-11-29
Hi, I do not think I can write the whole code from the scratch. But this code detects letter on that particular image.jpg file.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = 'C:\';
baseFileName = 'image.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual green color channels.
grayImage = rgbImage(:, :, 2);
% Display the monochrome image.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Original Monochrome Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 3);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image
binaryImage = grayImage < 140;
% Display the binary image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage numberOfBlobs] = bwlabel(binaryImage, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
figure;
subplot(2, 1, 1);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
imshow(coloredLabelsImage);
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, grayImage, 'all');
Blazej Staniak
Blazej Staniak 2021-11-29
Thank you for your help, but that's not what I meant but thank you for trying to help. Does anyone know how to solve this?

请先登录,再进行评论。


yanqi liu
yanqi liu 2021-11-30
clc
clear all
close all
format long g;
format compact;
im1 = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/817374/obraz_2021-11-29_164610.png'); %wczytanie obrazu
im1 = imcrop(im1, [20 148 481 85]);
%%figure(1); imshow(im1)
im2 = rgb2gray(im1); %zmiana koloru
%%figure(2); imshow(im2)
im3 = imbinarize(im2); % binaryzacja obrazu
%%figure(3); imshow(im3)
im4 = edge(im2, 'prewitt'); %znalezienie krawedzi
%%figure(4); imshow(im4)
%odnalezienie tablicy rejestracyjnej
Iprops=regionprops(im4,'BoundingBox','Area', 'Image');
area = Iprops.Area;
count = numel(Iprops);
maxa= area;
boundingBox = Iprops.BoundingBox;
for i=1:count
if maxa<Iprops(i).Area
maxa=Iprops(i).Area;
boundingBox=Iprops(i).BoundingBox;
end
end
im = imresize(im4, [240 NaN]);
im = imopen(im, strel('rectangle', [4 4]));
im = im3;%wyciecie tablicy
%%figure(5); imshow(im)
im = bwareaopen(~im, 1000); %usuniece obiektow jesli szerokosc jest za dluga lub za krotka niz 500
im_neg = imcomplement(im);
figure(6), imshowpair(im,im_neg,'montage');
labeledImage= bwlabel(im);
propied=regionprops(labeledImage,'BoundingBox');
im = imresize(im_neg, [240 NaN]);
im = imopen(im, strel ('rectangle', [4 4]));
im = bwareaopen(~im, 1000);
figure(34), imshow(im);
figure(7), imshow(im_neg), title ('Tablica');
[wiersz, kolumna] = size(im);
i=1;
suma=sum(im_neg);
while 1
if suma(i)== 0 || i >= size(im_neg,2)-1
p_kolumna = i;
break;
end
i=i+1;
end
p_kolumna = i;
im_neg = imcrop(im_neg, [1,1,p_kolumna-1,size(im_neg,1)-1]);
I3 = ~im_neg;
I4 = sum(I3,1);
I5 = sum(im_neg,1)>5;
tabhist=(1:1:size(im_neg,2));
figure(9), imshow(im_neg), title ('Tablica');
figure (10), plot(tabhist,I5); ylim([0 2])

Community Treasure Hunt

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

Start Hunting!

Translated by