How to read multiple raw tiff files from a folder

Hi,
I have downsampled a 720p raw video file to 360p using FFmpeg and stored all the frames as. tiff files in a folder. I am trying to read these raw files and upsample them using imresize to 720p again. Goal is to calculate PSNR for the original raw 720p file and the upsampled 720p files. Below is the code I am using after having created folder of my 360p raw files.
input_path = '.path for folder 360p_raw';
output_path = 'path for folder u_pic_720p where i want my upsampled tiff files';
cd(input_path)
dc = dir('*.tiff'); % loads all the image infos
for i = 1 : length(dc) % loop through all your images to resize
% %load image
baseFileName = dc(i).name;
image = imread(baseFileName)
%resize image
newimage=imresize(image, 2);
%save new image
imwrite(newimage, num2str(i, './u_pic_720p/%04d.tiff'))
end
however i am getting error with imread function
Error using imread>get_format_info (line 541)
Unable to determine the file format.
But the same imread is working fine for original 720p raw tiff files. My guess is the error is beacuse the 360p raw tiff files have been downsampled so matlab is unable to read them. Is there a way to read these downsampled files in matlab and resize them. Also please guide on how to calculate PSNR. I would need to take average PSNR for every frame from both the original 720p and downsampled 360p files to pass as input to the inbulit psnr function.

3 Comments

Please zip one of the .tiff and attach that for us to test with.
I have attched tiff files from original folder raw 720p folder and downsampled 360p folder. However I just realised that original folder has filename starting from 0000.tiff and both the other folders from 0001.tiff. I am using below ffmpeg command to create both these folders.
close all;
clear all;
v = VideoReader('filename');
i = 0;
while hasFrame(v)
frame = readFrame(v);
imwrite(frame, num2str(i, './720p/%04d.tiff'));
i = i + 1;
end;
cmdline = [' !ffmpeg -i ./720p/%04d.tiff -vcodec rawvideo -b:v 256k -pix_fmt yuv420p 720p_raw/%04d.tiff ']
eval(cmdline)
cmdline = [' !ffmpeg -i ./720p_raw/%04d.tiff -vcodec rawvideo -b:v 256k -pix_fmt yuv420p -s 640x360 ./360p_raw/%04d.tiff ']
eval(cmdline)
Your 0001.zip and 0001_360praw.zip do not contain valid TIFF files. TIFF files must start with either the characters II or MM but yours just go directly into data.

Sign in to comment.

Answers (1)

Try to create the filename like this:
folder = fullfile(pwd, 'u_pic_720p');
if ~isfolder(folder)
% Folder does not exist yet so create it.
mkdir(folder);
end
baseFileName = sprintf('%04d.tif', i);
fullFileName = fullfile(folder, baseFileName);
imwrite(newimage, fullFileName);

6 Comments

Hi,
The tried using the above but the file remains empty. no tiff files generated inside u_pic_720p folder
for i = 1 : length(dc)
baseFileName = sprintf('%04d.tif', i);
fullFileName = fullfile(output_path, baseFileName);
end;
Also, I need to read all files in order to resize them .
So, they're not there. Can you attach a screenshot of File Explorer showing the folder in the address bar, and the folder tree heirarchy in the left panel, and a list of filenames in the right panel?
input_path = '.path for folder 360p_raw';
output_path = 'path for folder u_pic_720p where i want my upsampled tiff files';
dc = dir(fullfile(input_path, '*.tiff')); % loads all the image infos
for i = 1 : length(dc)
baseFileName = dc(i).name;
try
IMG = imread(fullfile(input_path, baseFileName));
newIMG = imresize(IMG, 2);
imwrite(newIMG, fullfile(output_path, baseFileName));
catch
fprintf('imread failed on "%s"', baseFileName);
end
end
Thanks, this works for me !!
I am not sure why you do not use ffmpeg to do the resizing too?
thats the restriction with my work I have to upsample using matlab only

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

No tags entered yet.

Asked:

on 12 Apr 2020

Commented:

on 14 Apr 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!