Mentions légales du service

Skip to content
Snippets Groups Projects
Commit edc816de authored by DIB Elian's avatar DIB Elian
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
+yuv
function convFrames = convert(frames,varargin)
%CONVERT Summary of this function goes here
% Detailed explanation goes here
colSpaces = {'yuv','ycbcr','rgb'};
subSamps = {'420','444','422','400'};
% offsets = {'even','centered','odd'}; %WIP
p = inputParser;
p.addRequired('frames' ,@iscell);
p.addParameter('colSpace',colSpaces{1},@(x) any(validatestring(x,colSpaces)));
p.addParameter('subSamp' ,subSamps{1} ,@(x) any(validatestring(x,subSamps) ));
p.parse(frames,varargin{:});
frames = p.Results.frames;
colSpace = p.Results.colSpace;
subSamp = p.Results.subSamp;
convFrames = cellfun(@convertFrame,frames,'UniformOutput',false);
%% Auxiliary functions
function subFrames = convertFrame(frame)
frame = squeeze(frame);
frame = colorSpaceConvert(frame);
prevClass = class(frame);
frame = cast(frame,'double');
subFrames = subSample(frame);
subFrames = cellfun(@(f) cast(f,prevClass),subFrames,'UniformOutput',false);
end
function frame = colorSpaceConvert(frame)
switch colSpace
case {'yuv','ycbcr'}
frame = rgb2ycbcr(frame);
case 'rgb'
end
end
function frameCell = subSample(frame)
s = size(frame);
frameCell = mat2cell(frame,s(1),s(2),ones(1,s(3)));
switch s(3)
case 3
frame2Int = griddedInterpolant(frameCell{2},'linear');
frame3Int = griddedInterpolant(frameCell{3},'linear');
yq = 1:2:s(2);
switch subSamp
case '444'
case '422'
xq = 1:s(1);
case '420'
xq = 0.5+(1:2:s(1));
case '400'
end
if subSamp =='444'
elseif subSamp=='400'
frameCell = frameCell(1);
else
xq = xq(xq<=s(1)); yq = yq(yq<=s(2));
frameCell{2} = frame2Int({xq,yq});
frameCell{3} = frame3Int({xq,yq});
end
case 1
switch subSamp
case '400'
case {'444','422','420'}
warning('Monochrome image, switching to 400 chroma subsampling');
end
end
end
end
clear all, close all, clc
%%
subSamp ='444';
colSpace = 'yuv';
name = 'test';
precision = 'uint8';
%%
load('C:\Users\edib\Light Fields\Heidelberg\full_data\test\bedroom\LF.mat');
LF = LF.LF;
% LF = LF(1:5,3:4,34:307,88:400,:);
% LF = double(uint8(255*LF))/255;
% LF = zeros(size(LF),'double');
s = size(LF);
frames = mat2cell(LF,ones(1,s(1)),ones(1,s(2)),s(3),s(4),s(5));
%%
convFrames = yuv.convert(frames,...
'subSamp',subSamp,...
'colSpace',colSpace);
%%
[imgRes,imgSize,convFrames] = yuv.write(frames,name,...
'colSpace',colSpace,...
'subSamp',subSamp,...
'precision',precision);
%%
convFrames_ = yuv.read(name,imgRes,imgSize,...
'colSpace',colSpace,...
'subSamp',subSamp,...
'precision',precision);
%%
close all
i=1;
figure, imshow(convFrames{1,1}{i})
figure, imshow(convFrames_{1,1}{i})
figure, imshow(abs(convFrames{1,1}{i}-convFrames{1,1}{i}),[])
read.m 0 → 100644
function frames = read(name,imgRes,imgSize,varargin)
%READ Summary of this function goes here
% Detailed explanation goes here
colSpaces = {'yuv','ycbcr','rgb'};
subSamps = {'420','444','422','400'};
precisions = {'uint8','uint16','uint32','uint64'...
'int8','int16','int32','int64',...
'single','double'};
p = inputParser;
p.addRequired('name' ,@ischar);
p.addRequired('imgRes' ,@isnumeric);
p.addRequired('imgSize' ,@isnumeric);
p.addParameter('colSpace' ,colSpaces{1} ,@(x) any(validatestring(x,colSpaces)));
p.addParameter('subSamp' ,subSamps{1} ,@(x) any(validatestring(x,subSamps) ));
p.addParameter('precision',precisions{1},@(x) any(validatestring(x,precisions)));
p.addParameter('subName' ,'' ,@ischar);
p.parse(name,imgRes,imgSize,varargin{:});
name = p.Results.name;
imgRes = p.Results.imgRes;
imgSize = p.Results.imgSize;
colSpace = p.Results.colSpace;
subSamp = p.Results.subSamp;
precision = p.Results.precision;
subName = p.Results.subName;
filename = compName(name);
switch precision
case {'single','double'}
range = [0,1];
case {'int8','uint8','int16','uint16','int32','uint32','int64','uint64'}
range = double([intmin(precision),intmax(precision)]);
end
imgSizes{1} = imgSize;
switch subSamp
case '444'
case '422'
imgSize(2) = floor((imgSize(1)+1)/2);
case '420'
imgSize(1) = floor((imgSize(1))/2);
imgSize(2) = floor((imgSize(2)+1)/2);
case '400'
end
if ~strcmp(subSamp,'400')
[imgSizes{2},imgSizes{3}] = deal(imgSize);
end
imgGrid = zeros(imgRes);
fileID = fopen(filename,'r');
frames = arrayfun(@readFrame,imgGrid,'UniformOutput',false);
fclose(fileID);
function frameCell = readFrame(~)
frameCell = cell(1,numel(imgSizes));
for i = 1:numel(frameCell)
frameCell_ = fread(fileID,flip(imgSizes{i}),precision);
frameCell{i} = (frameCell_'-range(1))/diff(range);
end
%frameCell = cellfun(...
% @(s) (fread(fileID,flip(s),precision)'-range(1))/diff(range),sizes,...
% 'UniformOutput',false);
end
function filename = compName(name)
resCell = num2cell(imgRes);
sizeCell = num2cell(imgSize);
fullName = [resCell,sizeCell];
fullName = cellfun(@num2str,fullName,'UniformOutput',false);
fullName = strjoin([name fullName subSamp],'_');
filename = [fullName,subName,'.',colSpace];
end
end
\ No newline at end of file
write.m 0 → 100644
function [imgRes,imgSize,subFrames] = write(frames,name,varargin)
%WRITE Summary of this function goes here
% Detailed explanation goes here
colSpaces = {'yuv','ycbcr','rgb'};
subSamps = {'420','444','422','400'};
precisions = {'uint8','uint16','uint32','uint64'...
'int8','int16','int32','int64',...
'single','double'};
p = inputParser;
p.addRequired ('frames' ,@iscell);
p.addRequired ('name' ,@ischar);
p.addParameter('colSpace' ,colSpaces{1} ,@(x) any(validatestring(x,colSpaces)));
p.addParameter('subSamp' ,subSamps{1} ,@(x) any(validatestring(x,subSamps) ));
p.addParameter('precision',precisions{1} ,@(x) any(validatestring(x,precisions)));
p.addParameter('subName' ,'' ,@ischar);
p.parse(frames,name,varargin{:});
frames = p.Results.frames;
name = p.Results.name;
colSpace = p.Results.colSpace;
subSamp = p.Results.subSamp;
precision = p.Results.precision;
subName = p.Results.subName;
subFrames = yuv.convert(frames,'colSpace',colSpace,'subSamp',subSamp);
imgRes = size(subFrames);
imgSize = size(subFrames{1,1}{1});
filename = compName(name);
switch precision
case {'single',' double'}
range = [0,1];
case {'int8','uint8','int16','uint16','int32','uint32','int64','uint64'}
range = double([intmin(precision),intmax(precision)]);
otherwise
error('Invalid precision');
end
fileID = fopen(filename,'w+');
cellfun(@writeFrame,subFrames,'UniformOutput',false);
fclose(fileID);
%%
function writeFrame(frameCell)
for i = 1:numel(frameCell)
fwrite(fileID,diff(range)*frameCell{i}'+range(1),precision);
end
%cellfun(@(chan) fwrite(fileID,diff(range)*chan'+range(1),precision),frameCell,...
% 'UniformOutput',false);
end
function filename = compName(name)
resCell = num2cell(imgRes);
sizeCell = num2cell(imgSize);
fullName = [resCell,sizeCell];
fullName = cellfun(@num2str,fullName,'UniformOutput',false);
fullName = strjoin([name fullName subSamp],'_');
filename = [fullName,subName,'.',colSpace];
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment