目录
- 一、固定向量类
-
- 二、点云着色
- 四、结果展示
-
- 五、参考链接
一、固定向量类
1、cv::Vec
在OpenCV中针对三通道矩阵,定义的Vec类型有:cv::Vec3b
、cv::Vec3s
、cv::Vec3w
、cv::Vec3d
、cv::Vec3f
、cv::Vec3i
6种类型。其中的数字表示通道个数,最后一位是数据类型的缩写。
cv::Vec3b
:b是uchar
类型的缩写。cv::Vec3s
:s是short
类型的缩写。cv::Vec3w
:w是ushort
类型的缩写。cv::Vec3d
:d是double
类型的缩写。cv::Vec3f
:f是float
类型的缩写。cv::Vec3i
:i是int
类型的缩写。
对于二通道和四通道也定义了对应的变量类型,也有同样的命名规则。例如:cv::Vec2b
表示二通道uchar
类型。
2、读取像素
由于在OpenCV中,使用imread
读取到的Mat
图像数据,都是用uchar
类型的数据存储,对于RGB
三通道的图像,每个点的数据都是一个Vec3b
类型的数据。
使用at
定位方法如下:
Mat mat = imread("test.jpg");
mat.at<Vec3b>(row, col)[0] = 255;
mat.at<Vec3b>(row, col)[1] = 255;
mat.at<Vec3b>(row, col)[2] = 255;
二、点云着色
将双目相机的图像色彩添加到点云上生成彩色点云(PointXYZRGB点云)
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <opencv2/opencv.hpp>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std;
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("test.pcd", *cloud) == -1)
{
PCL_ERROR("读取点云失败 \n");
return (-1);
}
cv::Mat img = cv::imread("test.jpeg");
if (img.empty())
{
cout << "请确认图像文件名称是否正确" << endl;
return -1;
}
cv::imshow("image", img);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointcloud(new pcl::PointCloud<pcl::PointXYZRGB>);
for (int r = 0; r < img.rows; r++)
{
for (int c = 0; c < img.cols; c++)
{
pcl::PointXYZRGB point;
point.x = cloud->points[r * img.cols + c].x;
point.y = cloud->points[r * img.cols + c].y;
point.z = cloud->points[r * img.cols + c].z;
point.r = img.at<cv::Vec3b>(r, c)[2];
point.g = img.at<cv::Vec3b>(r, c)[1];
point.b = img.at<cv::Vec3b>(r, c)[0];
pointcloud->push_back(point);
}
}
pcl::io::savePCDFileASCII("RGB真彩点云.pcd", *pointcloud);
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(pointcloud);
viewer->addPointCloud<pcl::PointXYZRGB>(pointcloud, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
viewer->addCoordinateSystem(1000);
viewer->initCameraParameters();
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return 0;
}
四、结果展示
1、图像
2、点云
3、彩色点云
五、参考链接
【OpenCV】关于Vec3b类型的含义与使用
点云与RGB融合
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)