ISP流程(matlab)
程序员文章站
2022-04-26 22:12:30
...
clc
clear
%%
%通过dcraw将.nef转换成tiff格式
raw=im2double(imread('nikon_testim.tiff'));
figure;imshow(raw);title("原图");
%线性化处理
black=0;
saturation = max(max(raw));
lin_bayer=(raw-black)/(saturation-black);
lin_bayer=max(0,min(lin_bayer,1));
figure;imshow(lin_bayer);title("线性化后图像");
%白平衡校正
wb_multipliers = [1.842952 1.000000 1.133930]; % for particular condition, from dcraw;
mask = wbmask(size(lin_bayer,1),size(lin_bayer,2),wb_multipliers,'rggb');
balanced_bayer = lin_bayer .* mask;
figure;imshow(balanced_bayer);title("白平衡后图像");
%去马赛克
temp = uint16(balanced_bayer/max(balanced_bayer(:)) * (2^16-1));
lin_rgb = im2double(demosaic(temp,'rggb'));
figure;imshow(lin_rgb);title("去马赛克后图像");
%颜色空间转换
sRGB2XYZ = [0.4124564 0.3575761 0.1804375;0.2126729 0.7151522 0.0721750;0.0193339 0.1191920 0.9503041];
% sRGB2XYZ is an unchanged standard
XYZ2Cam = [7171 -1986 -648;-8085 15555 2718;-2170 2512 7457]/10000;
% Here XYZ2Cam is only for Nikon D3X, can be found in adobe_coeff in dcraw.c
sRGB2Cam = XYZ2Cam * sRGB2XYZ;
sRGB2Cam = sRGB2Cam./ repmat(sum(sRGB2Cam,2),1,3); % normalize each rows of sRGB2Cam to 1
Cam2sRGB = (sRGB2Cam)^-1;
lin_srgb = apply_cmatrix(lin_rgb, Cam2sRGB);
lin_srgb = max(0,min(lin_srgb,1)); % Always keep image clipped b/w 0-1
figure;imshow(lin_srgb);title("颜色空间转换后图像");
%亮度校正与伽马校正
grayim = rgb2gray(lin_srgb); % Consider only gray channel
grayscale = 0.25/mean(grayim(:));
bright_srgb = min(1,lin_srgb * grayscale); % Always keep image value less than 1
nl_srgb = bright_srgb.^(1/2.2);
figure;imshow(nl_srgb);title("伽马图像");