Remapping problem in order to calculate the estimate of new frame

2 次查看(过去 30 天)
I need to calculate the estimate of a new frame starting from frame1, and I need to use the process of remapping (the process of taking pixels from one place in the image and locating them in another position in a new image).
The function that allows me to do this is interp2, but on output it returns matrices of NAN (for the case of dst) or 0 (map_x and map_y). Does anyone know where I am going wrong or can suggest how to proceed? thank you very much
This is the code:
% Read the source image and convert it to double type.
img = im2double(imread('frame1.jpg'));
img_n = img(:, :, 1);
disp(size(img_n));
figure
subplot(211)
imshow(img);
title('3 channels');
subplot(212)
imshow(img_n)
title('2 channels');
% Initializes an empty target image and the two mapping matrices x and y
map_x = zeros(size(img_n), 'like', img_n);
map_y = zeros(size(img_n), 'like', img_n);
dst = zeros(size(img_n), 'like', img_n);
% Create a window to display the results
figure
remap_window = 'Remap demo';
imshow(dst);
title(remap_window);
% Initialize index for transformations.
ind = 0;
% Establish a cycle
while true
% Update mapping matrices
update_map(ind, map_x, map_y);
% Apply the transformation using interp2
dst = interp2(img_n, map_x, map_y);
% Show result
imshow(dst);
% Wait for a key press or mouse click.
c = waitforbuttonpress;
if c == 1
break; % Exit the while loop if a key is pressed
end
end
% Function to update mapping matrices.
function update_map(ind, map_x, map_y)
[rows, cols] = size(map_x);
for i = 1:rows
for j = 1:cols
switch ind
case 0
if j > cols * 0.25 && j < cols * 0.75 && i > rows * 0.25 && i < rows * 0.75
map_x(i, j) = 2 * (j - cols * 0.25) + 0.5;
map_y(i, j) = 2 * (i - rows * 0.25) + 0.5;
else
map_x(i, j) = 0;
map_y(i, j) = 0;
end
case 1
map_x(i, j) = single(j);
map_y(i, j) = single(rows - i);
case 2
map_x(i, j) = single(cols - j);
map_y(i, j) = single(i);
case 3
map_x(i, j) = single(cols - j);
map_y(i, j) = single(rows - i);
otherwise
end
end
end
ind = mod(ind + 1, 4); % Update the index for the next iteration
end

回答(1 个)

Matt J
Matt J 2023-10-23
Check wither map_x or map_y (or both) are outside the boundaries of img_n.

Community Treasure Hunt

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

Start Hunting!

Translated by