深度图和RGB图对齐

深度图(canny):
在这里插入图片描述
RGB(canny):
在这里插入图片描述

Alignment:
在这里插入图片描述

code:

import cv2
import numpy as np

from matplotlib import pyplot as plt 

import threading

imgdd = cv2.imread('./rgbd.jpg', 0) 
imgg  = cv2.imread('./my.png', 0)
imgd = cv2.Canny(imgdd,100,200) 
img  = cv2.Canny(imgg,100,200) 

def parm_adjust_th():  
    cv2.namedWindow('Pose_Adjuster')
    cv2.createTrackbar('pos_x', 'Pose_Adjuster', 159, 200 * 2, callback)
    cv2.createTrackbar('pos_y', 'Pose_Adjuster', 80, 200 * 2, callback)
    cv2.createTrackbar('scale', 'Pose_Adjuster', 186, 200, callback)  

    while(True):
        pos_x = (cv2.getTrackbarPos('pos_x', 'Pose_Adjuster') - 200) 
        pos_y = (cv2.getTrackbarPos('pos_y', 'Pose_Adjuster') - 200)
        scale = (cv2.getTrackbarPos('scale', 'Pose_Adjuster') )

        # print(img.shape)
        # print(imgd.shape[0], imgd.shape[1]) 

        # global trans
        trans = [pos_x, pos_y, scale]
        print("===>>> Trans : ", trans)
        scale_percent = trans[2] # percent of original size
        width = int(imgd.shape[1] * scale_percent / 100)
        height = int(imgd.shape[0] * scale_percent / 100)
        dim = (width, height)
        resized = cv2.resize(imgd, dim, interpolation = cv2.INTER_AREA) 
        # print(resized.shape)
        # print(img.shape)
        # print("===>>> Trans : ", trans)
        for u in range(min(len(resized[:,0]), img.shape[0])):
            for v in range(min(len(resized[0,:] - 1), img.shape[1])): 
                # print(u, v, img.shape[0], img.shape[1])
                if resized[u, v] != 0 and trans[1]+ u < img.shape[0] and trans[0]+v < img.shape[1]:
                    img[trans[1]+ u, trans[0]+v] = resized[u, v]
        # print(trans[1], trans[1]+ imgd.shape[0], trans[0], trans[0]+imgd.shape[1])

        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
        cv2.imshow('Pose_Adjuster', img)
        cv2.imwrite("cnny_align.png", img)
    cv2.destroyAllWindows()  

def callback(object):
    pass
  
if __name__ == '__main__':  
    parm_adjust_th()
 
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现对齐深度图像和RGB像,可以使用OpenCV库中的函数cv2.warpAffine()或cv2.warpPerspective()。这些函数可以将一幅像变换为另一幅像,可以应用于对齐深度图像和RGB像。 以下是实现对齐深度图像和RGB像的步骤: 1. 加载深度图像和RGB像。 2. 创建一个旋转矩阵和平移向量,以将RGB对齐深度图像中。 3. 使用cv2.warpAffine()或cv2.warpPerspective()函数将RGB像进行变换,使其与深度图对齐。 下面是一个简单的示例代码: ``` import cv2 import numpy as np # 加载深度图像和RGB像 depth_img = cv2.imread('depth_img.png', cv2.IMREAD_GRAYSCALE) rgb_img = cv2.imread('rgb_img.png') # 设置旋转矩阵和平移向量 rotation_matrix = np.eye(3) translation_vector = np.array([0, 0, 0]) # 创建变换矩阵 transform_matrix = np.hstack((rotation_matrix, translation_vector.reshape(3, 1))) transform_matrix = np.vstack((transform_matrix, [0, 0, 0, 1])) # 将RGB像变换为深度图像的大小和方向 transformed_rgb = cv2.warpPerspective(rgb_img, transform_matrix, (depth_img.shape[1], depth_img.shape[0])) # 显示变换后的RGB像和深度图像 cv2.imshow('Transformed RGB', transformed_rgb) cv2.imshow('Depth', depth_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在以上代码中,我们首先加载深度图像和RGB像。接着创建一个旋转矩阵和平移向量,用于将RGB对齐深度图像中。然后,我们使用cv2.warpPerspective()函数将RGB像进行变换,使其与深度图对齐。最后,我们显示变换后的RGB像和深度图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值