Hello,
I have been trying to get imwarp to do a simple translation on an image but have been having some problems getting it to work. I have a reference image which I am using to rectify another image. I have the coordinate translations as x=-64 and y=-302. I have set up a coordinate transformation matix but the resulting image does not change.
figure(4);
xform=[1 0 0;0 1 0;64 302 1]; input_image='DSC08069.JPG';
f1=im2double(imread(input_image));
tform=affine2d(xform);
corr_RGBIM=imwarp(f1,tform);
imshow(corr_RGBIM);
title('RGB registered');
Any suggestions?

 Réponse acceptée

David Young
David Young le 29 Déc 2014

7 votes

The problem may be that imwarp() crops the output image to the transformed positions of the corners of the input image. A simple translation does not then have any effect. Try using imwarp_same(), attached, which applies the transformation and crops to the original positions of the corners.

2 commentaires

thiirane
thiirane le 29 Déc 2014
Thank you David. That did the trick! I appreciate it.
Daoyu Li
Daoyu Li le 9 Oct 2019
nice work!

Connectez-vous pour commenter.

Plus de réponses (3)

Image Analyst
Image Analyst le 29 Déc 2014
Modifié(e) : Image Analyst le 30 Déc 2014

1 vote

Use the displacement field input argument of imwarp().
Try this to shift the image in x:
fontSize = 20;
grayImage = imread('cameraman.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
subplot(1, 2, 1);
imshow(grayImage);
axis on;
title('Original Image', 'fontSize', fontSize);
[rows, columns, numberOfColorChannels] = size(grayImage)
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
deltaX = 50; % Shift x by 50 pixels.
D = zeros(rows, columns,2);
D(:,:,1) = -deltaX; % Shift x by 50 pixels.
warpedImage = imwarp(grayImage, D);
subplot(1, 2, 2);
imshow(warpedImage);
axis on;
title('Shifted Image', 'fontSize', fontSize);
Try this to shift the image in x and y using the values of -64 and -302 like you specified:
fontSize = 20;
grayImage = imread('concordorthophoto.png');
[rows, columns, numberOfColorChannels] = size(grayImage);
subplot(1, 2, 1);
imshow(grayImage);
axis on;
title('Original Image', 'fontSize', fontSize);
[rows, columns, numberOfColorChannels] = size(grayImage)
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
deltaX = 64; % Shift x by 64 pixels.
deltaY = 302; % Shift y by 302pixels.
D = zeros(rows, columns, 2);
D(:,:,1) = -deltaX; % Shift x by 64 pixels.
D(:,:,2) = -deltaY; % Shift x by 302 pixels.
warpedImage = imwarp(grayImage, D);
subplot(1, 2, 2);
imshow(warpedImage);
axis on;
title('Shifted Image', 'fontSize', fontSize);

3 commentaires

Hi all,
I am trying to shift an image with imwarp using the displacement option. However I would like to shift the image inside an array slightly bigger than the image, without cropping the image during shifting. Below is a modified version of Image Analyst's code to demonstrate my problem. First I pad grayImage with mean grey level values, then I pad the displacement vector with zeros by the same amount. The subplot shows the result, where unfortunately the right hand side of the image is missing.
clear all
fontSize = 20;
grayImage = imread('cameraman.tif');
imshow(grayImage)
[rows, columns, numberOfColorChannels] = size(grayImage);
subplot(2, 2, 1);
imshow(grayImage);
axis on;
title('Original Image', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Displacement matrix
deltaX = 50; % Shift x by 50 pixels.
D = zeros(rows, columns,2);
D(:,:,1) = -deltaX; % Shift x by 50 pixels.
% Warp the original image
warpedImage = imwarp(grayImage, D);
subplot(2, 2, 2);
imshow(warpedImage);
axis on;
title('Shifted Image', 'fontSize', fontSize);
%--------------------------------------------------------------------------
% Additions
%--------------------------------------------------------------------------
% Average grey level of image
meanGreyLevel = round(mean(grayImage(:)));
% Amount of pixels to pad image
colsPad = 20;
rowsPad = 10;
% Pad option
colPadOption = 'post';
rowPadOption = 'both';
% Pad the columns
tempImage = padarray(grayImage, [0 colsPad], meanGreyLevel, colPadOption);
% Pad the rows
grayImagePadded = padarray(tempImage, [rowsPad 0], meanGreyLevel, rowPadOption);
% Show the image
subplot(2, 2, 3);
imshow(grayImagePadded);
axis on;
title('Padded Image', 'fontSize', fontSize);
% Pad the displacement matrix with 0's to match the padded image
% Pad the columns
tempDisp = padarray(D, [0 colsPad], 0.0, colPadOption);
% Pad the rows
DPadded = padarray(tempDisp, [rowsPad 0], 0.0, rowPadOption);
% Warp the padded image (fill displaced space with avg. grey level)
warpedPaddedImage = imwarp(grayImagePadded, DPadded, 'FillValues', meanGreyLevel);
subplot(2, 2, 4);
imshow(warpedPaddedImage);
axis on;
title('Shifted Padded Image', 'fontSize', fontSize);
Thanks in advance.
Image Analyst
Image Analyst le 11 Avr 2020
They now have an imtranslate() function that's much easier to use than imwarp().
DaltonTech
DaltonTech le 12 Avr 2020
imtranslate looks nice, but 'translation' needs to be a 2-element vector. I need to translate the image using a displacement matrix because in my real problem I need to displace each pixel a different amount. Perhaps I need to modify imwarp to update the world limits so that the outer bounds of the output image are input+displacement value.

Connectez-vous pour commenter.

k p
k p le 22 Sep 2016

0 votes

how to apply affine transform with euclidean distance to find shift between two image after finding matching point pair over image
Anas Musah
Anas Musah le 11 Mar 2018

0 votes

hello image analyst, I have tried ur code but it gives me this error
Not enough input arguments.
Error in imwarp (line 11)
cornerPoints = round(cornerPoints);
Error in untitled5 (line 41)
warpedImage = imwarp(grayImage, D);
pls any help

Community Treasure Hunt

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

Start Hunting!

Translated by