Skip to content

Instantly share code, notes, and snippets.

@doersino
Last active September 13, 2016 15:24
Show Gist options
  • Save doersino/5f691384713376b1b4c1ac8a257ab9be to your computer and use it in GitHub Desktop.
Save doersino/5f691384713376b1b4c1ac8a257ab9be to your computer and use it in GitHub Desktop.
% 1. download files from http://alumni.soe.ucsc.edu/%7Exzhu/doc/turbulence.html to current folder
%
% 2. run
% $ youtube-dl -o sx.mp4 https://www.youtube.com/watch?v=_BgJEXQkjNQ
% to download the video and
% $ ffmpeg -ss 00:00:50 -i sx.mp4 -filter:v fps=fps=60/1 frames/ffmpeg_%3d.png
% to extract the frames - quit this once it reaches frame 1310 or so
%
% 3. then run this script (might take a few days)
%
% 4 to generate video, run
% $ cd frames_allthree
% $ mogrify -resize 1920x1080 *.png
% $ ffmpeg -framerate 15 -i frame_%04d.png -c:v libx264 -vf "scale=trunc(iw/2+1)*2:trunc(ih/2+1)*2" -r 30 -pix_fmt yuv420p frames.mp4
% TODO possible improvement: look at color channels separately
if exist('frames_reg','var') == 0
% read images (if not already read)
if exist('imgs','var') == 0
imgPath = 'frames/';
imgType = '*.png';
images = dir([imgPath imgType]);
for idx = 1:length(images)
images(idx).name
img = imread([imgPath images(idx).name]);
img = img(240:561,460:650,:); % area of interest
img = rgb2gray(img);
imgs(:,:,idx) = img;
end
end
% setup variables
frames = imgs;
Ref = mean(imgs, 3);
imshow(Ref);
% run the turbulence removal, note that this takes ~30s per frame
% TODO improve settings (see Example.m)
Example
end
% write results
for idx = 1:size(frames_reg, 3) - 1
% area of interest from original
orig = frames(:,:,idx);
mkdir('frames_orig');
filename = sprintf('frames_orig/frame_%04d.png', idx);
imwrite(uint8(repmat(orig,[1 1 3])),filename,'png');
% result from turbulence removal
reg = frames_reg(E+1:end-E,E+1:end-E,idx);
mkdir('frames_reg');
filename = sprintf('frames_reg/frame_%04d.png', idx);
imwrite(uint8(repmat(reg,[1 1 3])),filename,'png');
% increase contrast
contr = (reg - 42) * 1.8;
%max(contr(:))
%min(contr(:))
% negative
contrNeg = 255 - contr;
% blind deconvolution
img = frames_reg(:,:,idx);
[deconv,PSF] = deconvblind(img, ones(size(img)));
%imshow(PSF*30000)
deconv = deconv(E+1:end-E,E+1:end-E);
% sharpening
sharp = imsharpen(reg,'Radius',3,'Amount',0.7);
if idx == size(frames_reg, 3)
origDiff = zeros(size(orig));
regDiff = zeros(size(orig));
contrDiff = zeros(size(orig));
contrNegDiff = zeros(size(orig));
deconvDiff = zeros(size(orig));
sharpDiff = zeros(size(orig));
else
orig2 = frames(:,:,idx+1);
reg2 = frames_reg(E+1:end-E,E+1:end-E,idx+1);
contr2 = (reg2 - 42) * 1.8;
contrNeg2 = 255 - contr2;
img = frames_reg(:,:,idx+1);
[deconv2,PSF] = deconvblind(img, ones(size(img)));
deconv2 = deconv2(E+1:end-E,E+1:end-E);
sharp2 = imsharpen(reg2,'Radius',3,'Amount',0.7);
origDiff = 127 + 25 * (double(orig) - double(orig2));
regDiff = 127 + 25 * (reg - reg2);
contrDiff = 127 + 25 * (contr - contr2);
contrNegDiff = 127 + 25 * (contrNeg - contrNeg2);
deconvDiff = 127 + 25 * (deconv - deconv2);
sharpDiff = 127 + 25 * (sharp - sharp2);
end
% final (for concat with ffmpeg)
orig = insertText(orig,[0 0],'1: raw video (0.25x speed)','BoxColor','white');
orig = orig(:,:,1);
reg = insertText(uint8(reg),[0 0],'2: after removing turbulences','BoxColor','white');
reg = reg(:,:,1);
contr = insertText(uint8(contr),[0 0],'3: <2> w/ increased contrast','BoxColor','white');
contr = contr(:,:,1);
contrNeg = insertText(uint8(contrNeg),[0 0],'4: negative of <3>','BoxColor','white');
contrNeg = contrNeg(:,:,1);
deconv = insertText(uint8(deconv),[0 0],'5: matlab deconvblind(<2>)','BoxColor','white');
deconv = deconv(:,:,1);
sharp = insertText(uint8(sharp),[0 0],'6: matlab imsharpen(<2>)','BoxColor','white');
sharp = sharp(:,:,1);
origDiff = insertText(uint8(origDiff),[0 0],'difference to next frame','BoxColor','white');
origDiff = origDiff(:,:,1);
dataraw = [orig reg contr contrNeg deconv sharp; origDiff regDiff contrDiff contrNegDiff deconvDiff sharpDiff];
mkdir('frames_allthree1');
filename = sprintf('frames_allthree1/frame_%04d.png', idx);
data = uint8(repmat(dataraw,[1 1 3]));
imwrite(data,filename,'png');
% print status
sprintf('wrote frame_%04d.png', idx)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment