代码之家  ›  专栏  ›  技术社区  ›  goetzmoritz

关于用python opencv将高度映射到法向映射的问题

  •  1
  • goetzmoritz  · 技术社区  · 6 年前

    我正在尝试从高度图生成一个切线空间法向图,使用 opencv python 下列的 this tutorial .

    中间的步骤似乎很好,但我仍在为最终的形象苦苦挣扎。此外,我不能合并我的输出-

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

    我将此作为示例图像:

    enter image description here

    这是我的代码:

    #!/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 )
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   nicoptere    6 年前

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

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