opencv--3d数据拟合平面并对倾斜平面矫正

2024-06-03 20:36

本文主要是介绍opencv--3d数据拟合平面并对倾斜平面矫正,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

对于深度数据而言,mat记录的是深度值,当对深度值进行各种处理,例如获取直线、圆、椭圆等其他形状时,如果平面没有完全水平,你使用opencv处理精度是有损失的,因此这里使用opencv 先对平面进行矫正,矫正原理是在有效平面内随机采集3000点的深度数据,使用深度数据进行拟合平面,计算平面的倾斜较大,然后使用角度对原始数据进行矫正,代码如下:

// 将深度图转换为点云
std::vector<cv::Point3f> depthToPointCloud(const cv::Mat& depthImage) {std::vector<cv::Point3f> pointCloud;// 设置随机数生成器std::random_device rd;std::mt19937 gen(rd());std::uniform_int_distribution<> distRow(0, depthImage.rows - 1);std::uniform_int_distribution<> distCol(0, depthImage.cols - 1);// 要提取的像素点数量int numPixels = 2000; // 你可以根据需要调整这个数量// 存储随机提取的像素点std::vector<cv::Vec3b> randomPixels;for (int i = 0; i < numPixels; ++i) {int y = distRow(gen);int x = distCol(gen);//randomPixels.push_back(image.at<cv::Vec3b>(row, col));cv::Point3f point((float)x, (float)y, depthImage.at<float>(y, x)); // 构造三维点pointCloud.push_back(point); // 添加到点云}//for (int y = 0; y < depthImage.rows; ++y) {//    for (int x = 0; x < depthImage.cols; ++x) {//        float depth = depthImage.at<float>(y, x); // 获取深度值//        //if (std::isnan(depth) || depth <= 0.0) {//        //    continue; // 跳过无效深度值//        //}//        cv::Point3f point((float)x, (float)y, depth); // 构造三维点//        pointCloud.push_back(point); // 添加到点云//    }//}return pointCloud;
}// 函数:拟合平面
cv::Vec4f fitPlaneRANSAC(const std::vector<cv::Point3f>& points, int maxIter = 1000, float threshold = 0.01) {if (points.empty())return cv::Vec4f();cv::Vec4f bestPlane;int bestInliers = 0;for (int iter = 0; iter < maxIter; ++iter) {// 随机选择三个点std::vector<int> indices(points.size());std::iota(indices.begin(), indices.end(), 0);std::shuffle(indices.begin(), indices.end(), std::default_random_engine(5));cv::Point3f p1 = points[indices[0]];cv::Point3f p2 = points[indices[1]];cv::Point3f p3 = points[indices[2]];// 计算平面的法向量cv::Point3f v1 = p2 - p1;cv::Point3f v2 = p3 - p1;cv::Point3f normal = v1.cross(v2);normal /= cv::norm(normal);// 平面公式:Ax + By + Cz + D = 0float D = -normal.dot(p1);cv::Vec4f plane(normal.x, normal.y, normal.z, D);// 计算内点数量int inliers = 0;for (const auto& point : points) {float distance = std::abs(plane[0] * point.x + plane[1] * point.y + plane[2] * point.z + plane[3]);if (distance < threshold) {inliers++;}}// 更新最佳平面if (inliers > bestInliers) {bestInliers = inliers;bestPlane = plane;}}return bestPlane;
}// 函数:矫正平面
void correctPlane(const cv::Vec4f& plane, cv::Mat& points) {cv::Mat normal_m, normal_m_8;cv::normalize(points, normal_m, 1, 0, cv::NORM_MINMAX);normal_m.convertTo(normal_m_8, CV_8U, 255.0);cv::Vec3f normal(plane[0], plane[1], plane[2]);cv::Vec3f zAxis(0, 0, 1);cv::Vec3f rotationAxis = normal.cross(zAxis);float angle = std::acos(normal.dot(zAxis) / (cv::norm(normal) * cv::norm(zAxis)));cv::Mat rotationMatrix;cv::Rodrigues(rotationAxis * angle, rotationMatrix);for (int i = 0; i < points.rows; ++i) {cv::Vec3f point = points.at<cv::Vec3f>(i);// 将 point 转换为 cv::Mat 类型cv::Mat pointMat = (cv::Mat_<float>(3, 1) << point[0], point[1], point[2]);// 矩阵乘法cv::Mat transformedPointMat = rotationMatrix * pointMat;// 将结果转换回 cv::Vec3f 类型points.at<cv::Vec3f>(i) = cv::Vec3f(transformedPointMat.at<float>(0), transformedPointMat.at<float>(1), transformedPointMat.at<float>(2));}cv::Mat normal_m_, normal_m_8_;cv::normalize(points, normal_m_, 1, 0, cv::NORM_MINMAX);normal_m_.convertTo(normal_m_8_, CV_8U, 255.0);}

从矫正前的数据和矫正后的数据可以发现,平面得到了很好得了很好的矫正。

这篇关于opencv--3d数据拟合平面并对倾斜平面矫正的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1028046

相关文章

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

在PyCharm中安装PyTorch、torchvision和OpenCV详解

《在PyCharm中安装PyTorch、torchvision和OpenCV详解》:本文主要介绍在PyCharm中安装PyTorch、torchvision和OpenCV方式,具有很好的参考价值,... 目录PyCharm安装PyTorch、torchvision和OpenCV安装python安装PyTor

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用