Matlab处理AppleProRaw

使用raw图工具打开AppleProRaw可以发现它是一个四通道线性rgba图像,博主用matlab写了一个处理线性rgb的simple pipe,仅供参考:

% apple pro raw
dng_file = 'D:/data/aeb/IMG_0001.dng';

% read meta
meta_info = imfinfo(dng_file);
wbc = reshape(meta_info.AsShotNeutral, [1 1 3]);
bl = meta_info.SubIFDs{1, 1}.BlackLevel(1);
wl = meta_info.SubIFDs{1, 1}.WhiteLevel(1);
tone_curve = meta_info.UnknownTags(3).Value;
tone_curve = reshape(tone_curve, [2, length(tone_curve)/2])';

% read linear rgb
im_lrgb = proraw_read(dng_file);
sz = size(im_lrgb);

% do wbc
im_wbc = single(im_lrgb)./repmat(wbc,[sz(1:2) 1]);
im_wbc = (im_wbc - bl)/wl;

% do ccm
rgb2xyz  = [0.4124564    0.3575761    0.1804375
            0.2126729    0.7151522    0.0721750
            0.0193339    0.1191920    0.9503041];
xyz2cam  = reshape(meta_info.ColorMatrix2,3,3)'; 
rgb2cam  = xyz2cam * rgb2xyz;
rgb2cam  = rgb2cam ./ repmat(sum(rgb2cam,2),1,3);
cam2rgb  = rgb2cam ^ -1;
ccm = cam2rgb;

im_ccm(:,:,1) = im_wbc(:,:,1) * ccm(1,1) + im_wbc(:,:,2) * ccm(1,2) + im_wbc(:,:,3) * ccm(1,3);
im_ccm(:,:,2) = im_wbc(:,:,1) * ccm(2,1) + im_wbc(:,:,2) * ccm(2,2) + im_wbc(:,:,3) * ccm(2,3);
im_ccm(:,:,3) = im_wbc(:,:,1) * ccm(3,1) + im_wbc(:,:,2) * ccm(3,2) + im_wbc(:,:,3) * ccm(3,3);

% baseline exposure
im_srgb = im_ccm*(2^meta_info.BaselineExposure);

% do tone
im_tone = interp1(tone_curve(:,1), tone_curve(:,2), im_srgb, 'spline');

% do gamma
im_gamma = im_tone.^(1/2.2);

从proraw中读取raw,使用的是rawpy, 我们用matlab调用rawpy

function [im_wbc,meta_info]=proraw_read(dng_file)
raw = py.rawpy.imread(dng_file);
img_lrgb= uint16(raw.raw_image);
img_lrgb = img_lrgb(:,:,1:3);

meta_info = imfinfo(dng_file);

wbc = reshape(meta__info.AsShotNeutral, [1,1,3]);
bl = meta_info.SubIFD{1,1}.BlackLevel(1);
wl = meta_info.SubIFD{1,1}.WhiteLevel(1);
sz = size(im_lrgb);

im_wbc = single(img_lrgb)./repmat(wbc, [sz(1:2) 1]);
im_wbc = (im_wbc-bl)/(wl-bl);
end

发表评论