0

I'm trying to generate a tangent space normal map from a height map using opencv and python following this tutorial.

The intermediate steps seem to be fine, still I struggle with the final image. Besides the fact that I cannot merge my output-

Maybe somebody has an idea what I am doing wrong?

I use this as an example image:

enter image description here

This is my 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 )

1 Answer 1

2

you probably found out already but in case it helps someone else, you can use the cv2.merge method to pack your channels together:

new_rgb = cv2.merge((B, G, R))
return new_rgb
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I actually found that out. Maybe I should have posted it, but I forgot about that. Sorry! And Thanks for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.