I am trying to compute surface normals in OpenCV. Well, this should be quick and easy but I don't know why it is not working. Below is the code:
> import cv2
> img_color = cv2.imread("color.png")
> img_depth = cv2.imread("depth.png", cv2.CV_16UC1) # millimeters
> img_color.shape, img_color.dtype
((720, 1280, 3), dtype('uint8'))
> img_depth.shape, img_depth.dtype
((720, 1280), dtype('uint16'))
> k = np.array([[ fx, 0, cx ],
[ 0, fy, cy ],
[ 0, 0, 1 ]])
> k
array([[900, 0, 640],
[ 0, 900, 360],
[ 0, 0, 1]])
> img_depth_m = img_depth.astype('float32') * 0.001 # meter
> rows, cols = img_depth_m.shape
> normal_estimator = cv2.rgbd.RgbdNormals_create(rows, cols, cv2.CV_32F, k, 3)
> normals = normal_estimator.apply( img_depth_m )
It throws the following error:
error Traceback (most recent call last)
/tmp/ipykernel_19178/1208043521.py in <module>
----> 4 normals = normal_estimator.apply( img_depth_m )
error: OpenCV(4.2.0) ../contrib/modules/rgbd/src/normal.cpp:776:
error: (-215:Assertion failed) points3d_ori.channels() == 3 in function 'operator()'
It seems that OpenCV is expecting a 3 Channel input matrix. However, I looked at the docstring in the notebook, it says:
Docstring:
apply(points[, normals]) -> normals
. Given a set of 3d points in a depth image, compute the normals at each point.
. * @param points a rows x cols x 3 matrix of CV_32F/CV64F or a rows x cols x 1 CV_U16S
. * @param normals a rows x cols x 3 matrix
Type: builtin_function_or_method
Anyway, how to compute surface normals using camera intrinsic matrix in OpenCV?
PS: I am using OpenCV v4.2.0 on Ubuntu 20.04 LTS.