Skip to content

Instantly share code, notes, and snippets.

@grapeot
Last active Dec 23, 2015
Embed
What would you like to do?
MATLAB code to rectify the RGB and depth photos directly exported from Microsoft Kinect SDK. We assume the file `imgfn` stores the binary data directly exported from `ColorImageFrame.CopyPixelDataTo()`, `dfn` stores the binary data exported from `FusionFloatImageFrame.CopyPixelDataTo()`, and `idxfn` stores the binary data exported from the `Colo…
function newImg = register(imgfn, dfn, idxfn)
% rectify the color image according to the depth and the alignment
% information from the data collection program
% Consult http://grapeot.me/some-technical-details-about-kinectfusion.html for more details.
%% read in the data
fp = fopen(imgfn, 'r');
imgRaw = fread(fp, 'char');
imgRaw = reshape(imgRaw, 4, 640, 480);
for i = 1: 3
img(:, :, i) = reshape(imgRaw(4 - i, :, :), 640, 480)';
end
fclose(fp);
fp = fopen(dfn, 'r');
d = fread(fp, 'single');
d = reshape(d, 640, 480)';
fclose(fp);
fp = fopen(idxfn, 'r');
idx = fread(fp, 'int');
idx = reshape(idx, 2, 640, 480);
ii = find(idx(1, :) >= 0 & idx(2, :) >= 0);
fclose(fp);
%% do the actual alignment
newImg = zeros(size(img));
for i = ii
oldy = floor(i / 640) + 1;
oldx = mod(i, 640) + 1;
y = idx(2, i) + 1;
x = idx(1, i) + 1;
newImg(oldy, oldx, :) = img(y, x, :);
end
for i = 1: 3
newImg(:, :, i) = fliplr(newImg(:, :, i)) / 255;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment