How to keep all image data after transformation?

In the below code, I try to calculate the distance between the 1-pixel and the other 0's pixels within a4 matrix. I face a problem because the 1-pixel is disappeared from the matrix after transformation, therefore, the err4 values becomes inf for all.
h = 120;
v = 80 ;
r=20;
row = 200;
col = 300;
T = [1 0 0;
0 1 0;
h v 1];
tform = affine2d(T);
%create mask
a4 = zeros(row,col);
%set one control point (cp) for mask
a4(row/5*4,col/5*4) = 1;
cb_ref = imref2d(size(a4));
%Apply transformation same as ourput floating image.
b4 = imrotate(a4,r);
b4 = imwarp(b4,tform,'OutputView', cb_ref); % refrence space relative with another image.
%calculte the error matrix
err4 = bwdist(b4);
How can allow the matrix to grow after transformation to keep the 1-pixel?

5 commentaires

any suggestions
Mark Hayworth
Mark Hayworth le 3 Mar 2018
Modifié(e) : Mark Hayworth le 3 Mar 2018
Doesn't run. What is "r" and "cb_ref"?
r is rotation value such as 20 degrees and cb_ref is reference space relative to another image in the same size. This is registration algorithm between two images and very hard to post my whole code
That doesn't help. Without code to define cb_ref, we can't run your code, and the comments aren't enough for me to figure out what's happening. So all I can say now is to make sure that b4 is a 2-D logical (binary) image.
I update my question to define cb_ref and r

Connectez-vous pour commenter.

Réponses (1)

I don't know what your plans are for the warping, but after you warped the image, the dot disappeared:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
h = 120;
v = 80 ;
r=20;
row = 200;
col = 300;
T = [1 0 0;
0 1 0;
h v 1];
tform = affine2d(T);
%create mask
a4 = zeros(row,col);
%set one control point (cp) for mask
a4(row/5*4,col/5*4) = 1;
% Display the image.
subplot(2, 2, 1);
imshow(a4, []);
title('a4 Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Apply transformation same as output floating image.
b4 = imrotate(a4, r);
% Display the image.
subplot(2, 2, 2);
imshow(b4, []);
title('b4 Image', 'FontSize', fontSize, 'Interpreter', 'None');
cb_ref = imref2d(size(a4));
b4 = imwarp(b4, tform, 'OutputView', cb_ref); % reference space relative with another image.
% Calculate the error matrix
% Display the image.
subplot(2, 2, 3);
imshow(b4, []);
title('b4 Warped Image', 'FontSize', fontSize, 'Interpreter', 'None');
err4 = bwdist(b4);
% Display the image.
subplot(2, 2, 4);
imshow(err4, []);
title('err4 Image', 'FontSize', fontSize, 'Interpreter', 'None');

7 commentaires

Thanks for your ans..
All err4 values still inf that I looking to solve. For this problem, the err4 should get a distance to the 1-pixel which not possible because it (i.e. 1-pixel) does not exist in warped b4 anymore.
Correct. Though if the point to find the distance to is not anywhere to be found, one could also make an argument that the values should be nan. Why did you want to do that anyway? What's the point of it?
I need to calculate the Euclidean distance between the original image and the new position after transformation. Then I can use this result to calculate the error in the evaluation phase.
Then why are you using bwdist?
because bwdist is the easiest method to calc the distance between the 1-pixel in original position and its new position after transformation - in case it exists.
But it's new position is not in the image. You did something after rotation so that the point is not there. That's what my demo showed.
Actually, keep the 1-pixel within the image is very important ... therefore I ask for a way to keep the image extend with transformation to display the pixel.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by