Help me about the following error: Index in position 3 exceeds array bounds (must not exceed 1).
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Guillaume
2020년 1월 27일
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.
답변 (1개)
Guillaume
2020년 1월 27일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!