title: Issues on Height Map to Normal Map with Python OpenCV
body: <p>I'm trying to generate a tangent space normal map from a height map using <code>opencv</code> and <code>python</code> following <a href="https://squircleart.github.io/shading/normal-map-generation.html" rel="nofollow noreferrer">this tutorial</a>.</p>
  
  <p>The intermediate steps seem to be fine, still I struggle with the final image. Besides the fact that I cannot merge my output-</p>
  
  <p>Maybe somebody has an idea what I am doing wrong?</p>
  
  <p>I use this as an example image:</p>
  
  <p><a href="https://i.stack.imgur.com/QgT5P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QgT5P.png" alt="enter image description here"></a></p>
  
  <p>This is my code:</p>
  
  <pre><code>#!/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 )
  </code></pre>