Index in position 1 exceeds array bounds (must not exceed 1)
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Luke Butcher
am 27 Feb. 2019
Beantwortet: Walter Roberson
am 27 Feb. 2019
Howdy all, super novice here.
Im trying to write a program to reduce the noise on an image by clustering, that is I want to take the value of a single pixel, and if it and at least 3 of its neighbors in a 3x3 area (with the pixel in question at the center) have a value of 1, keep the value of the center pixel at one. otherwise, set the center pixel to 0. Also, i want it to perform this function for all pixels in a 232 X 251 image. One of the problems I'm running into is that I tried to take into account the corner/ border pixels so as not to exceed the array bounds, but i still get an error.
For image named "blackandwhite"
r = size(blackandwhite,1);
c = size(blackandwhite,2);
for r = 2:1:max(r)-1
for c = 2:1:max(c)-1
if ((T(r,c)+T(r+1,c)+T(r-1,c)+T(r+1,c+1)+T(r,c+1)+T(r-1,c+1)+T(r+1,c-1)+T(r,c-1)+T(r-1,c-1)) < 3), T=0;
end
end
end
Anybody have any advice as to how to proceed? Thanks in advance!
(please use small words, still not versed in the jargon of matlab)
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Feb. 2019
The first time your if condition is true, you do T=0 which replaces all of T with the scalar 0. Then your loop continues and tries to index using sizes from the original T array, but T has been replaced with just the single 0. You should be assigning to T(r,c) instead of T.
Note: the way you use c = size(blackandwhite,2) and then for c = 2:1:max( c)-1 is confusing, because you are using c with two different meanings there. Also, size() with a dimension number always returns a scalar, so there is no need to look at max() of it.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!