Commit 9d5ec3a4 authored by Dany Dumont's avatar Dany Dumont

Ajout de fonctions pour convertir les DNG en jpg

parent 0f098f65
cd '/Volumes/DATA/Dropbox/SILLEX_2019/Drones/Fjord/HYPERLAPSE/100_0236/jpg'
g_stabilize('HYPERLAPSE_0100.jpg','roi_file','HYPERLAPSE_0100_roi.mat','L_level_Gaussian',6);
cd '/Volumes/DATA/Dropbox/SILLEX_2019/Drones/Fjord/HYPERLAPSE/100_0240/jpg'
g_stabilize('HYPERLAPSE_0010.jpg','roi_file','HYPERLAPSE_0010_roi.mat','L_level_Gaussian',6);
cd '/Volumes/DATA/Dropbox/SILLEX_2019/Drones/Fjord/HYPERLAPSE/100_0242/jpg'
g_stabilize('HYPERLAPSE_0100.jpg','roi_file','HYPERLAPSE_0100_roi.mat','L_level_Gaussian',6);
function corrected = apply_cmatrix(im,cmatrix)
% Applies CMATRIX to RGB input IM. Finds the appropriate weighting of the
% old color planes to form the new color planes, equivalent to but much
% more efficient than applying a matrix transformation to each pixel.
if size(im,3) ~=3
error('Apply cmatrix to RGB image only.');
end
r = cmatrix(1,1) * im(:,:,1)+cmatrix(1,2) * im(:,:,2)+cmatrix(1,3) * im(:,:,3);
g = cmatrix(2,1) * im(:,:,1)+cmatrix(2,2) * im(:,:,2)+cmatrix(2,3) * im(:,:,3);
b = cmatrix(3,1) * im(:,:,1)+cmatrix(3,2) * im(:,:,2)+cmatrix(3,3) * im(:,:,3);
corrected = cat(3,r,g,b);
\ No newline at end of file
clear
%g_stabilize('HYPERLAPSE_0001.jpg','roi_file','roi_100_0243_0001.mat','L_level_Gaussian',6);
%cd ..
%folders = dir;
%ifolders = find(folders.isdir == 1);
%folders = folders(ifolders);
%N_folders = length(folders);
%for i = 3:N_folders
%for i = 11:N_folders
% cd(folders(i).name);
DNG_filenames = dir('*.DNG');
N_files = length(DNG_filenames);
for j = 1:N_files
newFileName = strcat(DNG_filenames(j).name(1,1:end-3),'jpg');
display([' Converting ', DNG_filenames(j).name, ' to ',newFileName,' (',...
num2str(j),'/',num2str(N_files),')']);
% Call the function that makes the conversion
g_DNG2jpg(DNG_filenames(j).name)
end
%if strcmp(folders(i).name,'100_0243');
% g_stabilize('HYPERLAPSE_0001.jpg','roi_file','roi_100_0243_0001.mat','L_level_Gaussian',6);
%end
%
%cd ..
%end
function dummy = DNG2jpg(DNG_filename)
% Based off code found in Processing RAW Images in MATLAB by Rob Sumner
% (May 19, 2014) which can be found here:
% https://rcsumner.net/raw_guide/RAWguide.pdf
% Used to convert DNG images to JPGs without additional post-processing
% Original code by Rob Sumner debugged and augmented by Daniel Bourgault
% ISMER, 2019.
%clear all;
%% Turn off annoying warnings
warning off MATLAB:tifflib:TIFFReadDirectory:libraryWarning
warning off MATLAB:imagesci:tifftagsread:badTagValueDivisionByZero
warning off MATLAB:imagesci:tiffmexutils:libtiffWarning
%% DNG Image Loading
%[fileName,pathName] = uigetfile('*.DNG','Select the DNG image');
%cd(pathName);
t = Tiff(DNG_filename,'r');
offsets = getTag(t,'SubIFD');
setSubDirectory(t,offsets(1));
raw = read(t);
close(t);
meta_info = imfinfo(DNG_filename);
x_origin = meta_info.SubIFDs{1}.ActiveArea(2) + 1;
width = meta_info.SubIFDs{1}.Width;
y_origin = meta_info.SubIFDs{1}.ActiveArea(1) + 1;
height = meta_info.SubIFDs{1}.Height;
raw = double(raw(y_origin:y_origin+height-1,x_origin:x_origin+width-1));
%% Linearizing
black = meta_info.SubIFDs{1}.BlackLevel(1);
saturation = meta_info.SubIFDs{1}.WhiteLevel;
lin_bayer = (raw - black)/(saturation - black);
lin_bayer = max(0,min(lin_bayer,1));
%% White balancing
wb_multipliers = (meta_info.AsShotNeutral).^(-1);
wb_multipliers = wb_multipliers/wb_multipliers(2);
mask = wbmask(size(lin_bayer,1),size(lin_bayer,2),wb_multipliers,'rggb');
balanced_bayer = lin_bayer.*mask;
%% De-mosaicing
%temp = uint16(lin_bayer/max(lin_bayer(:))*2^16);
temp = uint16(balanced_bayer/max(balanced_bayer(:))*2^16);
lin_rgb = double(demosaic(temp,'rggb'))/2^16;
%% Color Space Conversion
xyz2cam = [meta_info.ColorMatrix2(1),meta_info.ColorMatrix2(2),meta_info.ColorMatrix2(3);...
meta_info.ColorMatrix2(4),meta_info.ColorMatrix2(5),meta_info.ColorMatrix2(6);...
meta_info.ColorMatrix2(7),meta_info.ColorMatrix2(8),meta_info.ColorMatrix2(9)]; % color correction matrix
rgb2xyz = [0.4124564,0.3575761,0.1804375;...
0.2126729,0.7151522,0.0721750;...
0.0193339,0.1191920,0.9503041]; % sRGB to XYZ
rgb2cam = xyz2cam*rgb2xyz; % Assuming previously defined matrices
rgb2cam = rgb2cam./repmat(sum(rgb2cam,2),1,3); % Normalize rows to 1
cam2rgb = rgb2cam^-1;
lin_srgb = apply_cmatrix(lin_rgb, cam2rgb);
lin_srgb = max(0,min(lin_srgb,1)); % Always keep image clipped b/w 0-1
%% Brightness and gamma correction
grayim = rgb2gray(lin_srgb);
grayscale = 0.25/mean(grayim(:));
bright_srgb = min(1,lin_srgb*grayscale);
nl_srgb = bright_srgb.^(1/2.2);
%% File Saving
newFileName = strcat(DNG_filename(1,1:end-3),'jpg');
imwrite(nl_srgb,newFileName,'jpg','Quality',100)
% Source:
% https://github.com/rcsumner/devCam/blob/dbaa63c0c6001fc5ec9482b570f0fdaaf6c9a537/MATLAB/dngShow.m#L112-L134
function colormask = wbmask(m,n,wbmults,align)
% COLORMASK = wbmask(M,N,WBMULTS,ALIGN)
%
% Makes a white-balance multiplicative mask for an image of size m-by-n
% with RGB while balance multipliers WBMULTS = [R_scale G_scale B_scale].
% ALIGN is string indicating Bayer arrangement: 'rggb','gbrg','grbg','bggr'
colormask = wbmults(2)*ones(m,n); %Initialize to all green values
switch align
case 'rggb'
colormask(1:2:end,1:2:end) = wbmults(1); %r
colormask(2:2:end,2:2:end) = wbmults(3); %b
case 'bggr'
colormask(2:2:end,2:2:end) = wbmults(1); %r
colormask(1:2:end,1:2:end) = wbmults(3); %b
case 'grbg'
colormask(1:2:end,2:2:end) = wbmults(1); %r
colormask(1:2:end,2:2:end) = wbmults(3); %b
case 'gbrg'
colormask(2:2:end,1:2:end) = wbmults(1); %r
colormask(1:2:end,2:2:end) = wbmults(3); %b
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment