问题
My problem is as follows:
I have the picture of a half cylinder taken from a horizontal perspective and it has square grid lines on it, so I was wondering how can I implement in MATLAB to unwrap this half cylinder so all my grid cells become the same size? I know I will loose lots of resolution in the edge cells and a simple linear interpolation should do the trick, but I do not know how to tell MATLAB to do this. Also I know the geometrical properties of the cylinder, radius and height. Any help is greatly appreciated.
This is the approach I am using, but I am trying to find the transformation that will make the edges be same size as inner cells.
im=imread('Capture.png');
imshow(im);
impixelinfo
r = @(x) sqrt(x(:,1).^2 + x(:,2).^2);
w = @(x) atan2(x(:,2), x(:,1));
f = @(x) [sqrt(r(x)) .* cos(w(x)), sqrt(r(x)) .* sin(w(x))];
g = @(x, unused) f(x);
tform2 = maketform('custom', 2, 2, [], g, []);
im3 = imtransform(im, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
figure,
imshow(im3)
回答1:
I think the transformation is much simpler than what you're trying to do. Take a look at the (forward) transformation to take a flat grid and wrap it around a cylinder. The coordinates along the axis of the cylinder (the y
coordinates, in this case) are unchanged. If we take the range of the grid coordinates in the x
direction to be [-1,1], the coordinates on the cylinder will be:
sin(x × π/2)
Since this is the forward transformation going from a grid to the cylinder, it is also the inverse transformation going from the cylinder to the grid.
f = @(x, unused) [sin(x (:, 1) * pi / 2), x(:, 2)]
tform2 = maketform('custom', 2, 2, [], f, []);
im3=imtransform(img, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
Result:
This still isn't perfect, primarily because the original image has borders around it that we're transforming along with the rest of the image. This could be improved by cropping the image to contain only the cylinder portion.
来源:https://stackoverflow.com/questions/32974007/unwrap-picture-of-a-half-cylinder-in-matlab