仅供参考,主要为个人学习使用
一、排序
function [X,ndx,dbg] = natsort(X,xpr,varargin) %#ok<*SPERR>
% Alphanumeric / Natural-Order sort the strings in a cell array of strings.
%
% (c) 2016 Stephen Cobeldick
%
% Alphanumeric sort of a cell array of strings: sorts by character order
% and also by the values of any numbers that are within the strings. The
% default is case-insensitive ascending with integer number substrings:
% optional inputs control the sort direction, case sensitivity, and the
% number substring matching (see the section "Number Substrings" below).
%
%%% Syntax:
% Y = natsort(X)
% Y = natsort(X,xpr)
% Y = natsort(X,xpr,<options>)
% [Y,ndx] = natsort(X,...);
%
% To sort filenames or filepaths use NATSORTFILES (File Exchange 47434).
% To sort the rows of a cell array of strings use NATSORTROWS (File Exchange 47433).
%
% See also NATSORTFILES NATSORTROWS SORTROWS SORT CELLSTR REGEXP SSCANF NUM2ORDINAL NUM2WORDS NUM2BIP NUM2SIP INTMAX
%
%% Number Substrings %%
%
% By default consecutive digit characters are interpreted as an integer.
% The optional regular expression pattern <xpr> permits the numbers to also
% include a +/- sign, a decimal point, exponent E-notation or any literal
% characters, quantifiers or look-around requirements. For more information:
% http://www.mathworks.com/help/matlab/matlab_prog/regular-expressions.html
%
% The substrings are then parsed by SSCANF into numeric variables, using
% either the *default format '%f', or the user-supplied format specifier.
% The numeric variables' class type is determined by the format specifier.
%
% This table shows some example regular expression patterns for some common
% notations and ways of writing numbers (see section "Examples" for more):
%
% <xpr> Regular | Number Substring | Number Substring | SSCANF
% Expression: | Match Examples: | Match Description: | Format Specifier:
% ==============|==================|===============================|==================
% * \d+ | 0, 1, 234, 56789 | unsigned integer | %f %u %lu %i
% --------------|------------------|-------------------------------|------------------
% (-|+)?\d+ | -1, 23, +45, 678 | integer with optional +/- sign| %f %d %ld %i
% --------------|------------------|-------------------------------|------------------
% \d+(\.\d+)? | 012, 3.45, 678.9 | integer or decimal | %f
% --------------|------------------|-------------------------------|------------------
% \d+|Inf|NaN | 123456, Inf, NaN | integer, infinite or NaN value| %f
% --------------|------------------|-------------------------------|------------------
% \d+\.\d+e\d+ | 0.123e4, 5.67e08 | exponential notation | %f
% --------------|------------------|-------------------------------|------------------
% 0[0-7]+ | 012, 03456, 0700 | octal prefix & notation | %o %i
% --------------|------------------|-------------------------------|------------------
% 0X[0-9A-F]+ | 0X0, 0XFF, 0X7C4 | hexadecimal prefix & notation | %x %i
% --------------|------------------|-------------------------------|------------------
% 0B[01]+ | 0B101, 0B0010111 | binary prefix & notation | %b
% --------------|------------------|-------------------------------|------------------
%
% The SSCANF format specifier (including %b) can include literal characters
% and skipped fields. The octal, hexadecimal and binary prefixes are optional.
% For more information: http://www.mathworks.com/help/matlab/ref/sscanf.html
%
%% Relative Sort Order %%
%
% The sort order of the number substrings relative to the characters
% can be controlled by providing one of the following string options:
%
% Option Token:| Relative Sort Order: | Example:
% =============|======================================|====================
% 'beforechar' | numbers < char(0:end) | '1' < '.' < 'A'
% -------------|--------------------------------------|--------------------
% 'afterchar' | char(0:end) < numbers | '.' < 'A' < '1'
% -------------|--------------------------------------|--------------------
% 'asdigit' *| char(0:47) < numbers < char(48:end) | '.' < '1' < 'A'
% -------------|--------------------------------------|--------------------
%
% Note that the digit characters have character values 48 to 57, inclusive.
%
%% Examples %%
%
% % Integer number substrings:
% A = {'a2', 'a10', 'a1'};
% sort(A)
% ans = 'a1' 'a10' 'a2'
% natsort(A)
% ans = 'a1' 'a2' 'a10'
%
% % Multiple number substrings (e.g. release version numbers):
% B = {'v10.6', 'v9.10', 'v9.5', 'v10.10', 'v9.10.20', 'v9.10.8'};
% sort(B)
% ans = 'v10.10' 'v10.6' 'v9.10' 'v9.10.20' 'v9.10.8' 'v9.5'
% natsort(B)
% ans = 'v9.5' 'v9.10' 'v9.10.8' 'v9.10.20' 'v10.6' 'v10.10'
%
% % Integer, decimal or Inf number substrings, possibly with +/- signs:
% C = {'test+Inf', 'test11.5', 'test-1.4', 'test', 'test-Inf', 'test+0.3'};
% sort(C)
% ans = 'test' 'test+0.3' 'test+Inf' 'test-1.4' 'test-Inf' 'test11.5'
% natsort(C, '(-|+)?(Inf|\d+(\.\d+)?)')
% ans = 'test' 'test-Inf' 'test-1.4' 'test+0.3' 'test11.5' 'test+Inf'
%
% % Integer or decimal number substrings, possibly with an exponent:
% D = {'0.56e007', '', '4.3E-2', '10000', '9.8'};
% sort(D)
% ans = '' '0.56e007' '10000' '4.3E-2' '9.8'
% natsort(D, '\d+(\.\d+)?(E(+|-)?\d+)?')
% ans = '' '4.3E-2' '9.8' '10000' '0.56e007'
%
% % Hexadecimal number substrings (possibly with '0X' prefix):
% E = {'a0X7C4z', 'a0X5z', 'a0X18z', 'aFz'};
% sort(E)
% ans = 'a0X18z' 'a0X5z' 'a0X7C4z' 'aFz'
% natsort(E, '(?<=a)(0X)?[0-9A-F]+', '%x')
% ans = 'a0X5z' 'aFz' 'a0X18z' 'a0X7C4z'
%
% % Binary number substrings (possibly with '0B' prefix):
% F = {'a11111000100z', 'a0B101z', 'a0B000000000011000z', 'a1111z'};
% sort(F)
% ans = 'a0B000000000011000z' 'a0B101z' 'a11111000100z' 'a1111z'
% natsort(F, '(0B)?[01]+', '%b')
% ans = 'a0B101z' 'a1111z' 'a0B000000000011000z' 'a11111000100z'
%
% % uint64 number substrings (with full precision!):
% natsort({'a18446744073709551615z', 'a18446744073709551614z'}, [], '%lu')
% ans = 'a18446744073709551614z' 'a18446744073709551615z'
%
% % Case sensitivity:
% G = {'a2', 'A20', 'A1', 'a10', 'A2', 'a1'};
% natsort(G, [], 'ignorecase') % default
% ans = 'A1' 'a1' 'a2' 'A2' 'a10' 'A20'
% natsort(G, [], 'matchcase')
% ans = 'A1' 'A2' 'A20' 'a1' 'a2' 'a10'
%
% % Sort direction:
% H = {'2', 'a', '3', 'B', '1'};
% natsort(H, [], 'ascend') % default
% ans = '1' '2' '3' 'a' 'B'
% natsort(H, [], 'descend')
% ans = 'B' 'a' '3' '2' '1'
%
% % Relative sort-order of number substrings compared to characters:
% X = num2cell(char(32+randperm(63)));
% cell2mat(natsort(X, [], 'asdigit')) % default
% ans = '!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_'
% cell2mat(natsort(X, [], 'beforechar'))
% ans = '0123456789!"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_'
% cell2mat(natsort(X, [], 'afterchar'))
% ans = '!"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_0123456789'
%
%% Input and Output Arguments %%
%
%%% Inputs (*=default):
% X = Cell of Strings, with strings to be sorted into natural-order.
% xpr = String Token, regular expression to detect number substrings, *'\d+'.
% <options> string tokens can be entered in any order, as many as required:
% - Case sensitive/insensitive matching: 'matchcase'/'ignorecase'*.
% - Sort direction: 'descend'/'ascend'*.
% - Relative sort of numbers: 'beforechar'/'afterchar'/'asdigit'*.
% - The SSCANF number conversion format, e.g.: *'%f', '%x', '%i', etc.
%
%%% Outputs:
% Y = Cell of Strings, <X> with all strings sorted into natural-order.
% ndx = Numeric Array, such that Y = X(ndx). The same size as <X>.
% dbg = Cell Array of all parsed characters and number values. Each row is
% one string, linear-indexed from <X>. Helps to debug string parsing.
%
% [X,ndx,dbg] = natsort(X,*xpr,<options>)
%% Input Wrangling %%
%
assert(iscell(X),'First input <X> must be a cell array.')
tmp = cellfun('isclass',X,'char') & ...
2>cellfun('size',X,1) & ...
3>cellfun('ndims',X);
assert(all(tmp(:)),'First input <X> must be a cell array of strings (1xN character).')
%
% Regular expression:
if nargin<2 || isnumeric(xpr)&&isempty(xpr)
xpr = '\d+';
else
assert(ischar(xpr)&&isrow(xpr),'Second input <xpr> must be a regular expression.')
end
%
% Optional arguments:
tmp = cellfun('isclass',varargin,'char')&1==cellfun('size',varargin,1)&2==cellfun('ndims',varargin);
assert(all(tmp(:)),'All optional arguments must be strings (1xN character).')
% Character case matching:
MatL = strcmpi(varargin,'matchcase');
CasL = strcmpi(varargin,'ignorecase')|MatL;
% Sort direction:
DesL = strcmpi(varargin,'descend');
DirL = strcmpi(varargin,'ascend')|DesL;
% Relative sort-order of numbers compared to characters:
BefL = strcmpi(varargin,'beforechar');
AftL = strcmpi(varargin,'afterchar');
RsoL = strcmpi(varargin,'asdigit')|BefL|AftL;
% SSCANF conversion format:
FmtL = ~(CasL|DirL|RsoL);
%
if nnz(DirL)>1
error('Sort direction is overspecified:%s\b.',sprintf(' ''%s'',',varargin{DirL}))
end
%
if nnz(RsoL)>1
error('Relative sort-order is overspecified:%s\b.',sprintf(' ''%s'',',varargin{RsoL}))
end
%
FmtN = nnz(FmtL);
if FmtN>1
error('Overspecified optional arguments:%s\b.',sprintf(' ''%s'',',varargin{FmtL}))
end
%
%% Split Strings %%
%
% Split strings into number and remaining substrings:
[MtS,MtE,MtC,SpC] = regexpi(X(:),xpr,'start','end','match','split',varargin{CasL});
%
% Determine lengths:
MtcD = cellfun(@minus,MtE,MtS,'UniformOutput',false);
LenZ = cellfun('length',X(:))-cellfun(@sum,MtcD);
LenY = max(LenZ);
LenX = numel(MtC);
%
dbg = cell(LenX,LenY);
NuI = false(LenX,LenY);
ChI = false(LenX,LenY);
ChA = char(double(ChI));
%
ndx = 1:LenX;
for k = ndx(LenZ>0)
% Determine indices of numbers and characters:
ChI(k,1:LenZ(k)) = true;
if ~isempty(MtS{k})
tmp = MtE{k} - cumsum(MtcD{k});
dbg(k,tmp) = MtC{k};
NuI(k,tmp) = true;
ChI(k,tmp) = false;
end
% Transfer characters into char array:
if any(ChI(k,:))
tmp = SpC{k};
ChA(k,ChI(k,:)) = [tmp{:}];
end
end
%
%% Convert Number Substrings %%
%
if FmtN % One format specifier
fmt = varargin{FmtL};
err = ['Format specifier results in an empty output from sscanf: ''',fmt,''''];
P = '(?<!%)(%%)*%'; % match an odd number of % characters.
[T,S] = regexp(fmt,[P,'(\d*)(b|d|i|u|o|x|f|e|g|l(d|i|u|o|x))'],'tokens','split');
assert(isscalar(T),'Unsupported optional argument: ''%s''',fmt)
assert(isempty(T{1}{2}),'Format specifier cannot include field-width: ''%s''',fmt)
switch T{1}{3}(1)
case 'b' % binary
fmt = regexprep(fmt,[P,'(\*?)b'],'$1%$2[01]');
val = dbg(NuI);
if numel(S{1})<2 || ~strcmpi('0B',S{1}(end-1:end))
% Remove '0B' if not specified in the format string:
val = regexprep(val,'(0B)?([01]+)','$2','ignorecase');
end
val = cellfun(@(s)sscanf(s,fmt),val, 'UniformOutput',false);
assert(~any(cellfun('isempty',val)),err)
NuA(NuI) = cellfun(@(s)sum(pow2(s-48,numel(s)-1:-1:0)),val);
case 'l' % 64-bit
NuA(NuI) = cellfun(@(s)sscanf(s,fmt),dbg(NuI)); %slow!
otherwise % double
NuA(NuI) = sscanf(sprintf('%s\v',dbg{NuI}),[fmt,'\v']); % fast!
end
else % No format specifier -> double
NuA(NuI) = sscanf(sprintf('%s\v',dbg{NuI}),'%f\v');
end
% Note: NuA's class is determined by SSCANF.
NuA(~NuI) = 0;
NuA = reshape(NuA,LenX,LenY);
%
%% Debugging Array %%
%
if nargout>2
for k = reshape(find(NuI),1,[])
dbg{k} = NuA(k);
end
for k = reshape(find(ChI),1,[])
dbg{k} = ChA(k);
end
end
%
%% Sort %%
%
if ~any(MatL) % ignorecase
ChA = upper(ChA);
end
%
ide = ndx.';
% From the last column to the first...
for n = LenY:-1:1
% ...sort the characters and number values:
[C,idc] = sort(ChA(ndx,n),1,varargin{DirL});
[~,idn] = sort(NuA(ndx,n),1,varargin{DirL});
% ...keep only relevant indices:
jdc = ChI(ndx(idc),n); % character
jdn = NuI(ndx(idn),n); % number
jde = ~ChI(ndx,n)&~NuI(ndx,n); % empty
% ...define the sort-order of numbers and characters:
jdo = any(AftL)|(~any(BefL)&C<48);
% ...then combine these indices in the requested direction:
if any(DesL) % descending
idx = [idc(jdc&~jdo);idn(jdn);idc(jdc&jdo);ide(jde)];
else % ascending
idx = [ide(jde);idc(jdc&jdo);idn(jdn);idc(jdc&~jdo)];
end
ndx = ndx(idx);
end
%
ndx = reshape(ndx,size(X));
X = X(ndx);
%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%natsort
二、计算PSNR
clear;close all;
folder_A = 'data_inference/BSD100_result';
folder_B = 'GTdata/BSD100';
filepaths_A = dir(fullfile(folder_A,'*.png'));
dircell_A = struct2cell(filepaths_A)';
filenames_A = natsort(dircell_A(:,1));
filepaths_B = dir(fullfile(folder_B,'*.jpg'));
dircell_B = struct2cell(filepaths_B)';
filenames_B = natsort(dircell_B(:,1));
% initialization
count = 0;
%width = 2040;
%height = 1404;
frame_count = size(dircell_A,1);
for j = 1 : frame_count
str_GT = fullfile(folder_A, char(filenames_A(j)));
str_HR = fullfile(folder_B, char(filenames_B(j)));
img_GT = imread(str_GT);
img_GT = rgb2ycbcr(img_GT);
img_HR = imread(str_HR);
img_HR = rgb2ycbcr(img_HR);
img_GT = img_GT(:,:,1);
img_HR = img_HR(:,:,1);
data1(j,1) = psnr(img_HR,img_GT);
data1(j,2) = ssim(img_HR,img_GT);
% figure;
% subplot(1,3,1);
%
% imshow(img_HR);
% title('HR');
%
% subplot(1,3,2);
%
% imshow(img_SR);
% title('SR');
%
% subplot(1,3,3);
%
% imshow(img_LR,[0,80]);
% title('bicubic');
%
%
end
mean(data1)
三、视频转图片集
clear all
clc
%% 读取视频
aviinfo('监控.avi')
video_file='监控.avi';
video=VideoReader(video_file);
frame_number=floor(video.Duration * video.FrameRate);
video.FrameRate
frame_number
%% 分离图片
for i=1:149
if(i<10)
image_name=strcat('000',num2str(i));
end
if(100>i && i>9)
image_name=strcat('00',num2str(i));
end
if(1000>i && i>99)
image_name=strcat('0',num2str(i));
end
if(i>999)
image_name=strcat(num2str(i));
end
image_name=strcat(image_name,'.png');
I=read(video,i); %读出图片
[H,W,C] = size(I);
H = H-mod(H,16);
W = W-mod(W,16);
I = imcrop(I,[1,1,W-1,H-1]);
% I1 = imresize(I,0.25,'bicubic');
mkdir 监控
imwrite(I,strcat('监控/',image_name),'png'); %写图片
% imwrite(I1,strcat('video/BIx4/',image_name),'png'); %写图片
I=[];
i
end
四、图片集转视频
clc;
clear all;
close all;
path1 = 'D:\EDSR\dataset\video\city_new\';%降采样
path2 = 'D:\EDSR\dataset\video\city_result\';%超分
path3 = 'D:\EDSR\dataset\video\city\';%GT
pic1=dir(path1);% 图像序列路径
pic2=dir(path2);
pic3=dir(path3);
WriterObj=VideoWriter('video\city1.avi', 'Motion JPEG AVI');% xxx.avi表示待合成的视频
open(WriterObj);
for i=3:length(pic1)
frame_lr=imread(strcat(path1,pic1(i).name));% 读取图像,放在变量frame中
frame_sr=imread(strcat(path2,pic2(i).name));
frame_hr=imread(strcat(path3,pic3(i).name));
frame_lr = imresize(frame_lr,2,'bicubic');
frame= [frame_lr,frame_sr,frame_hr];
%imwrite(frame,'test.png');
writeVideo(WriterObj,frame);% 将frame放到变量WriterObj中
end
close(WriterObj);