I am trying to perform the No-Reference Image Quality Assessment (NR-IQA) calculation on video. Several NR-IQA methods including Naturalness Image Quality Evaluator (NIQE), Perception based Image Quality Evaluator (PIQE) and Blind/Referenceless Image Spatial Quality Evaluator (BRISQUE) are used and the calculation results are both show on video frame and export to an Excel file. The file BigBuckBunny
is used as the input experimental video.
The experimental implementation
%% Read and process a video into MATLAB
% Reference: https://www.mathworks.com/solutions/image-video-processing/video-processing.html
tic
% Setup: create Video Reader and Writer
videoFilename = ['BigBuckBunny_320x180'];
videoPath = ['.'];
videoFullFilename = fullfile(videoPath, [videoFilename '.mp4']);
videoFileReader = VideoReader(videoFullFilename);
% Reference: https://www.mathworks.com/matlabcentral/answers/123189-get-the-number-of-frame-of-a-video-using-vision-videofilereader-and-videoreader
TotalFrameCount = floor(videoFileReader.Duration * videoFileReader.FrameRate);
% Setup: create output folder
OutputFolderRoot = 'NIQE_PIQE_BRISQUE';
if ~exist(OutputFolderRoot, 'dir')
mkdir(OutputFolderRoot);
end
outputFilename = fullfile(OutputFolderRoot, [videoFilename '.avi']);
outputVideo = VideoWriter(outputFilename);
% Setup: create data record variables
% Reference: https://www.mathworks.com/help/matlab/ref/round.html
NIQE_results = zeros(1, round(TotalFrameCount, 0));
PIQE_results = zeros(1, round(TotalFrameCount, 0));
BRISQUE_results = zeros(1, round(TotalFrameCount, 0));
FrameIndex = 1;
% Setup: create deployable video player
depVideoPlayer = vision.DeployableVideoPlayer;
open(outputVideo);
%% Detect faces in each frame
while hasFrame(videoFileReader)
% read video frame
videoFrame = readFrame(videoFileReader);
[videoFrame, NIQE_results, PIQE_results, BRISQUE_results] = processEachFrame(videoFrame, FrameIndex, NIQE_results, PIQE_results, BRISQUE_results);
% Display video frame to screen
depVideoPlayer(videoFrame);
% Write frame to final video file
writeVideo(outputVideo, videoFrame);
% Show progress
fprintf('The calculation of frame %d is finished!\n', FrameIndex);
FrameIndex = FrameIndex + 1;
end
close(outputVideo);
depVideoPlayer.delete();
ElapsedTime = toc;
% Write calculated results
% Reference: https://www.mathworks.com/help/matlab/ref/fprintf.html
fileID = fopen(fullfile(OutputFolderRoot, [videoFilename '_ElapsedTime.txt']),'w');
nbytes = fprintf(fileID,'Elapsed time is %f seconds.\n', ElapsedTime);
output_filename = fullfile(OutputFolderRoot, [videoFilename '_Results.xlsx']);
% Write title
output_title = [{'Frame Index'} {'NIQE'} {'PIQE'} {'BRISQUE'}];
writecell(output_title, output_filename, 'Sheet', 1, 'Range', 'A1');
index_column = 1:TotalFrameCount;
output_data = [index_column' NIQE_results' PIQE_results' BRISQUE_results'];
writematrix(output_data, output_filename, 'Sheet', 1, 'Range', 'A2');
function [output, NIQE_results, PIQE_results, BRISQUE_results] = processEachFrame(input, FrameIndex, NIQE_results, PIQE_results, BRISQUE_results)
output = input;
NIQE_score = niqe(input);
PIQE_score = piqe(input);
BRISQUE_score = brisque(input);
NIQE_position = [10 10];
PIQE_position = [10 40];
BRISQUE_position = [10 70];
output = insertText(output, NIQE_position, ['NIQE_score: ' num2str(NIQE_score)], 'BoxOpacity', 0.6);
output = insertText(output, PIQE_position, ['PIQE_score: ' num2str(PIQE_score)], 'BoxOpacity', 0.6);
output = insertText(output, BRISQUE_position, ['BRISQUE_score: ' num2str(BRISQUE_score)], 'BoxOpacity', 0.6);
NIQE_results(FrameIndex) = NIQE_score;
PIQE_results(FrameIndex) = PIQE_score;
BRISQUE_results(FrameIndex) = BRISQUE_score;
end
All suggestions are welcome.
Reference:
- Big Buck Bunny: https://peach.blender.org/