Preserving MP4 quality when using VideoWriter in MATLAB [duplicate] - matlab

I need to get an mpeg4 file to use in another application, from an original mpeg4 video I loaded into matlab and edited (frame by frame).
To do so, I tried using VideoWriter, setting the quality to 100%:
newVid = VideoWriter(outputfilename, 'MPEG-4');
newVid.FrameRate = fps;
newVid.Quality = 100;
However, the result I'm getting is very poor and if the original unedited video size was ~50MB, the one I get post-edit in matlab is around ~20MB, and I don't know how to keep the quality and size as they were.
I also tried saving as .avi and converting to mpeg4 with ffmpeg, but it gave even poorer results.
Any ideas?

MPEG-4is a compressed format so there is information loss when you save it in this format. Quality is the quality of the compression but you do not want any compression. To force Matlab not to use compression my guess is to use the statement below as the default is H.264
newVid.VideoCompressionMethod = 'none'

Related

How do I use imagemagick to convert HEIC to JPG and preserve quality?

I'd like to batch convert .heic images (e.g. iPhone photograph) to .jpg files with imagemagick, with the goal of retaining as much of the quality from the original image as possible. To be clear, the resulting output size is not a concern.
I've been using
magick input.heic -quality 100% output.jpg
Is it possible to do better?
No, its not possible to do better per ImageMagick's website:
Set the quality to 100 to produce lossless HEIC images. Requires the libheif delegate library.
I interpret lossless as NOTHING is lost from original picture. However since you ARE converting to another file type maybe its possible you lose something, are you seeing any artifacts/issues?

Read avi video - Matlab

I want to read an AVI File in Matlab. I tried it according to this link: http://inside.mines.edu/~whoff/courses/EENG512/lectures/other/Matlab_movies.pdf :
clear all
close all
movieObj = VideoReader('ap001_BL0_SP2_cam03_compressed.avi'); % open file
get(movieObj) % display all information about movie
nFrames = movieObj.NumberOfFrames; %shows 310 in my case
for iFrame=1:2:nFrames
I = read(movieObj,iFrame); % get one RGB image
imshow(I,[]); % Display image
end
I get the following error:
Error using VideoReader/read (line 145) The frame index requested is
beyond the end of the file.
Error in test_video_read (line 9) I = read(movieObj,iFrame); % get
one RGB image
(Shortened) Output from "get(movieObj)" is:
General Settings:
Duration = 10.3333
Name = ap001_BL0_SP2_cam03_compressed.avi
Type = VideoReader
Video Settings:
BitsPerPixel = 24
FrameRate = 30
Height = 1280
NumberOfFrames = 310
VideoFormat = RGB24
Width = 960
So it should be possible to read the first frame, as there are 310 available!
I can play the AVI file in VLC-Player, so the codec should be already installed, right?
I'm using MATLAB R2013a, Windows 7. Can anyone please help, thank you!
VLC player is built using the ffmpeg codecs. VideoReader uses DirectShow and Media Foundation API's that are Windows Platform API's and are different from ffmpeg. So, if a file plays using VLC, it is not guaranteed to be opened by VideoReader. Couple of things you can do:
Can the file be viewed on Windows Media Player? If so, in most cases it should work with VideoReader. If not, then you do not have the suitable codecs. Try installing ffsdhow or K-lite codec pack.
If file works on Windows Media Player but VideoReader does not support, it would indicate a bug. A workaround that has worked for me in the past is that I install codecs mentioned above and give it a try again.
If (1) and (2) do not help, use software like handbrake or Mirro to transcode the file into MP4 which works win VideoReader.
Hope this helps.
Dinesh
I have tested your Matlab code using some avi files and I have no problem with them. So, I think it is your avi file that is causing the error.
I have a similar problem before, where my (mp4) movies can be played in any media players but matlab cannot open them. In my case, the problem is the pixel format when the mp4 movies were compiled (by ffmpeg). By default, my movies were compiled with the High 4:4:4 Predictive (yuv444p) format but Matlab cannot handle this. When I switched to an older pixel format (yuv420p), I don't have any problem loading the movies into Matlab.
You can check if this is the problem using ffprobe, which is part of ffmpeg and you can download them from https://www.ffmpeg.org.
Otherwise, have you tried with an uncompressed avi?

Matlab: how to save TIFF with transparency or PNG with no compression?

