Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我在图像的(u,v)坐标中有一个三角形。我想在与图像中的三角形贴图的3D坐标(X,Y,Z)处绘制此三角形。
在此, u,v,X,Y,Z都是具有三个表示三角形的三个角的元素的向量。 我有一个非常丑陋,缓慢且不能令人满意的解决方案,其中:
回答: 我认为有两个步骤对您来说是一个更好的解决方案。首先,它提取图像的矩形部分,其中一半是用作纹理贴图的三角形部分,一半将被忽略。然后,将此纹理贴图应用于3D表面对象,将其点调整为将其呈现为三角形而不是四边形。 对于我将在此处显示的示例,我将为您的各种参数使用以下值,假设您有一个三角形,其三角形的点分别标记为“原点”(三角形顶点),点“ A”和点“ B”。图片空间(如下面的第一张图片所示): x = [0.1 0.9 0.8]; % [xorigin xA xB] coordinates in 3-D space y = [0.9 0.1 0.8]; % [yorigin yA yB] coordinates in 3-D space z = [0.1 0.1 0.9]; % [zorigin zA zB] coordinates in 3-D space origin = [150 350]; % Vertex of triangle in image space U = [300 -50]; % Vector from origin to point A in image space V = [50 -250]; % Vector from origin to point B in image space img = imread('peppers.png'); % Sample image for texture map 通过投影变换提取纹理贴图: 此步骤使用“ 图像处理工具箱”功能maketform和imtransform对包含要用作纹理贴图的三角形的图像部分进行投影变换。注意,由于图像必须是矩形的,因此必须包括由点(O,B,C)定义的附加三角形部分。 ![]() 所需图像的三角形部分将在图像的右下半部分,而附加的三角形“填充物”部分将在左上角。请注意,这个额外的三角形可以延伸到图像的外部,默认情况下会导致其一部分填充为黑色。这是执行上面说明的投影变换的代码: A = origin+U; % Point A B = origin+V; % Point B C = BU; % Point C [nRows, nCols, nPages] = size(img); % Image dimensions inputCorners = [origin; ... % Corner coordinates of input space A; ... B; ... C]; outputCorners = [1 nRows; ... % Corner coordinates of output space nCols nRows; ... nCols 1; ... 1 1]; tform = maketform('projective', ... % Make the transformation structure inputCorners, ... outputCorners); triTexture = imtransform(img,tform, 'bicubic', ... % Transform the image 'xdata', [1 nCols], ... 'ydata', [1 nRows], ... 'size', [nRows nCols]); 请注意,此代码将创建与输入图像img相同大小的最终图像triTexture 。 绘制三角形纹理映射的表面: 现在,假设您已对x,y,z变量中的值进行了排序,使得原点的坐标在第一个索引中,点A的坐标在第二个索引中,并且B点的坐标在第三个索引中。现在,您可以创建新的2 X,Y,Z 2曲面坐标集X,Y,Z ,它们包含点B的两个副本,这将导致仅渲染一半的曲面(即,一半包含所需的三角形图像作为纹理贴图) )。这是执行此操作的代码: index = [3 3; 1 2]; % Index used to create 2-by-2 surface coordinates X = x(index); % x coordinates of surface Y = y(index); % y coordinates of surface Z = z(index); % z coordinates of surface hSurface = surf(X, Y, Z, triTexture, ... % Plot texture-mapped surface 'FaceColor', 'texturemap', ... 'EdgeColor', 'none'); axis equal % Use equal scaling on axes axis([0 1 0 1 0 1]); % Set axes limits xlabel('x-axis'); % x-axis label ylabel('y-axis'); % y-axis label zlabel('z-axis'); % z-axis label 这是它创建的结果纹理映射的三角形表面,并添加了插图以显示纹理贴图包含原始图像的正确三角形部分: ![]() 更多&回答... |
![]() |
![]() |