필터 지우기
필터 지우기

How can I fix this code?

조회 수: 1 (최근 30일)
Ridley
Ridley 2024년 5월 25일
편집: Image Analyst 2024년 5월 25일
[xb, yb] = getpts; %click on the bases you would like to add
new_coords = [yb xb]
imgcoordsRC = [imgcoordsRC(:,1:2); new_coords];
idx2 = sub2ind([1520 2704],floor(imgcoordsRC(:,1)),floor(imgcoordsRC(:,2)));
imgcoords = zeros([1520 2704]);
imgcoords(idx2) = 1;
imshow(coordsImage)
Index exceeds the number of array elements. Index must not exceed 1.
  댓글 수: 1
Torsten
Torsten 2024년 5월 25일
My guess is that "imgcoordsRC" has only one column, but you try to reference "imgcoordsRC(:,2)" in your code.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2024년 5월 25일
편집: Image Analyst 2024년 5월 25일
I don't see how imshow would throw that error. So now we're left wondering exactly which line of code threw the error. Because you didn't read the posting guidelines and didn't give us the full error message, we don't know and just have to guess. I would not like to guess at possible reasons for all of those lines, why they might fail. So give us the full error message, ALL the red text, including the actual line number and line of code that threw the error. All you gave was the actual error message but not the line number and line of code. Also put this in front of that code and tell us what you see in the command window:
whos imgcoordsRC
whos coordsImage
and then after you make new_coords, put this:
whos new_coords
And after you make idx2, show us what it is:
whos idx2
And why are you showing coordsImage after that code when apparently none of that code made any change to the image?
Also you probably should not hard code in 1520 2704], you should probably get the size from somewhere (Is it the image???) and use it as a variable.
If you never instantiate imgcoordsRC then you can't append to it so you can instantiate it like I show below.
So the final code should be
whos imgcoordsRC
whos coordsImage
[xb, yb] = getpts; % Click on the bases you would like to add
new_coords = [yb, xb] % [Row, Column]
if isempty(imgcoordsRC) % Check if it's not been assigned yet.
% Create initial matrix since it doesn't exist yet.
imgcoordsRC = new_coords;
else
% Vertically concatenate onto bottom of existing matrix.
imgcoordsRC = [imgcoordsRC(:,1:2); new_coords];
end
whos new_coords
[rows, columns, numberOfColorChannels] = size(coordsImage)
idx2 = sub2ind([rows, columns], floor(imgcoordsRC(:,1)), floor(imgcoordsRC(:,2)));
whos idx2
imgcoords = zeros([rows, columns]);
imgcoords(idx2) = 1;
imshow(coordsImage, []);

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by