Storing values in an array

I want to calculate the distance to each pixel from the center pixel and sort the highest 4 values. I used the following code for that. But it gives the error "Subscript indices must either be real positive integers or logicals". How to solve it?
totalVal = 0;
for h = 1 : H
for w = 1:W
%value = pdist2([cen h],[cen w],'euclidean');
value(h,w) = sqrt((cen-h)^2 + (cen-w)^2);
end
end
f3Row = reshape(value, 1, []);
[sortedDist, sortedInds] = sort(f3Row,'descend');
highestDist = (sortedDist(1:4));
for num = 1:n
totalVal = totalVal+highestDist(num);
end
f3 = totalVal;
fprintf('Feature 03 : ');
disp(f3);

2 comentarios

Jan
Jan el 19 de Jun. de 2018
Please post the error message. It is much easier to fix an error than to guess, what the error is.
NC
NC el 20 de Jun. de 2018
Question was edited with the error message.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Jun. de 2018

1 voto

centy = H/2;
centx = W/2;
[Hg, Wg] = ndgrid(1:H, 1:W);
value = sqrt((Hg - centy).^2 + (Wg - centx).^2);
sortedDist = sort(value(:), 'descend');
f3 = sum(sortedDist(1:4));
However, you can predict this value without doing any of this. The value will always be 4 * sqrt(H/2^2 + W/2)^2
Perhaps you are only to consider pixels that meet some particular condition, like belonging to a non-square ROI? And perhaps the center point is not the center of the image but is instead the centroid of the non-square ROI ?

5 comentarios

NC
NC el 20 de Jun. de 2018
Thank you very much. Actually I want to Fourier transform the image and then get the distances because the high frequencies are aggregated around the center. These distances are used as a feature as an input to a SVM.
Walter Roberson
Walter Roberson el 20 de Jun. de 2018
The code you were showing deals with the euclidean distance of a pixel location from something, and does not have any relationship to pixel content or content of the fft() or fft2() of an image.
I guess you could do
fimg = fft2(YourImage);
fimgss = fimg(2:end,2:end); %skip the zero-order components
absfimg = abs(fimgss);
meanabs = mean(absfimg(:));
[sortdiff, diffidx] = sort(abs( absfimg(:) - meanabs ), 'descend');
ssfar4 = diffidx(1:4);
f13frabs = sum(sortdiff(1:4)); %sum of the abs of the frequency differences
f13frorig = sum(fimgss(ssfar4)); %sum of the complex frequency differences
Even though I took care to skip the zero-order coefficients along the left and top boundary, the ssfar4 indices turn out to correspond to pixels at the upper left and lower right corners. This is because for visual images, the fft2 often has peaks at the edges (and at both edges, because of conjugate symmetry.)
NC
NC el 20 de Jun. de 2018
Editada: Walter Roberson el 20 de Jun. de 2018
When I use this code I got the error
Subscript indices must either be real positive integers or logicals.
Error in completeFeatures (line 126) %completeFeatures is file name
f13frabs = sum(sortdiff(1:4))
What should be done here?
Walter Roberson
Walter Roberson el 20 de Jun. de 2018
The only way that could have happened there is if you had created your own variable named sum . Do not name your own variable sum .
NC
NC el 20 de Jun. de 2018
Thank you. The problem was resolved by renaming existing sum variable.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Preguntada:

NC
el 19 de Jun. de 2018

Comentada:

NC
el 20 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by