I have to process a lot of images and save results to image files with transparency in Matlab. But PNG compression takes too much time for me. How can I save PNG with no compression or TIFF with transparency? Are there other ways to save an image without compression and with transparency?
It's my first question here, sorry for my bad English and wrong question style if there are any mistakes in question.
Using the TIFF class in Matlab you can write TIFFs with transparancy:
%# create a synthetic RGBA image
ch = checkerboard(100);
rgba = repmat(ch,[1,1,4]);
rgba(:,:,4) = rgba(:,:,4)==0;
rgba = uint8(round(rgba*255));
%# create a tiff object
tob = Tiff('test.tif','w');
%# you need to set Photometric before Compression
tob.setTag('Photometric',Tiff.Photometric.RGB)
tob.setTag('Compression',Tiff.Compression.None)
%# tell the program that channel 4 is alpha
tob.setTag('ExtraSamples',Tiff.ExtraSamples.AssociatedAlpha)
%# set additional tags (you may want to use the structure
%# version of this for convenience)
tob.setTag('ImageLength',size(ch,1));
tob.setTag('ImageWidth',size(ch,2));
tob.setTag('BitsPerSample',8);
tob.setTag('RowsPerStrip',16);
tob.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
tob.setTag('Software','MATLAB')
tob.setTag('SamplesPerPixel',4);
%# write and close the file
tob.write(rgba)
tob.close
%# open in Photoshop - see transparency!
Matlab's imwrite does not have parameter for the PNG compression level. If it did, you could set it to zero for no compression. While for TIFF it does have a none option for Compression, there is no alpha channel. You can write to the old Sun Raster (RAS) format with an alpha channel and no compression. Though nothing would likely be able to read it.
"There is no uncompressed variant of PNG. It is possible to store uncompressed data by using only uncompressed deflate block"
The uncompressed deflate block uses a header of 5 bytes + up to 65535 bytes of uncompressed data per block.
http://www.w3.org/TR/PNG-Rationale.html

Set quality for PNG images in MATLAB

I have a matlab code and it generates a .png image of 1024*768 resolution. The images are about 450KB in size and I need to know how to optimise and compress these images using matlab.
Can't I play with the quality as in JPEG ?
I read the imwrite manual and don`t seem to find a good way to do this.
Is there any way to achieve it in matlab ?
By design PNG files are lossless - there is no 'quality' to be adjusted (it's probably why a mod changed your question title).
You can reduce the number of colors in the image (the color depth) which will in turn reduce filesize (PNG-8 instead of PNG-24, for example), but the whole point of PNG is it produces lossless images, so there is simple no quality value a la JPEG.
Taken from the manual :
A parameter of input in case it is JPEG:
'Quality' - A number between 0 and 100; higher numbers mean higher quality (less image degradation due to compression), but the resulting file size is larger.
imwrite(x,'c:\1.jpg','Quality',10)
edit: Sorry, I answered this one while the title was JPEG and not PNG.
PNG doesn't support any quality settings - it is a lossless format. The compression it applies is generally as good as possible.

How to generate a video from matlab without codec errors?

Using my Windows 7 64bit machine, I'm trying to generate an avi file from Matlab using the sequence
aviobj = avifile('test.avi', 'fps', 25);
% a loop of aviobj = addframe(aviobj, frame)
close(aviobj)
However, the file generated is corrupt - with VLC it looks sheared and with bad colors, with Media Player it was a black screen, and with Divx Plus player it looks okay but generates a warning.
I've tried specifying other codec types (via avifile('test.avi', 'fps', 25, 'compression', 'TYPE') but Matlab never seems to be able to find that codec - I've tried Indeo and cvid and MSVC and MRLE and many more, but Matlab just generates a "not a supported compression method" warning, and then outright fails when addframe is invoked.
How can I solve the above problem, or alternatively, is there a different, simple way to just generate an avi by adding a frame at a time?
How to generate a video from matlab without codec errors?
try to use mmwrite
http://www.mathworks.com/matlabcentral/fileexchange/15881-mmwrite
Well after a few more searches online and experimentation, looks like others have encountered this problem as well, and suggested just using a different program to compress it and that should also fix the file, so that's what I did:
Generated an uncompressed file
Opened it in VirtualDub(good open-source video software which doesn't require installation)
From the video menu I chose "compression" and selected the "Microsoft Video 1" option
Saved the file via the save-as menu option
It now looks correct in all the players I've tried, and the file is smaller in size.
What you are describing what happens in VLC is probably related to having a resolution that is not supported by the codec. Try resolutions that are multiples of 2,4,8,16 and see what works. VirtualDub probably takes care of that somehow.
Otherwise it's really simple. All you have to do:
aviobj = avifile('example.avi', 'compression', 'none', 'fps', 25);
for i=1:1000
%generating the filenames
filename = strcat(FolderName,'/frame', sprintf('%05d', i),'.bmp');
I = imread(filename);
aviobj = addframe(aviobj,I);
end;
aviobj = close(aviobj);
I know I am replying to a very old thread, but I helped my brother with this and learned a few things that might help others:
My brother had a very small video, the result of an MRI scan, 51x51 pixels. Using VideoWriter with profile "Uncompressed AVI" worked, but did not play well in VLC. Virtualdub also threw errors.
What fixed it, is making sure that the video dimensions where a multiple of 2.
A 96x96 video played fine and could also be transcoded to XVID in virtualdub.
Also, with very small videos, set the video output mode to wingdi in VLC. With OpenGL or Direct3D and overlay mode, the GPU will do the scaling and interpolate pixels in between.