티스토리 뷰
내가 하고 싶은 일은 lidar로 수집한 .bag file에서 x, y, z coordinate data를 추출하는 것 이다.
- .bag file 잘 생성됐는지 확인
- .bag을 .pcd로 변환
- .pcd에서 xyz 추출하여 csv로 저장
이 순서대로 진행하였다.
1. .bag file 잘 생성되었는지 확인
: 나는 sample bag file을 다른 분으로부터 얻어온 거라 이 파일이 제대로 잘 생성이 되었는지 일단 확인할 필요가 있었다.
https://pcl.gitbook.io/tutorial/part-0/part00-chapter03 에서 검증된 bag file을 다운 받아서 이 파일과 내 파일이 같게 동작하는지 확인하였다.
(rosbag으로 play 가능하고, rviz로 visualization 가능한지 확인하였다.)
2. .bag 를 .pcd로 변환
rosrun pcl_ros bag_to_pcd bagfile.bag /topics /dir
/topics에는 변환하고자 하는 topic을 쓴다. 나는 /velodyne_points 를 썼다.
/dir에는 pcd file을 저장한 경로를 쓴다.
이 때 Error: package pcl_ros not found 가 떴는데 이는 pcl_ros 를 설치해주면 해결된다.
sudo apt-get install ros-melodic-pcl-ros
나는 ros melodic을 쓰고 있어서 ros-melodic-pcl-ros를 apt-get으로 설치해주었다.
그리고 다시 변환을 위한 command를 실행하면 다음과 같은 문구들이 뜨면서 bag file이 pcd로 변환된다.
3. .pcd에서 xyz추출하여 csv로 저장
1) .pcd에서 xyz coordinate data 추출
일단 pcl-c++를 install 해주어야한다.
나는 다음과 같은 command를 이용했다.
wget https://github.com/PointCloudLibrary/pcl/archive/pcl-1.8.1.tar.gz
tar zvfx pcl-1.8.1.tar.gz
cd pcl-1.8.1
mkdir build && cd build
cmake ..
make -jall
sudo make -jall install
그리고 https://pcl.gitbook.io/tutorial/part-0/part00-chapter02 를 참고하여 pcl test code로 pcl이 잘 설치 되었는지 확인하고, xyz를 추출하는 코드를 https://pointclouds.org/documentation/tutorials/reading_pcd.html 여기서 가지고 왔다.
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
return -1;
}
std::cout << "Loaded " << cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: " << std::endl;
for (const auto& point: *cloud)
std::cout << point.x << " " << point.y << " " << point.z << std::endl;
return 0;
}
작성한 후 compile 해주면
다음과 같은 결과를 출력한다. compile을 위해 CMakeLists.txt도 https://pcl.gitbook.io/tutorial/part-0/part00-chapter02 여기서 가져왔는데 수정해야할 부분이 한가지 있었다.
#최소 요구 버젼
CMAKE_MINIMUM_REQUIRED (VERSION 2.8 FATAL_ERROR)
# 패키지 이름
PROJECT (Part00-Chapter02)
MESSAGE ( STATUS ${CMAKE_PROJECT_NAME} ) #( [<Type>] <메시지> )
# 변수 설정
SET(COMPILE_FLAGS "-std=c++11") #SET ( <변수명> <값> )
# 의존성 패키지
FIND_PACKAGE (PCL 1.2 REQUIRED) # 프로그램 실행시 필요한 패키지
# 없을 경우 에러 발생
# 헤더 디렉토리 지정 (-I)
INCLUDE_DIRECTORIES(
${PCL_INCLUDE_DIRS}
)
# 라이브러리 디렉토리 지정 (-L)
LINK_DIRECTORIES(${PCL_LIBRARY_DIRS})
# 전처리기 매크로 추가 (-D)
ADD_DEFINITIONS(${COMPILE_FLAGS})
ADD_DEFINITIONS(${PCL_DEFINITIONS})
# 생성할 실행 파일 옵션
ADD_EXECUTABLE (pcd_write_test pcd_write.cpp) # (<생성될 실행 파일명> <생성시 사용할 소스코드> )
# Target 링크 옵션 및 라이브러리 지정 (-l)
TARGET_LINK_LIBRARIES(pcd_write_test ${PCL_LIBRARIES}) #( <Target_이름> <라이브러리> <라이브러리> ... )
# 실행 파일생성하기에 앞서 링크 해야 하는 라이브러리와 실행 파일 링크
의존성 패키지 PCL 버전 본인이 설치한 패키지에 맞게 변경
FIND_PACKAGE (PCL 1.2 REQUIRED)
에서 나는 PCL 1.8 REQUIRED로 바꿔주었다. 나는 Pcl 1.8 version을 설치했기 때문이다.
2) xyz추출하여 csv로 저장
위에서 작성한 코드에서 file에 write 하는 코드 몇 줄만 추가해주면 된다.
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <fstream>
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
return -1;
}
std::cout << "Loaded " << cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: " << std::endl;
std::ofstream writeFile("xyz.csv", std::ios::out);
for (const auto& point: *cloud)
writeFile << point.x << ", " << point.y << ", " << point.z << "," << std::endl;
writeFile.close()
return 0;
}
그러면 드디어 한 pcd file을 csv로 변환되었다.
.bag file하나에서 추출한 pcd file은 굉장히 많기 때문에(물론 bag file마다 가지고 있는 data 양에 따라 다르겠지만) script를 짜서 한번에 다 변환 시켜야겠다.
'이것저것 자료 > 그 외' 카테고리의 다른 글
[Open-source build] M3DSSD build 하기 (0) | 2021.09.24 |
---|---|
[Apollo] Apollo 설치 command (0) | 2021.09.15 |
[Apollo-LGSVL] Apollo와 LGSVL 제대로 연결해서 제대로 simulation하기 (0) | 2021.09.07 |
[Troubleshooting doc] Pointnet++ build 중 만난 error들 (0) | 2021.07.26 |
[rosbag] rviz global option - Fixed Frame 이름 확인하기 (0) | 2021.06.15 |
- Total
- Today
- Yesterday
- error
- 동적프로그래밍
- 프로그래머스
- 다익스트라
- 카카오
- 설치하기
- BFS
- tensorflow
- 이것이코딩테스트다
- 파이썬
- shellscript
- dfs
- Python
- 코딩테스트
- pytorch
- PIP
- numpy
- torch
- 백준
- LGSVL
- 설치
- 백트래킹
- 최소신장트리
- n과m
- notfound
- docker
- CUDA
- torchscript
- matplotlib
- version
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |