Windows下使用pybind11编译KMSolver

本文讲述了作者试图在Windows11环境下移植一个基于纯卷积RCNN的相机重定位项目,遇到的问题在于一个预编译的库KMSolver只支持Linux和Python3.7。作者计划使用pybind11在Windows上重新编译,并详细描述了所需工具和编译步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近搞相机重定位的项目,大多都是在linux系统上写的,使用windows配环境就非常多问题

项目地址:GitHub - jinlinyi/SparsePlanes: [ICCV 2021 (oral)] Planar Surface Reconstruction from Sparse Views

虽然不是最新的研究成果,效果还是不错的,基于纯卷积RCNN的方法。

项目在linux下运行正常,主要是想在windows下使用这个代码,其中有个

KMSolver.cpython-37m-x86_64-linux-gnu.so

的库是预编译好的,仅支持特定的python3.7和linux环境,所以这里想在windows11下用pybind11编译KMSolver用其他版本的python调用C++代码

编译工具:

cmake 3.29.0

vs 2019

pybind11:GitHub - pybind/pybind11: Seamless operability between C++11 and Python

准备代码:

代码地址:GitHub - RoboJackets/hungarian: C++ Implementation of the hungarian algorithm

下载好后需要对代码做一些修改:

新建一个 KMSolver.cpp

#include "../include/pybind11/pybind11.h"
#include "../include/pybind11/eigen.h"
#include "../include/pybind11/stl.h"

#include <iostream>
#if defined(_MSC_VER)
#  pragma warning(disable: 4996) // C4996: std::unary_negation is deprecated
#endif

#include <vector>
#include "hungarian.hpp"

namespace py = pybind11;

Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> solve(Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> costMatrix, int threshold) {
	int num_rows = costMatrix.rows();
	int num_cols = costMatrix.cols();

	// create a input matrix to km
	// note cost above threshold are considered invalid match right away
	// and they are treated to be "equally invalid", remaining the default value threshold+1
	// to avoid affect matching with the valid costs
	Hungarian::Matrix cost2DVec (num_rows, std::vector<int>(num_cols, threshold+1));
	for (int i = 0; i < num_rows; i++) {
		for (int j = 0; j < num_cols; j++) {
			if (costMatrix(i,j) <= threshold) {			
				cost2DVec[i][j] = costMatrix(i,j);
			}
		}
	}

	// solve KM
	Hungarian::Result res2DVec = Hungarian::Solve(cost2DVec, Hungarian::MODE_MINIMIZE_COST);
	if (!res2DVec.success) {
		std::cout << "Matching not successful. Return input matrix itself." << std::endl;
		return costMatrix;
	}

	// convert 2d std vector to eigen matrix
	Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> matchMatrix (num_rows, num_cols);
	for (int i = 0; i < num_rows; i++) {
		for (int j = 0; j < num_cols; j++) {
			if (res2DVec.assignment[i][j] == 1 && cost2DVec[i][j] <= threshold) {
				// only accept a match if
				// KM agorithm assigns a match and
				// the match can be valid in the first place (cost <= threshold)
				matchMatrix(i,j) = 1;
			} else {
				matchMatrix(i,j) = 0;
			}			
		}
	}

	return matchMatrix;
}

namespace py = pybind11;

PYBIND11_MODULE(KMSolver, m) {
    m.def("solve", &solve, py::arg("cost_matrix"), py::arg("threshold"));
}



 编辑CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project("Hungarian Algorithm")

set(CMAKE_CXX_STANDARD 11)

FIND_PACKAGE(Eigen3 3.1.0 REQUIRED)
include_directories( ${EIGEN3_INCLUDE_DIRS} )

add_library(hungarian hungarian.cpp hungarian.hpp KMSolver.cpp)

add_executable(test_cases test.cpp) # test.cpp怎么写的不重要
target_link_libraries(test_cases KMSolver)

还需要准备eigen库,一个用于矩阵运算的库,用之前最好用cmake去build一下,以免找不到

GitHub - libigl/eigen: This is a mirror of the latest stable version of Eigen.

在当前路径下创建一个build目录

 打开cmake路径和编译器配置好后点生成,之后在build路径下找到Hungarian Algorithm.sln,并双击打开。

点开Hungarian的属性,在常规中做如下设置

 更改扩展名

添加include和库路径

包含目录:
G:\DLproject\py_build_tool\pybind11
G:\DLproject\py_build_tool\pybind11\include # pybind11的路径
E:\xxx\xxx\anaconda3\envs\sparseplane_py39\include  # 根据自己的python环境来确定
G:\DLproject\SparsePlanes\km\eigen   # 这里将eigen放在编译时的根路径下
库目录:
E:\xxx\xxx\anaconda3\envs\sparseplane_py39\libs

 增加链接器的附加依赖

 这个依赖主要根据库目录中的pythonlib来确定

之后直接生成 

一般按上面的步骤来不会报错,在build/release下能看到对应的pyd文件

将该文件复制到SparsePlanes\sparsePlane下,替换之前的.so文件

sparsePlane的windows环境配置好后就能直接在windows上运行了

上面的报错是pytorch3d的问题,具体问题还得折腾一下 

代码运行完会在tools/debug下保存处理好的文件,包括mask、匹配的结果、渲染好的3d文件以及对应的贴图。obj文件可以用3D建图工具打开,比如blender,这里因为pytorch3d有问题,3d相关的文件没能保存下来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

athrunsunny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值