【发布时间】:2018-11-17 10:36:47
【问题描述】:
我尝试在 python 中实现以下 c++ 代码:
depth.convertTo(depth, CV_64FC1); // I do not know why it is needed to be
transformed to 64bit image my input is 32bit
Mat nor(depth.size(), CV_64FC3);
for(int x = 1; x < depth.cols - 1; ++x)
{
for(int y = 1; y < depth.rows - 1; ++y)
{
Vec3d t(x,y-1,depth.at<double>(y-1, x)/*depth(y-1,x)*/);
Vec3d l(x-1,y,depth.at<double>(y, x-1)/*depth(y,x-1)*/);
Vec3d c(x,y,depth.at<double>(y, x)/*depth(y,x)*/);
Vec3d d = (l-c).cross(t-c);
Vec3d n = normalize(d);
nor.at<Vec3d>(y,x) = n;
}
}
imshow("normals", nor);
python 代码:
d_im = cv2.imread("depth.jpg")
d_im = d_im.astype("float64")
normals = np.array(d_im, dtype="float32")
h,w,d = d_im.shape
for i in range(1,w-1):
for j in range(1,h-1):
t = np.array([i,j-1,d_im[j-1,i,0]],dtype="float64")
f = np.array([i-1,j,d_im[j,i-1,0]],dtype="float64")
c = np.array([i,j,d_im[j,i,0]] , dtype = "float64")
d = np.cross(f-c,t-c)
n = d / np.sqrt((np.sum(d**2)))
normals[j,i,:] = n
cv2.imwrite("normal.jpg",normals*255)
输入图像:
c++代码输出:
我的python代码输出:
我找不到这些差异的原因。如何使用 python 获取 c++ 代码输出?
【问题讨论】:
-
实际上当我测试它时,我的 Python 输出看起来很合理;您安装了哪些版本的使用模块? (
pip list --local) -
opencv-python 3.4.3.18,numpy 1.15.2
-
好的,我的版本是:cv2:3.4.3,numpy:1.13.1。所以我不认为这是问题所在。我不得不承认,my image 看起来仍然没有你的 C++ 图像那么流畅。我有一个问题:在 Python 中,你用
d_im[j-1,i,0]构造你的向量;在 C++ 中你写depth.at<double>(y-1, x)为什么在 C++ 中不需要三个索引?` -
好吧,在修改了代码并最终对图像进行了超级采样之后,我想到了查看原始图像的深度。我从您那里下载的图像是 8 位图像!因此,您在 Python 脚本中看到的是非常低的位深度和可怕的 jpeg 伪影的组合。您应该尝试获得更好的测试图像
-
这也解释了为什么我确实得到了更好的结果:SO 上的图像是 png;所以我没有 jpeg 人工制品