Help me about the following error: Index in position 3 exceeds array bounds (must not exceed 1).

4 vues (au cours des 30 derniers jours)
Hello i have a problem about my matlab code i don't know if someone has an idea i have a grayscale image i want to write a matlab programm object detection so i started to read and the convert it but now i try to define the classes of my picture but i have some issues i looked for some so answer i got something but i was'nt good explained if someone can help me thanks
% Object recognition in a CFK-Layer Process
clear ALL
clc;
% Load picture
img = imread('Foil.png');
% Show original pic
subplot(4,3,1);
imshow(img);
% Convert into lab
labimg = rgb2lab( repmat(img, [1 1 3]) );
% Adjust and show color channels
subplot(4,3,2);
imshow(labimg(:,:,1),[]);
subplot(4,3,3);
imshow(labimg(:,:,2),[]);
subplot(4,3,4);
imshow(labimg(:,:,3),[]);
% Define classes
foil = img(159:205, 178:307, :);
backgr = img (151:213, 173:316, :);
% Save averages from classes in a matrix
classes = [[mean2(foil(:,:,2)), mean2(foil(:,:,3))]', [mean2(backgr(:,:,2)), mean2(backgr(:,:,3))]'];
[height, width, channels] = size(labimg);
classimg = zeros(height,width);
for i = 1:height
for j = 1:width
classimg(i,j) = nearestNeighbour2d(labimg(i,j,2:3), classes);
end
end
% Show class-image
subplot(4,3,5);
imshow(classimg, []);
  3 commentaires
Guillaume
Guillaume le 27 Jan 2020
As Adam says, we don;t have enough information to help you. It's not even clear what the code does.
However, this function call:
.. = nearestNeighbour2d(labimg(i,j,2:3), classes);
is very suspicious if we go by the name of the function. The two inputs of the function are a 1x1x2 matrix (labimg(i, j, 2:3) and a 2x2 matrix (classes). For a function that's supposed to find a nearest neighbour it doesn't have many neighbours to search into.
If that's the problem, since we have no idea what the code is meant to be doing, we can't tell you how to fix it.
BERG
BERG le 27 Jan 2020
thanks for the reply i forgot to show the line code with the error the goal at the end is to seperate two differents object in a picture i am at the beginning i saw one tutorial i youtube it was interesting i try to follow the same thing as they explain but the diffrence is they used a color image while i use a grayscale image:
classes = [[mean2(foil(:,:,2)), mean2(foil(:,:,3))]', [mean2(backgr(:,:,2)), mean2(backgr(:,:,3))]'];
and i got the this error:
Index in position 3 exceeds array bounds (must not exceed 1).
Error in Code (line 29)
classes = [[mean2(foil(:,:,2)), mean2(foil(:,:,3))]', [mean2(backgr(:,:,2)), mean2(backgr(:,:,3))]'];
if it help this is the nearestNeigbour code they write:
% this is the nearestNeigbour code they used
function y = nearestNeighbour2d(x,classes)
dists = zeros(size(classes(1,:)));
for i = 1:size(classes(1,:),2)
dists(i) = euclidOist(x(1),classes(1,i))+ euclidOist(x(2),classes(2,i));
end
[~, y] = ismember(min(dists), dists);
end

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 27 Jan 2020
Oh! I did think it could be because you had a greyscale image, but since you had a call to rgb2lab I assumed it couldn't be the case. However, I've just noticed the repmat in the rgb2lab call, which I'd overlooked.
1) It's completely pointless to convert a greyscale image to L*a*b*. Your a* and b* channels are all going to be 0, and the L channel will be pretty much a rescaling to 0:100 of your original grey channel. If you're using code that's meant to work with L*a*b*, then it's not going to work with greyscale input.
2) Yes, you're going to get an error on your line 29, since you only have 1 channel. You could rewrite it as:
classes = [mean2(foil), mean2(backgr);
mean2(foil), mean2(bacgkr)];
but once again, for code that expects colour, you're probably wasting your time.
3) That nearestNeighbour2d code is awful, I'm afraid. This has been written by someone who doesn't understand the size function, or the min function. I don't know what euclidOist does, it can't be very complicated, most likely the whole nearestNeighbour2d can be replaced with just one line. In any case, since your loop only uses the a* and b* channel of the image and since as said for greyscale image, a* == b* == 0, the nearestNeighbour2d output will always be the same: 1.
So, in conclusion, the code you're using is not suitable at all for greyscale images. And it's poorly written.
  1 commentaire
BERG
BERG le 27 Jan 2020
thanks i will look for something new i though it the same way that because the picture is already grey i don't need to do it anymore

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by