I am trying to run an code an shows Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
26 ビュー (過去 30 日間)
古いコメントを表示
function saliency_map = calculate_single_saliency(img, f_x, f_y, b_x, b_y)
% Calculate saliency map for a single image
I = double(img);
N = round(double([f_x; b_x]));
M = round(double([f_y; b_y]));
y = [ones(1, numel(f_x)), zeros(1, numel(b_x))];
[P, Q, ~] = size(I); % Get image dimensions
% Ensure that N and M are within the bounds of the image
N = min(max(N, 1), Q);
M = min(max(M, 1), P);
% Flatten the image channels to match the number of pixels
I1 = I(:,:,1);
I2 = I(:,:,2);
I3 = I(:,:,3);
x = [I1(sub2ind([P, Q], M, N)), I2(sub2ind([P, Q], M, N)), I3(sub2ind([P, Q], M, N))];
tt = [I1(:), I2(:), I3(:)];
fx = zeros(P*Q, 1); % Initialize the saliency map
for i = 1:size(x, 1)
tx = ones(P*Q, 1);
for j = 1:size(x, 1)
if i ~= j
diff = x(i, :) - x(j, :);
if any(diff ~= 0)
tx = tx .* (bsxfun(@minus, tt, x(j, :)) ./ diff);
end
end
end
fx = fx + tx * y(i);
end
fx = min(max(fx, 0), 1); % Clip values to [0, 1]
% Reshape fx to [P, Q] without using hard-coded dimension calculations
if numel(fx) ~= P * Q
error('Unexpected size mismatch: fx cannot be reshaped to match the image dimensions.');
end
saliency_map = reshape(fx, [P, Q]);
% Normalize the saliency map
saliency_map = (saliency_map - min(saliency_map(:))) / (max(saliency_map(:)) - min(saliency_map(:)) + eps);
end
0 件のコメント
回答 (1 件)
Walter Roberson
2024 年 11 月 6 日 2:04
移動済み: Walter Roberson
2024 年 11 月 6 日 2:04
N = round(double([f_x; b_x]));
M = round(double([f_y; b_y]));
We are not given any information about the sizes of f_x or f_y or b_x or b_y, so we are unable to calculate the sizes of N or M.
[P, Q, ~] = size(I); % Get image dimensions
P will be the number of rows in I and Q will be the number of columns in I -- both will be scalars.
N = min(max(N, 1), Q);
M = min(max(M, 1), P);
max(N,1) will be the same size as N, and min() of that and Q will be the same size as N as well. That statement will not change the size of N. (This includes the case where N and M are empty -- after the min() max() process, they would continue to be empty.)
x = [I1(sub2ind([P, Q], M, N)), I2(sub2ind([P, Q], M, N)), I3(sub2ind([P, Q], M, N))];
We have no idea what size M and N are, so we are unable to predict the size of I1(sub2ind([P, Q], M, N)) or the other components. However, sub2ind() would error if M and N are not the same size, and the expression would return something the same size as M (which is the same size as N). So x will be size(N,1) by (size(N,2)*3)
tt = [I1(:), I2(:), I3(:)];
tt will be initialized to (P*Q,3) in size.
tx = ones(P*Q, 1);
tx is initialized to (P*Q, 1) in size.
diff = x(i, :) - x(j, :);
x(i,:) is 1 x 3 and so is x(j,:) so diff is 1 x 3.
tx = tx .* (bsxfun(@minus, tt, x(j, :)) ./ diff);
(P*Q,3) in size bxsfun @minus 1 x 3 will give a result that is (P*Q,3) in size. Using ./ with a 1 x 3 will give a (P*Q,3) result.
A (P*Q,1) value .* a (P*Q,3) value will give a (P*Q,3) result.
So after the first iteration in which if any(diff ~= 0) is true, tx will switch from being a (P*Q,1) output to being a (P*Q,3) value.
For the second and subsequent iterations, the tx .* value will be a (P*Q,3) .* a (P*Q,3) value and will give a (P*Q,3) output.
fx = zeros(P*Q, 1); % Initialize the saliency map
fx is initialized to (P*Q,1)
fx = fx + tx * y(i);
tx will be either (P*Q,1) or (P*Q,3) . After the addition, fx will either continue to be (P*Q,1) or else will be promoted to (P*Q,3)
fx = min(max(fx, 0), 1); % Clip values to [0, 1]
The size of fx will not change -- it will continue to be either (P*Q,1) or (P*Q,3)
if numel(fx) ~= P * Q
That is a good test. But numel(fx) will be P*Q only if any(diff ~= 0) was never true -- otherwise it will be P*Q*3
saliency_map = reshape(fx, [P, Q]);
That reshape should work -- you have protected against the possibility of fx being a different size with the if statement.
I have to wonder if the error message is showing up with a version of the code that does not have the if numel(fx) protection in it?
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!