使用MATLAB将2D图像放入半球 [英] Place 2D image into Hemisphere using MATLAB
问题描述
我正在尝试将2D图像放入3D半球.我有一个尺寸为128x128的图片.我产生了我的半球:
I am trying to place a 2D image into a 3D hemisphere. I have an image of size 128x128. I generate my hemisphere:
[x,y,z] = sphere(127);
x = x(64:end,:);
y = y(64:end,:);
z = z(64:end,:);
Attempt = warp(x,y,z,Img)
我的图像是一个圆圈(如下所示).我得到的是半球周围的奇怪变形.但是,我本质上希望将图像放到半球(不是我所得到的)中时的外观.
My image is a circle (as shown below). What I get is a strange warping around the hemisphere. However, I essentially want how it would look if I dropped the image into the hemisphere (which is not what I get).
感谢您提供的任何帮助!
Thanks for any help you can provide!
推荐答案
这里要考虑两个普遍的问题:sphere
生成的(x,y,z)坐标的顺序以及图像的处理方式映射到他们:
There are two general issues to consider here: The ordering of (x,y,z) coordinates generated by sphere
and the way in which the image will be mapped to them:
要查看 sphere
是如何产生积分的,我'要弯曲一个128 x 128的RGB图像样本mapImage
到球面:
To see how sphere
is generating points, I'm going to warp a sample 128-by-128 RGB image mapImage
to the spherical surface:
subplot(1, 2, 1);
imshow(mapImage);
subplot(1, 2, 2);
[x, y, z] = sphere(128);
warp(x, y, z, mapImage);
axis equal
请注意,图像的最左列已映射到在球体的左侧(从其最低点到最高点)上方延伸的一条线.图像中的每个连续列都按顺时针方向(俯视)围绕球体按顺序分段映射.这向我们展示了(x,y,z)
矩阵中的点如何排序.
Notice that the left-most column of the image gets mapped to a line running up the left side of the sphere (from its bottom point to its top point). Each successive column from the image gets mapped in sequential segments following clock-wise (looking down) around the sphere. This shows us how the points in our (x,y,z)
matrices are ordered.
如果希望将半球形的碗"映射到图像上,则需要生成沿碗边缘一半的点,并依次围绕底部和边缘的另一半缠绕.避免涉及几何解释,我只告诉您可以通过在sphere
的返回调用中交换y
和z
矩阵,然后从这三个矩阵中丢弃最后一半的列来生成碗:
If you want a hemispheric "bowl" to map an image onto, you would want to generate points running along one half of the bowls rim, wrapping in sequential segments around the bottom and to the other half of the rim. Avoiding involved geometric explanations, I'll simply tell you that you can generate your bowl by just swapping your y
and z
matrices in the return call from sphere
, then discarding the last half of the columns from the three matrices:
[x, z, y] = sphere(128);
x = x(:, 1:65);
y = y(:, 1:65);
z = z(:, 1:65);
warp(x, y, z, mapImage)
axis equal
从上图可以看到,整个图像被映射到表面,而不仅仅是圆形的中心区域.注意,在碗边缘的顶部和底部,明亮的角如何被捏住,球的生成点密集地聚集在碗的顶部和底部.也许您只想将圆形中心区域映射到表面?
As you can see from the figure above, the entire image is mapped to the surface, not just the circular center region. Notice how the bright-colored corners get pinched up at the top and bottom points of the bowl rim, where the generated points of the sphere are densely clustered. Perhaps you only want to map the circular center region to the surface?
要执行此操作,您将必须变换图像,以使圆形区域沿每一行延伸,从而使其充满图像.您可能可以使用 imwarp
做这种事情,但是我将展示一个使用插值的示例.
To do this, you'll have to transform your image so that the circular region is stretched along each row so that it fills the image. You could probably do this sort of thing with imwarp
, but I'm going to show an example of doing this with interpolation.
[Xq, Yq] = meshgrid(1:128);
Xq = 64.5+sqrt(abs(63.5^2-(Yq-64.5).^2)).*(Xq-64.5)./63.5;
centerImage(:, :, 1) = interp2(mapImage(:, :, 1), Xq, Yq);
centerImage(:, :, 2) = interp2(mapImage(:, :, 2), Xq, Yq);
centerImage(:, :, 3) = interp2(mapImage(:, :, 3), Xq, Yq);
subplot(1, 2, 1);
imshow(centerImage);
subplot(1, 2, 2);
warp(x, y, z, centerImage);
axis equal
计算Xq
的行有点难看.为了解释,Xq
的每行中的索引从-1重新缩放(而不是从1到128),再乘以该行中圆形区域的宽度的一半,然后移回以用作插值范围在1到128之间.这样会在顶部和底部进一步拉伸圆形区域,从而填充整个正方形图像并更好地映射到球形表面.
The line where Xq
is computed looks kinda ugly. To explain, the indices in each row of Xq
are rescaled from -1 to 1 (instead of 1 to 128), multiplied by half the width of the circular region in that row, then shifted back up to be used as an interpolant in the range of 1 to 128. This stretches the circular region moreso at the top and bottom so it fills the entire square image and maps better to the spherical surface.
这篇关于使用MATLAB将2D图像放入半球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!