VideoWriter with 4k images - matlab

Is there any way to use 4k images to make 4k mp4 video with the VideoWriter function? I used 38 4k images to write a 4k video. But I got the error :Frame must be between 64x64 and 1088x1920. clear all
load images.mat% size of images is 2160x3840x3x38
myVideo = VideoWriter('A9_B4_4k.mp4','MPEG-4');
myVideo.FrameRate = 30;
myVideo.Quality = 100;
open(myVideo);
writeVideo(myVideo,images);
close(myVideo);

Related

Read in mp4 video file with audio, edit the frames and write them to a new file with the audio

I am reading a filename.mp4 video file in MATLAB. I want to edit the images, however, I want to keep the audio intact. Using VideoReader and VideoWriter only does the images part. I used vision.VideoFileReader and 'vision.VideoFileWriter'. I read the video and audio files, then take the image and add a picture next to it. Then write the frame and the audio associated with it. The final video shows the picture I added, but not the original image. Any help appreciated.
v = VideoReader('movie.mp4');
nfr = v.NumberofFrames;
clear v;
vR = vision.VideoFileReader('movie.mp4','AudioOutputPort',1);
fr = vR.info.VideoFrameRate;
vW = vision.VideoFileWriter('filename.avi','AudioInputPort',1,'FrameRate',fr);
pic = imread('picture.png');%read picture
[a1,b1,~] = size(pic);% get picture size to be resized.
for i = 1:nfr
[I,audio] = vR();
I = permute(I,[2,1,3]);%rotate 90 degrees
if i == 1%resize the picture
[a,b,~] = size(I);
pic = imresize(pic,[a,a/a1*b1]);
end
I = [I pic];%combine picture and movie frame
vW(I,audio);%write frame and audio
end
release(vR);
release(vW);
I figured it out.
v = VideoReader('movie.mp4');
nfr = v.NumberofFrames;
clear v;
vR = vision.VideoFileReader('movie.mp4','AudioOutputPort',1,'VideoOutDataType','uint8');
%default of VideoOutDataType is 'single', converting it to a similar format is essential
fr = vR.info.VideoFrameRate;
vW = vision.VideoFileWriter('filename.avi','AudioInputPort',1,'FrameRate',fr);
pic = imread('picture.png');%read picture
[a1,b1,~] = size(pic);% get picture size to be resized.
for i = 1:nfr
[I,audio] = vR();
I = permute(I,[2,1,3]);%rotate 90 degrees
if i == 1%resize the picture
[a,b,~] = size(I);
pic = imresize(pic,[a,a/a1*b1]);%resizing the pic to same height as movie frame %with proportional width
end
I = [I pic];%combine picture and movie frame
vW(I,audio);%write frame and audio
end
release(vR);
release(vW);

Create video with identical frames in matlab

I am creating a video for a presentation with matlab. The video shows a optimization algorithm moving towards the minimum of a costfunction. I want the last seconds of the video to be a freeze-frame, which means that I use the writevideo function to write a couple of identical frames. However when I play the video with VLC player or with powerpoint the correct length of the video is shown, but the identical frames of the video are "skipped". Is there any way I can stop this?
My Code looks like this:
graph = figure('units','pixels','position',[0 0 1920 1080]);
scatter3... %(first frame)
v = VideoWriter('Presentation.avi');
v.Quality = 95;
v.FrameRate = 1;
open(v);
frame = getframe(graph);
writeVideo(v,frame);
for i = 1:10
plot3... %(changing frames)
frame = getframe(graph);
writeVideo(v,frame);
end
for j = 1:5
%(identical frames)
frame = getframe(graph);
writeVideo(v,frame);
end
close(v);
Thank you for answering!

Compressing PNG images using PIL

I am having problem with increasing opacity to the image. My original image is of 230 KB
after i resize the image using the code:
Method 1: imh=imgg.resize((1000,500),Image.ANTIALIAS) #filesize is 558 KB
Method 2: imh=imgg.resize((1000,500),Image.ANTIALIAS)
im2 = imh.convert('P', palette=Image.ADAPTIVE) #filesize is 170KB
Now i am adding transparency of the image by using this code:
def reduce_opacity(im, opacity,pathname):
assert opacity >= 0 and opacity <= 1
if im.mode != 'RGBA':
im = im.convert('RGBA')
else:
im = im.copy()
alpha = im.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
im.putalpha(alpha)
jj=<pathname>
im.save(jj)
return im
Method 1: filesize is 598 KB
Method 2: filesize is 383 KB
So the best code i got till now is
imh=imgg.resize((1000,500),Image.ANTIALIAS)
im2 = imh.convert('P', palette=Image.ADAPTIVE)
reduce_opacity(im2,0.5,name)
which gives me a file size of 383KB. To add opacity it has to be opened in RGBA mode which increase the file size from 170 KB to 383 KB.I am not satisfied with this, i need to reduce the size more, it there any way that i can achieve that, not compromising the quality to a great extent?

Matlab-Making video from images

Im trying to make a video from a file with 95 images in matlab.When i run the program,i get this error:
Error using VideoWriter/writeVideo (line 383)
Frame must be 481 by 272
Error in savenew (line 14)
writeVideo(writerObj,im2frame(Frame)
Although when the number of images that i use for making the video is under 11 i have no problem and the program is constructing the video that i want.Do you know how i can fix the problem?
Thanks in advance
Here is my update code:
ImagesFolder=uigetdir;
jpegFiles = dir(strcat(ImagesFolder,'\*.jpg'));
S = [jpegFiles(:).datenum];
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
VideoFile=strcat(ImagesFolder,'\MyVideo');
writerObj = VideoWriter(VideoFile);
fps= 10;
writerObj.FrameRate = fps;
open(writerObj);
for t= 1:length(jpegFilesS)
Frame=imread(strcat(ImagesFolder,'\',jpegFilesS(t).name));
B = imresize(Frame, 1.0);
C=im2double(B);
writeVideo(writerObj,im2frame(C));
end
close(writerObj);
implay('C:\Program Files\MATLAB\R2013a\bin\sfalmata neo\MyVideo.avi');
All frames of a video must have the same size. Resize your frames using 'imresize'. In case of different data types also apply 'im2double'

How to read avi file in mexopencv

As the title suggest I want to read avi files using mexopencv.I have got this far from here for OpenCV. I want something similar for mexopencv.
CvCapture* capture = cvCaptureFromAVI("infile.avi");
Capturing a frame:
IplImage* img = 0;
if(!cvGrabFrame(capture)){ // capture a frame
printf("Could not grab a frame\n\7");
exit(0);
}
img=cvRetrieveFrame(capture);// retrieve the captured frame
In the C++ API, cv::VideoCapture class can capture videos from both webcams and video files saved on disk:
cap = cv.VideoCapture('file.avi');
for i=1:cap.get('FrameCount')
img = cap.read();
if isempty(img), break; end
imshow(img)
drawnow
end