基于Sparse Optical Flow 的Homography estimation

python 复制代码
import copy
import time

import cv2
import numpy as np


def draw_kpts(image0, image1, mkpts0, mkpts1, margin=10):
    H0, W0 = image0.shape
    H1, W1 = image1.shape
    H, W = max(H0, H1), W0 + W1 + margin
    out = 255 * np.ones((H, W), np.uint8)
    out[:H0, :W0] = image0
    out[:H1, W0+margin:] = image1
    out = np.stack([out]*3, -1)

    mkpts0, mkpts1 = np.round(mkpts0).astype(int), np.round(mkpts1).astype(int)
    # print(f"mkpts0.shape : {mkpts0.shape}")
    c = (0, 255, 0)
    for (new, old) in zip(mkpts0, mkpts1):
        x0, y0 = new.ravel()
        x1, y1 = old.ravel()
        # print(f"x0 : {x0}")
        # cv2.line(out, (x0, y0), (x1 + margin + W0, y1),
        #         color=c, thickness=1, lineType=cv2.LINE_AA)
        # display line end-points as circles
        cv2.circle(out, (x0, y0), 2, c, -1, lineType=cv2.LINE_AA)
        cv2.circle(out, (x1 + margin + W0, y1), 2, c, -1,
                lineType=cv2.LINE_AA)
        
    return out

if __name__ == "__main__":
    img0Path = "/training/datasets/orchard/orchard_imgs_/000130.jpg"
    img1Path = "/training/datasets/orchard/orchard_imgs_/000132.jpg"

    img0 = cv2.imread(img0Path, 0)
    img1 = cv2.imread(img1Path, 0)
    h, w = img0.shape

    mask = np.zeros_like(img0)
    mask[int(0.02 * h): int(0.98 * h), int(0.02 * w): int(0.98 * w)] = 255

    keypoints = cv2.goodFeaturesToTrack(
                img0,
                mask=mask,
                maxCorners=2048,
                qualityLevel=0.01,
                minDistance=1,
                blockSize=3,
                useHarrisDetector=False,
                k=0.04
            )
    print(f"keypoints , size : {keypoints.shape}")

    next_keypoints, status, err = cv2.calcOpticalFlowPyrLK(
                img0, img1, keypoints, None
            )
    
    H, _ = cv2.estimateAffinePartial2D(
                keypoints, next_keypoints, cv2.RANSAC
            )
    
    print(f"H : {H}")

    out = draw_kpts(img0, img1, keypoints , next_keypoints)
    cv2.imwrite("keypoints.jpg", out)
相关推荐
行走的小派26 分钟前
引爆AI智能体时代!OPi 6Plus全面适配OpenClaw
人工智能
云边有个稻草人27 分钟前
CANN:解构AIGC底层算力,ops-nn驱动神经网络算子加速
人工智能·神经网络·aigc·cann
爱吃大芒果27 分钟前
CANN神经网络算子库设计思路:ops-nn项目的工程化实现逻辑
人工智能·深度学习·神经网络
人工智能培训38 分钟前
具身智能如何让智能体理解物理定律?
人工智能·多模态学习·具身智能·ai培训·人工智能工程师·物理定律
lili-felicity38 分钟前
CANN加速Stable Diffusion文生图推理:从UNet优化到内存复用
人工智能·aigc
哈__38 分钟前
CANN加速语音合成TTS推理:声学模型与声码器优化
人工智能
哈__40 分钟前
CANN加速VAE变分自编码器推理:潜在空间重构与编码解码优化
人工智能·深度学习·重构
美狐美颜SDK开放平台43 分钟前
多终端适配下的人脸美型方案:美颜SDK工程开发实践分享
人工智能·音视频·美颜sdk·直播美颜sdk·视频美颜sdk
哈__1 小时前
CANN加速Image Captioning图像描述生成:视觉特征提取与文本生成优化
人工智能
禁默1 小时前
Ops-Transformer深入:CANN生态Transformer专用算子库赋能多模态生成效率跃迁
人工智能·深度学习·transformer·cann