基于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)
相关推荐
Elastic 中国社区官方博客6 分钟前
Observability:适用于 PHP 的 OpenTelemetry:EDOT PHP 加入 OpenTelemetry 项目
大数据·开发语言·人工智能·elasticsearch·搜索引擎·全文检索·php
神仙别闹20 分钟前
基于 C++和 Python 实现计算机视觉
c++·python·计算机视觉
ytttr87331 分钟前
Landweber迭代算法用于一维、二维图像重建
人工智能·算法·机器学习
feifeigo12339 分钟前
Matlab编写压缩感知重建算法集
人工智能·算法·matlab
紫小米1 小时前
提示词(Prompt)工程与推理优化
人工智能·ai·prompt·ai agent
Shang180989357261 小时前
T41NQ/T41N高性能低功耗SOC芯片 软硬件资料T41NQ适用于各种AIoT应用,适用于智能安防、智能家居,机器视觉等领域方案
驱动开发·嵌入式硬件·计算机视觉·fpga开发·信息与通信·t41nq
子非鱼9211 小时前
1 NLP导论及环境准备
人工智能·自然语言处理
狠活科技1 小时前
Claude Code 重大更新:支持一键原生安装,彻底别了 Node.js
人工智能·aigc·ai编程·claude·claude code
mwq301232 小时前
解密“混合专家模型” (MoE) 的全部魔法
人工智能·llm
能来帮帮蒟蒻吗3 小时前
深度学习(2)—— 神经网络与训练
人工智能·深度学习·神经网络