使用 Python OpenCV 将高度图转换为法线图的问题

Issues on Height Map to Normal Map with Python OpenCV

我正在尝试使用 this tutorial 之后的 opencvpython 从高度图生成切线 space 法线图。

中间步骤看起来还不错,但我仍然对最终图像感到困惑。除了我无法合并我的输出-

也许有人知道我做错了什么?

我用这个作为示例图片:

这是我的代码:

#!/usr/bin/env python
from __future__ import division
import cv2 as cv
import numpy as np
import math
from matplotlib import pyplot as plt


img = cv.imread('sourceimage.jpg')

gray_image = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
cv.imwrite( "grey.png", gray_image )

rows,cols = gray_image.shape

M1 = np.float32([ [1,0, 1], [0,1, 0] ])
M2 = np.float32([ [1,0,-1], [0,1, 0] ])
M3 = np.float32([ [1,0, 0], [0,1,1] ])
M4 = np.float32([ [1,0, 0], [0,1,-1] ])

temp1 = cv.warpAffine(gray_image,M1,(cols,rows), borderMode = cv.BORDER_WRAP)
temp2 = cv.warpAffine(gray_image,M2,(cols,rows), borderMode = cv.BORDER_WRAP)
temp3 = cv.warpAffine(gray_image,M3,(cols,rows), borderMode = cv.BORDER_WRAP)
temp4 = cv.warpAffine(gray_image,M4,(cols,rows), borderMode = cv.BORDER_WRAP)

dx = cv.subtract(temp1, temp2)
dy = cv.subtract(temp3, temp4)

dxNeg = dx * -1
dyNeg = dy * -1

dxSquare = np.power(dx, 2)
dySquare = np.power(dy, 2)

nxSquareRoot = np.sqrt(dxSquare + dxSquare + 1)
nySquareRoot = np.sqrt(dySquare + dySquare + 1)
nzSquareRoot = np.sqrt(dxSquare + dxSquare + 1)

nx = np.divide(dxNeg,nxSquareRoot)
ny = np.divide(dyNeg,nySquareRoot)
nz = np.divide(dxNeg,nzSquareRoot)

R = np.divide(nx +1,2)
G = np.divide(ny +1,2)
B = nx

new_rgb = np.stack(R,G,B)

cv.imwrite( "output.jpg", new_rgb )

您可能已经发现了,但如果它对其他人有帮助,您可以使用 cv2.merge 方法将您的频道打包在一起:

new_rgb = cv2.merge((B, G, R))
return new_rgb