【问题标题】:C++ computer vision perspective renderingC++ 计算机视觉透视渲染
【发布时间】:2016-09-30 06:23:17
【问题描述】:

我有一个顶点坐标文件。我在显示将每个坐标与线段连接时产生的图像时遇到问题。我遇到了无数错误。有任何解决问题的帮助或想法吗?

bool computePixelCoordinates(
const Vec3f &pWorld,
const Matrix44f &cameraToWorld,
const float &canvasWidth,
const float &canvasHeight,
const int &imageWidth,
const int &imageHeight,
Vec2i &pRaster)
{
// First transform the 3D point from world space to camera space. 
// It is of course inefficient to compute the inverse of the cameraToWorld
// matrix in this function. It should be done outside the function, only once
// and the worldToCamera should be passed to the function instead. 
// We are only compute the inverse of this matrix in this function ...
Vec3f pCamera;
Matrix44f worldToCamera = cameraToWorld.inverse();
worldToCamera.multVecMatrix(pWorld, pCamera);
// Coordinates of the point on the canvas. Use perspective projection.
Vec2f pScreen;
pScreen.x = pCamera.x / -pCamera.z;
pScreen.y = pCamera.y / -pCamera.z;
// If the x- or y-coordinate absolute value is greater than the canvas width 
// or height respectively, the point is not visible
if (std::abs(pScreen.x) > canvasWidth || std::abs(pScreen.y) > canvasHeight)
return false;
// Normalize. Coordinates will be in the range [0,1]
Vec2f pNDC;
pNDC.x = (pScreen.x + canvasWidth / 2) / canvasWidth;
pNDC.y = (pScreen.y + canvasHeight / 2) / canvasHeight;
// Finally convert to pixel coordinates. Don't forget to invert the y coordinate
pRaster.x = std::floor(pNDC.x * imageWidth);
pRaster.y = std::floor((1 - pNDC.y) * imageHeight);

return true;
}

int main(...)
{
...
Matrix44f cameraToWorld(...);
Vec3f pWorld(...);
float canvasWidth = 2, canvasHeight = 2;
uint32_t imageWidth = 512, imageHeight = 512;
// The 2D pixel coordinates of pWorld in the image if the point is visible
Vec2i pRaster;
if (computePixelCoordinates(pWorld, cameraToWorld, canvasWidth, canvasHeight, imageWidth, imageHeight, pRaster)) {
std::cerr << "Pixel coordinates " << pRaster << std::endl;
}
else {
std::cert << Pworld << " is not visible" << std::endl;
}
...

return 0;
} 

【问题讨论】:

  • 你能详细说明一下我遇到了无数错误吗?
  • practice.cpp:2:7: error: 'Vec3f' 没有命名一个类型 const Vec3f &pWorld, ^ practice.cpp:3:7: error: 'Matrix44f' 没有命名一个类型 const Matrix44f &cameraToWorld, ^ practice.cpp:8:1: error: 'Vec2i' has not been declared Vec2i &pRaster) ^ practice.cpp: In function 'bool computePixelCoordinates(const int&, const int&, const float&, const float&, const int&, const int&, int&)': practice.cpp:15:1: error: 'Vec3f' is not declared in this scope Vec3f pCamera;
  • worldToCamera.multVecMatrix(pWorld, pCamera); ^ practice.cpp:17:37: 错误:'pCamera' 未在此范围内声明 worldToCamera.multVecMatrix(pWorld, pCamera);
  • practice.cpp:19:1: 错误:“Vec2f”未在此范围内声明 Vec2f pScreen; ^ practice.cpp:20:1: 错误:'pScreen' 未在此范围内声明 pScreen.x = pCamera.x / -pCamera.z; ^ practice.cpp:24:5: 错误:'abs' 不是 'std' 的成员 if (std::abs(pScreen.x) > canvasWidth || std::abs(pScreen.y) > canvasHeight)
  • practice.cpp:24:42: 错误:'abs' 不是'std' 的成员 if (std::abs(pScreen.x) > canvasWidth || std::abs(pScreen) .y) > canvasHeight) ^ practice.cpp:27:7: error: expected ';' before 'pNDC' Vec2f pNDC; ^ practice.cpp:28:1: 错误:'pNDC' 未在此范围内声明 pNDC.x = (pScreen.x + canvasWidth / 2) / canvasWidth; ^ practice.cpp:31:9: 错误:在'pRaster'中请求成员'x',它是非类类型'int' pRaster.x = std::floor(pNDC.x * imageWidth);跨度>

标签: c++ computer-vision


【解决方案1】:

这段代码使用了一个矩阵运算库,它定义了像Matrix44fVec3f这样的类型。您需要获取此库,包含适当的标头,并链接到它。

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 2018-11-07
    • 2011-10-18
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2011-07-08
    • 2022-01-18
    相关资源
    最近更新 更多