基于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)
相关推荐
KaneLogger20 分钟前
AI模型与产品推荐清单20250709版
人工智能·程序员·开源
中电金信24 分钟前
中电金信 :十问高质量数据集:金融大模型价值重塑有“据”可循
人工智能·金融
吕永强25 分钟前
算法化资本——智能投顾技术重构金融生态的深度解析
人工智能·科普
新智元41 分钟前
奥特曼:再也不和小扎说话!OpenAI 偷袭小扎马斯克,反手挖 4 核心员工
人工智能·openai
新智元1 小时前
CS 专业爆冷,失业率达艺术史 2 倍!年入千万只需 5 年,大学却在禁 Cursor
人工智能·openai
代码能跑就行管它可读性1 小时前
【论文复现】利用生成式AI进行选股和分配权重
人工智能·chatgpt
阿里云大数据AI技术1 小时前
ODPS 15周年开发者活动|征文+动手实践双赛道开启,参与活动赢定制好礼!
大数据·人工智能·云计算
一颗小树x1 小时前
【机器人】复现 Aether 世界模型 | 几何感知统一 ICCV 2025
人工智能·机器人·世界模型·aether
Black_Rock_br1 小时前
语音交互新纪元:Hugging Face LeRobot如何让机器人真正“懂你”
人工智能·计算机视觉·机器人
1900_1 小时前
【论文解读】Referring Camouflaged Object Detection
人工智能·目标检测·计算机视觉