提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
项目时间:2023/10
复盘时间:2023/10/10
图像处理基础(二)暗通道先验去雾
基于Matlab暗通道图像去雾处理
图像去雾算法
基于MATLAB的去雾系统(深度学习/传统/改进方法)
将音频写入视频
同时感谢Simon同学在拉通代码时给予的帮助
需求分析
Enhance all the images under folder IEI2019 and enhance at least one video clip under the folder IEV2022, increase the contrast, reveal more details hidden in the fog (haze). Do it either in spatial domain or in frequency domain. Different dehazing techniques such as Dark Channel Priori or those based on Retinex or Homomorphic Filtering might be used.
Please keep an eye on the run-time efficiency of your algorithm.
采取暗通道先验去雾算法,对文件夹下’Fall.mp4’进行去雾
设计思路
与处理图像相比,视频处理同时要考虑bgm的处理,分为以下步骤:
原视频V拆bgm-拆帧I-去雾得帧J-合帧得去雾后视频-写入bgm
代码
main
%% source vedio files
video_file = 'D:\数字图像处理DIP\DIP_lab1\Project 1\final\fall.mp4';%源文件绝对路径
video=VideoReader(video_file);
frame_number = video.NumFrames;% normal frames
original_path = 'D:\数字图像处理DIP\DIP_lab1\Project 1\final\original_frame\';% splited image files path
aug_path ='D:\数字图像处理DIP\DIP_lab1\Project 1\final\augumented_frame\';% modified and augumented images files path
%% split bgm
%Fs=48000;
[y,Fs]=audioread('D:\数字图像处理DIP\DIP_lab1\Project 1\final\fall.mp4');%源文件绝对路径
[pathstr, name, ext] = fileparts('D:\数字图像处理DIP\DIP_lab1\Project 1\final\');%工作路径
audio_path = fullfile(pathstr, 'audio.wav');
audiowrite(audio_path,y,Fs);
%% split video into several frames(ctrl+R there if finished in file)
for i=1:1:frame_number%拆帧位置
% create the path parameter store the image address
image_name=strcat('fall',num2str(i));
path = [original_path,image_name,'.jpg'];
image=read(video,i); %read image
imwrite(image,path) %write image
image=[]; %initialize parameter image
end
hold on
%% select w0 value (or set a W0 directly)
% w0=0.63;
w0 = select_w0();
%% agumented the images using haze remove algorithm (wait 2s for 1 frame)
for i = 1:frame_number %拆帧位置
image_name = strcat('fall',num2str(i));
path_original =[original_path,image_name,'.jpg'];
image_agu = haze_remove(path_original,w0);
path_augumented = [aug_path,image_name,'.jpg'];
imwrite(image_agu,path_augumented) %write image
end
%% emerge the images and create the video
writerObj = VideoWriter('fall_aug.avi');
open(writerObj);
for i = 1:frame_number %拆帧位置
image_name = strcat('fall',num2str(i));
path_augumented = [aug_path,image_name,'.jpg'];
frame = imread(path_augumented);
writeVideo(writerObj,frame);
end
close(writerObj);
%% combine the video and the bgm together
video_path='fall_aug.avi';
movObj=VideoReader(video_path); % 获取视频信息
[AUDIO,Fs]=audioread(audio_path);% 获取音频信息
audioFrameLen=round(size(AUDIO,1)./(movObj.NumFrames));% 获取每一帧对应音频长度 (时间/总帧数)
% 调用Computer Vision Toolbox内函数创建视频对象
videoFWriter=vision.VideoFileWriter('result.avi','FrameRate',movObj.FrameRate);%输出规格
videoFWriter.FrameRate = 30; % 设置输出文件的帧率
videoFWriter.AudioInputPort=true; % 把允许声音写入设置为true
% 为了显示进度创建一个窗口,不要也罢
close all;
fig=figure(1);
fig.NumberTitle='off';
aug_path ='D:\数字图像处理DIP\DIP_lab1\Project 1\final\augumented_frame\';% modified and augumented images files path
for k=1:movObj.NumFrames%拆帧位置
image_name=strcat('fall',num2str(k));%映射要对应
path = [aug_path,image_name,'.jpg'];
frame= imread(path);
% 视频音频一帧一帧写入
videoFWriter(frame,AUDIO((k-1)*audioFrameLen+1:k*audioFrameLen,:));
% 显示进度
imshow(frame)
fig.Name=['Frame:[',num2str(k),'/',num2str(movObj.NumFrames),']'];
end
release(videoFWriter);%关掉视频对象
function [J] =haze_remove(path,w0)
此处5*5的图还是显得模糊了,但如果继续细分会缺失对整体的把控
function [J] =haze_remove(path,w0)
%w0=0.95; %0.65 乘积因子用来保留一些雾,1时完全去雾,但呈现效果会比较差,如处理后白天变晚上
t0=0.3; %防止/0
image=imread(path);
[h,w,s]=size(image);
min_I=zeros(h,w);
%calculate the dark_channel
for i=1:h
for j=1:w
dark_channel(i,j)=min(image(i,j,:));
end
end
Max_dark_channel=double(max(max(dark_channel))); %天空亮度
dark_channel=double(dark_channel);
t=1-w0*(dark_channel/Max_dark_channel); %取得透射分布率图
% calculate the result images
t=max(t,t0);
I1=double(image);
J(:,:,1) = uint8((I1(:,:,1) - (1-t)*Max_dark_channel)./t);
J(:,:,2) = uint8((I1(:,:,2) - (1-t)*Max_dark_channel)./t);
J(:,:,3) =uint8((I1(:,:,3) - (1-t)*Max_dark_channel)./t);
end
function [w0] = select_w0()
function [w0] = select_w0()
image_path = 'D:\数字图像处理DIP\DIP_lab1\Project 1\final\original_frame/fall3.jpg';
figure(1);
for w0 = 0.25:0.01:0.49
aug_image = haze_remove(image_path,w0);
subplot(5,5,w0*100-24);
imshow(aug_image);
title(sprintf('w0 value:%f',w0));
end
figure(2);
for w0 = 0.50:0.01:0.74
aug_image = haze_remove(image_path,w0);
subplot(5,5,w0*100-49);
imshow(aug_image);
title(sprintf('w0 value:%f',w0));
end
figure(3);
for w0 = 0.75:0.01:0.99
aug_image = haze_remove(image_path,w0);
subplot(5,5,w0*100-74);
imshow(aug_image);
title(sprintf('w0 value:%f',w0));
end
prompt = 'What is the w0 value? w0=';
w0 = input(prompt);
end
补充批量处理并保存图片
采用9*9
clear all
folder = 'D:\数字图像处理DIP\DIP_lab1\Project 1\P1\db\IEI2019';
outputfolder='D:\数字图像处理DIP\DIP_lab1\Project 1\P1\db\IEI2019\dehaze'
images_jpg = dir(fullfile(folder, '*.jpg')); % 获取文件夹中的所有image文件
images_png = dir(fullfile(folder, '*.png'));
images=[images_jpg;images_png]
w0=0.63 %未经过选择
t0=0.001
for i = 1:length(images)
filename = images(i).name;
filepath = fullfile(folder, filename);
I = imread(filepath);
I = imresize(I,[600,800]);
% I = rgb2gray(I);
figure;
subplot(121)
imshow(I); title('原图');
[h,w,s]=size(I);
min_I=zeros(h,w);
for i=1:h %获取三通道最小值
for j=1:w
min_I(i,j)=min(I(i,j,:));
end
end
%最小值滤波
dark_I = ordfilt2(min_I,1,ones(9,9),'symmetric'); %窗口大小9*9
Max_dark_channel=double(max(max(dark_I))); %A
dark_channel=double(dark_I);
t=1-w0*(dark_channel/Max_dark_channel); %t
t1=max(t,t0); %清除透射率图中可能出现的零值
I1=double(I); %计算去雾图像
J(:,:,1) = uint8(Max_dark_channel + (I1(:,:,1)-Max_dark_channel)./t1);
J(:,:,2) = uint8(Max_dark_channel + (I1(:,:,2)-Max_dark_channel)./t1);
J(:,:,3) = uint8(Max_dark_channel + (I1(:,:,3)-Max_dark_channel)./t1);
subplot(122)
imshow(J);
title('暗通道先验去雾图像');
output_file = fullfile(outputfolder,filename);
imwrite(J,output_file); %write image
% J=[] %no need to initialize parameter image,this is not videoframe
end
复盘
1)保存绝对路径,or保存路径的char为空,写入的文件会出现在上两级母文件夹中
2)抽帧和拆帧的两种方法:BV1oQ4y1d7J8
3)处理后合并的视频未经过合理的压缩编码
4)调参的自适应w0算法未实现,前端未实现,小波分析预处理帧未实现