对于转换,我有下一个公式:
normalized = (R + G * 256 + B * 256 * 256) / (256 * 256 * 256 - 1)
in_meters = 1000 * normalized
cv::Mat ConvertRawToDepth(cv::Mat raw_image)
{
// raw_image.type() => CV_8UC3
// Extend raw image to 2 bytes per pixel
cv::Mat raw_extended = cv::Mat::Mat(raw_image.rows, raw_image.cols, CV_16UC3, raw_image.data);
// Split into channels
std::vector<cv::Mat> raw_ch(3);
cv::split(raw_image, raw_ch); // B, G, R
// Create and calculate 1 channel gray image of depth based on the formula
cv::Mat depth_gray = cv::Mat::zeros(raw_ch[0].rows, raw_ch[0].cols, CV_32FC1);
depth_gray = 1000.0 * (raw_ch[2] + raw_ch[1] * 256 + raw_ch[0] * 65536) / (16777215.0);
// Create final BGR image
cv::Mat depth_3d;
cv::cvtColor(depth_gray, depth_3d, cv::COLOR_GRAY2BGR);
return depth_3d;
}
def convert_raw_to_depth(raw_image):
raw_image = raw_image[:, :, :3]
raw_image = raw_image.astype(np.float32)
# Apply (R + G * 256 + B * 256 * 256) / (256 * 256 * 256 - 1).
depth = np.dot(raw_image, [65536.0, 256.0, 1.0])
depth /= 16777215.0 # (256.0 * 256.0 * 256.0 - 1.0)
depth *= 1000
return depth
实现下一个结果:
很明显,在python中它做得更好,但是公式是一样的,图像是一样的,那么为什么会有区别呢?我如何用C++重写代码,以获得与python类似的结果?