Main Content

rosbag 파일에서 라이다 데이터와 카메라 데이터 읽어오기

이 예제에서는 rosbag 파일에서 영상 데이터와 포인트 클라우드 데이터를 읽고 저장하는 방법을 보여줍니다. 또한 라이다-카메라 보정을 위해 데이터를 준비하는 방법도 보여줍니다.

이 예제의 끝부분에 정의된 helperDownloadRosbag 헬퍼 함수를 사용하여 rosbag 파일을 다운로드합니다.

path = helperDownloadRosbag;

rosbag 파일에서 정보를 가져옵니다.

bag = rosbag(path);

rosbag에서 영상 메시지와 포인트 클라우드 메시지를 선택하고 적절한 토픽 이름을 사용하여 이 파일에서 메시지의 일부를 선택합니다. 타임스탬프를 사용하여 데이터를 필터링할 수도 있습니다. 자세한 내용은 select (ROS Toolbox) 함수를 참조하십시오.

imageBag = select(bag,'Topic','/camera/image/compressed');
pcBag = select(bag,'Topic','/points');

모든 메시지를 읽어옵니다.

imageMsgs = readMessages(imageBag);
pcMsgs = readMessages(pcBag);

라이다-카메라 보정을 위해 데이터를 준비하려면, 두 센서의 데이터가 시간적으로 동기화되어야 합니다. 선택한 토픽에 대한 timeseries (ROS Toolbox) 객체를 만들고 타임스탬프를 추출합니다.

ts1 = timeseries(imageBag);
ts2 = timeseries(pcBag);
t1 = ts1.Time;
t2 = ts2.Time;

정확한 보정을 위해 영상과 포인트 클라우드는 동일한 타임스탬프로 캡처되어야 합니다. 두 센서의 타임스탬프에 따라 이 두 센서에서 대응하는 데이터를 매칭합니다. 불확실성을 고려하기 위해 0.1초의 여유를 사용합니다.

k = 1;
if size(t2,1) > size(t1,1)
    for i = 1:size(t1,1)
        [val,indx] = min(abs(t1(i) - t2));
        if val <= 0.1
            idx(k,:) = [i indx];
            k = k + 1;
        end
    end
else
    for i = 1:size(t2,1)
        [val,indx] = min(abs(t2(i) - t1));
        if val <= 0.1
            idx(k,:) = [indx i];
            k = k + 1;
        end
    end
end

유효한 영상과 포인트 클라우드를 저장하기 위한 디렉터리를 만듭니다.

pcFilesPath = fullfile(tempdir,'PointClouds');
imageFilesPath = fullfile(tempdir,'Images');
if ~exist(imageFilesPath,'dir')
    mkdir(imageFilesPath);
end
if ~exist(pcFilesPath,'dir')
    mkdir(pcFilesPath);
end

영상과 포인트 클라우드를 추출합니다. 파일의 이름을 지정하고 각 폴더에 파일을 저장합니다. 대응하는 영상과 포인트 클라우드를 동일한 번호에 저장합니다.

for i = 1:length(idx)
    I = readImage(imageMsgs{idx(i,1)});
    pc = pointCloud(readXYZ(pcMsgs{idx(i,2)}));
    n_strPadded = sprintf('%04d',i) ;
    pcFileName = strcat(pcFilesPath,'/',n_strPadded,'.pcd');
    imageFileName = strcat(imageFilesPath,'/',n_strPadded,'.png');
    imwrite(I,imageFileName);
    pcwrite(pc,pcFileName);
end

라이다 카메라 보정기 앱을 실행하고 인터페이스를 사용하여 데이터를 앱으로 불러옵니다. 또한 데이터를 불러오고 MATLAB® 명령줄에서 이 앱을 실행할 수도 있습니다.

checkerSize = 81; %millimeters
padding = [0 0 0 0];
lidarCameraCalibrator(imageFilesPath,pcFilesPath,checkerSize,padding)

지원 함수

function rosbagFile = helperDownloadRosbag()
% Download the data set from the given URL.
rosbagZipFile = matlab.internal.examples.downloadSupportFile( ...
    'lidar','data/lccSample.zip');
[outputFolder,~,~] = fileparts(rosbagZipFile);
rosbagFile = fullfile(outputFolder,'lccSample.bag');
if ~exist(rosbagFile,'file')
    unzip(rosbagZipFile,outputFolder);
end
end