OpenCV CvMat 到 Mat 和指针

发布于 2024-11-28 11:42:43 字数 559 浏览 2 评论 0原文

我正在尝试将一些代码从使用 OpenCV 的 CvMat 转换为 Mat,但在指针方面遇到了一些问题。

这是原始代码:

CvMat *m_projectedTrainFaceMat;
float d_i;
i = 0;
d_i = projectTestFace[i] - m_projectedTrainFaceMat->data.fl[iTrain * m_nEigens + i];

这是我更新的代码:

Mat *m_projectedTrainFaceMat;
float d_i;
i = 0;
d_i = projectTestFace[i] - m_projectedTrainFaceMat->data[iTrain * m_nEigens + i];

如您所见,我从数据调用中删除了 .fl,但我不太清楚如何将数据作为浮点数返回。

有什么建议吗?我尝试将其转换为 float 和 (float *) 但它们最终导致了错误/错误。

e:projectTestFace 是一个 float*。

I'm trying to convert some code from using OpenCV's CvMat to Mat but am having some trouble with pointers.

This is the original code:

CvMat *m_projectedTrainFaceMat;
float d_i;
i = 0;
d_i = projectTestFace[i] - m_projectedTrainFaceMat->data.fl[iTrain * m_nEigens + i];

Here is my updated code:

Mat *m_projectedTrainFaceMat;
float d_i;
i = 0;
d_i = projectTestFace[i] - m_projectedTrainFaceMat->data[iTrain * m_nEigens + i];

As you can see, I removed the .fl from the call to data but I can't quite figure out how to return the data as floats.

Any suggestions? I tried casting it to float and (float *) but they ended up causing errors/mistakes.

e: projectTestFace is a float*.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-12-05 11:42:43

假设您的记忆是连续的,安德烈上面的代码是正确的。在某些情况下,情况并非如此,我们不应该假设。也许更直接的方法(无需使用指针)如下:

d_i = projectTestFace[i] - (*m_projectedTrainFaceMat).at<float>(row, col);

与 Andrey 类似,我假设您的 m_projectedTrainFaceMat 被初始化为具有 CV_32FC1 元素。

Andrey's code above is correct assuming that your memory is continuous. There are cases when that is not and we should not assume. Perhaps a more straightforward way (without having to use pointers) would be as follows:

d_i = projectTestFace[i] - (*m_projectedTrainFaceMat).at<float>(row, col);

Similar to Andrey I'm assuming that your m_projectedTrainFaceMat is intialized as having CV_32FC1 elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文