How can I shift images with imwarp and displacement fields without cropping the image?

8 visualizzazioni (ultimi 30 giorni)
Hi all,
I need to shift an image with a series of 2D displacement fields and would like to output the entire shifted image. This is kind of like the 'OutputView' = 'full' option in imtranslate.
Please see my code, which:
1) Creates a checkerboard image
2) Pads the image to include some free space for the image to move into.
3) Loads in some displacement fields (attached)
4) Interpolates the disp fields to the image size
5) Pads the displacement fields
6) Warps the image with imwarp
As you can see the left hand side is cropped. I would like this to extend into the free space (not be cropped).
As far as I know, this is not possible using imwarp and the displacement option, so I would like to know if there is some other way of doing this. Initially I thought I could update world limits in the outputRef var through the imref2D function, but was not able to do this. If someone thinks this is possible and knows how to do it, it would be much appreciated.
With thanks,
% ------------------------------------------------------------------------
% Image file
%--------------------------------------------------------------------------
% Create a checkerboard image, save as tiff image format
p = 10; % rows
q = 20; % cols
n = 10; % width of squares in pixels
image.doubleFile = checkerboard(n,p,q);
imwrite(image.doubleFile,'checkerBoard.tif');
% Read in image
image.uint8File = imread('checkerBoard.tif');
imshow(image.uint8File)
% Image pixel vectors
image.xPxVec = 1:1:size(image.uint8File,2);
image.yPxVec = 1:1:size(image.uint8File,1);
% Image pixel grid [xq,yq] = ndgrid(vec1,vec2);
[image.xPxGrid, image.yPxGrid] = ndgrid(image.yPxVec,image.xPxVec);
%% ------------------------------------------------------------------------
% Pad the image to include 'field of view space' around the checker image
%--------------------------------------------------------------------------
% Average grey level of image
image.meanGreyLevel = round(mean(image.uint8File(:)));
% Amount of pixels to pad image
image.xPxPad = 20;
image.yPxPad = 10;
% Padding options
colPadOption = 'both';
rowPadOption = 'both';
% Pad the columns
image.temp = padarray(image.uint8File,[0 image.xPxPad],image.meanGreyLevel,colPadOption);
% Pad the rows
image.uint8FilePadded = padarray(image.temp,[image.yPxPad 0],image.meanGreyLevel,rowPadOption);
% Show the image
imagesc(image.uint8FilePadded)
pbaspect([1 0.5 1])
%% ------------------------------------------------------------------------
% Displacements
%--------------------------------------------------------------------------
% Read in displacement fields
load('displacementFields.mat');
% Pixels per mm ratio for the FE disp fields
pxPerMm = 4;
% Number of frames
timeFrames = size(disp.x,3);
%% ------------------------------------------------------------------------
% Interpolate disp fields to image size, pad the disp fields, deform images
%--------------------------------------------------------------------------
% Loop over each time frame, deform the image and save
for t = 1:timeFrames
% Disp fields in pixels
pix.x = disp.x(:,:,t)*pxPerMm*10^3*-1;
pix.y = disp.y(:,:,t)*pxPerMm*10^3;
% Pixel vectors (only need to do disp.x because disp.y is same size)
pix.xRowVec = 1:1:size(pix.x,1);
pix.xColVec = 1:1:size(pix.x,2);
% Grid the displacement pixel vectors
[pix.xGrid, pix.yGrid] = ndgrid(pix.xRowVec,pix.xColVec);
% F = griddedInterpolant(X1,X2,V) X1 and X2 define the grids to sample
% the field value V. Use ndgrid not meshgrid
Fx = griddedInterpolant(pix.xGrid, pix.yGrid, pix.x);
Fy = griddedInterpolant(pix.xGrid, pix.yGrid, pix.y);
% Interpolate displacement fields to the image grid [vq = F(xq,yq)];
vqx = Fx(image.xPxGrid, image.yPxGrid);
vqy = Fy(image.xPxGrid, image.yPxGrid);
% Pad disp fields with zeros to match the image and background
% disp.x cols first then rows
tempPadx = padarray(vqx,[0 image.xPxPad],0.0,colPadOption);
vqxPadded = padarray(tempPadx,[image.yPxPad 0],0.0,rowPadOption);
% disp.y cols first then rows
tempPady = padarray(vqy,[0 image.xPxPad],0.0,colPadOption);
vqyPadded = padarray(tempPady,[image.yPxPad 0],0.0,rowPadOption);
% D(:,:,1) is the x displacement matrix and D(:,:,2) is the y disp
D(:,:,1) = vqxPadded;
D(:,:,2) = vqyPadded;
% Transpose the coordinates of the image to disp fields
B = imwarp(image.uint8FilePadded,D,'FillValues',image.meanGreyLevel);
imshow(B)
% Save the deformed image as a tiff file
imwrite(B,['deformedImage_',num2str(t)],'tif');
end

Risposte (0)

Categorie

Scopri di più su Convert Image Type in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by