impixel fails when a function is called the second time.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Douglas Brenner
il 17 Ott 2016
Commentato: Guillaume
il 17 Ott 2016
Impixel doesn't work the second time.
CENTER = 0;
[X,Y,MAX]=getcenter(MA,x,y)
if (MAX > CENTER)
[X,Y,MAX] = getcenter(MAX,X,Y);
CENTER = MAX;
end
function [X,Y,MAX] = getcenter(MA,X,Y)
value = impixel(MA,X,Y); % FAILS HERE WHEN getcenter is called the second time.
for i = -5:5
for j = -5:5
temp = impixel(MA, X + i,Y + j);
if temp > value
value = temp;
X = X + i;
Y = Y + j;
end
end
end
MAX = value;
The function getcenter fails the second time around at impixel. Error using interp2>makegriddedinterp (line 237) Interpolation requires at least two sample points in each dimension. Consider using INTERP1 if X or Y have constant coordinates that can be eliminated to reduce the dimension.
Error in interp2 (line 128)
F = makegriddedinterp({X, Y}, V, method,extrap);
Error in impixel (line 106)
rgb = interp2(xx,yy,a,xi,yi,'*nearest');
Error in getcenter (line 2)
value = impixel(MA,X,Y);
Error in MAflow (line 17)
[X,Y,MAX] = getcenter(MAX,X,Y);
ideas? thanks
Risposta accettata
Guillaume
il 17 Ott 2016
The problem is not with impixel but with your code. For impixel with three inputs, the first input is an image, the next two the pixel coordinates.
Your getcenter function:
function [X,Y,MAX]=getcenter(MA,x,y);
value = impixel(MA,X,Y);
%...
MAX = value;
end
So MA must be an image (what a bad name). x and y are coordinates. and the MAX returned by getcenter is a pixel intensity.
Your first call:
[X,Y,MAX]=getcenter(MA,x,y)
So the returned MAX is an intensity. Second call:
[X,Y,MAX] = getcenter(MAX,X,Y);
MAX is not an image, it's just a scalar. So of course, the call to impixel is going to fail. You probably meant to pass MA again. If you give your variable some more meaningful names, this wouldn't happen.
By the way, a lot more efficient getcenter function would be:
function [maxcol, maxrow, maxintensity] = getcenter(searchimage, col, row)
%Note that your code only worked with greyscale images
%and scalar coordinate inputs.
%same here
assert(ndims(searchimage) == 2, 'Image is not greyscale');
assert(numel(col) == 1 && numel(row) == 1, 'Coordinates are not scalar');
%generate all combinations of coordinates within +/- 5 of input:
%note that unlike your code, the below works properly even if the input coordinates are within 5 pixels of the image edge
[searchcols, searchrows] = ndgrid(searchimage(max(col-5, 1):min(col+5, size(searchimage, 2), ...
searchimage(max(row-4, 1):max(row+5, size(searchimage, 1));
intensities = impixel(searchimage, searchcols(:), searchrows(:)); %get all pixel intensities at once
[maxintensity, loc] = max(intensities); %find the max intensity and linear index of location
maxcol = searchcols(loc);
maxrow = searchrows(loc);
end
0 Commenti
Più risposte (1)
Douglas Brenner
il 17 Ott 2016
1 Commento
Guillaume
il 17 Ott 2016
In general abbreviations make for poor variable names as they don't mean the same thing to all readers. It can mean microanuryem (?), master of arts, Massachusetts, mega ampere, etc.
In this case, it's also too specific, the function works with any image.
See how much more descriptive and self-documenting the variable names are in my code.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!