Making (MPEG-4) movie from multi images (JPG) in MatLab - matlab

Please, I used below code to make a video from multi images (JPG). and it works fine. My problem that I have 10000 images and the code generate the movie with extension avi. This making the size of the movie very large. Are there anyways to save the movie with mp4 extension?
cd('\\Desktop\Movies');
Files = dir('*.jpg');
NumFiles= size(Files,1);
Megamind_Images = uint8(zeros([600 1000 3 NumFiles*5]));
VideoObj = VideoWriter('Create_Video');
VideoObj.FrameRate = 5;
VideoObj.Quality = 80;
count=1;
for i = 1 : NumFiles
I = imread(Files(i).name);
ResizeImg = imresize(I,[600 1000]);
for j = 1 : 5
Megamind_Images(:,:,:,count)=ResizeImg;
count = count + 1;
end
end
open(VideoObj);
writeVideo(VideoObj, Megamind_Images);
close(VideoObj);

Try manually specifying the output profile of your video before writing frames to the object.
VideoObj = VideoWriter('Create_Video.mp4', 'MPEG-4');

Related

How to save a single .mat file of features for many images (loop for)?

I trained two-class-classification (svmtrain) with 15 features for 18 images and I have many objects in a single image. I want to save single .mat file of features of these images. My problem is when I run this code, I have just the variables of last image.
This is my code:
NbIm = size(names1,1);
n1 = 1;
n2 = NbIm;
for n = n1:n2
%1- Read the original image
%2- Processing : Segmentation
%3- characterization :
[B3,L3,N3] = bwboundaries(ICellules); (see picture)
CC = bwconncomp(L3);
BW = bwlabel(L3);
stats1 = regionprops(CC,'Area','Centroid','Eccentricity','Perimeter','ConvexArea','ConvexHull','ConvexImage','MajorAxisLength','MinorAxisLength','Orientation','Solidity','BoundingBox');
for k = 1:length(B3),
V = []; glcm = [];
V = Im_originale(BW==k);
glcm = graycomatrix(V,'Offset',[2 0],'Symmetric', true);
stats= graycoprops(glcm);
Contrast_Cellule = stats.Contrast;
Correlation_Cellule = stats.Correlation;
Energy_Cellule = stats.Energy;
Homogeneity_Cellule = stats.Homogeneity;
Area_cellule = stats1(k).Area;
Perimeter_cellule = stats1(k).Perimeter;
Circularity_cellule = (4*pi*Area_cellule)/Perimeter_cellule^2;
Centroid_cellule = stats1(k).Centroid;
Compactness_cellule = Perimeter_cellule^2/(4*pi*Area_cellule);
MajorAxis_cellule = stats1(k). MajorAxisLength;
MinorAxis_cellule = stats1(k). MinorAxisLength;
Orientation_cellule = stats1(k).Orientation;
Eccentricity_cellule = stats1(k).Eccentricity;
Solidity_cellule = stats1(k).Solidity;
boundary3 = B3{k};
[cc] = chaincode(boundary3);
ai = cc.code;
ai = ai.';
output = calc_harmonic_coefficients(ai,30);
Ampl = 0.5*sqrt((output(1)^2)+(output(2)^2)+(output(3)^2)+(output(4)^2));
Feat(k,:) = [Area_cellule,Perimeter_cellule,Circularity_cellule,Compactness_cellule, Solidity_cellule,Eccentricity_cellule,MajorAxis_cellule,MinorAxis_cellule, Centroid_cellule,Ampl,Contrast_Cellule,Correlation_Cellule,Energy_Cellule,Homogeneity_Cellule];
end
end
save('FeatTrain.mat', 'Feat');
Every time you run the inner loop (k=...) you overwrite Feat, without ever saving it.
If you want to have a single variable for Feat, instead of saving multiple .mat files there are 2 ways to it:
If every time you run the inner loop the size of Feat is the same, you can make it a 3D matrix, by changing you line to Feat(n-n1+1,k,:)=[.... This will make a 3D matrix where each Feat(n,:,:) will be the features of each image.
If instead every time you run the inner loop Feat can change size, then use a cell array, as in Feat{n-n1+1}(k,:)=[....

Matlab - combine two videos into a single split-screen video

I have 2 videos I would like to play side by side in a split screen. They are same duration and dimensions. I found a code developed few years back to do the job. The problem is, it is full of errors maybe due to the fact I am using a newer Matlab version (2014a). The errors start from (%name of the new avi file) onward.
Can anyone please try and fix it:
% select two files:
[filename1,pathname1] = uigetfile('.avi','pick first AVI file');
[filename2,pathname2] = uigetfile('.avi','pick second AVI file');
file1 = fullfile(pathname1,filename1);
file2 = fullfile(pathname2,filename2);
pdMovie1 = aviread(file1);
pdMovie2 = aviread(file2);
fileinfo1 = aviinfo(file1);
fileinfo2 = aviinfo(file2);
% check if AVI files have the same length and height:
if fileinfo1.NumFrames~=fileinfo2.NumFrames || ...
fileinfo1.Height~=fileinfo2.Height
errordlg('files are not compatible!')
else
% inspired by Herbert Ramoser in Message-ID:
% <art0c0$l9fip$1#ID-148798.news.dfncis.de>
for i=1:size(pdMovie1,2)
output(i).cdata = [pdMovie1(i).cdata, pdMovie2(i).cdata];
output(i).colormap = pdMovie1(i).colormap;
end;
% name of the new avi file:
[pathstr,name,ext,versn] = fileparts(filename1);
newmoviename = [pathname1,name,'_combined', ...
num2str(fileinfo1.FramesPerSecond;),ext];
% create the avi file:
movie2avi(output, newmoviename, ...
'fps', fileinfo1.FramesPerSecond;, ...
'compression', 'none');
close
end
If it is just for playing the videos side-by-side, this simplker code will work,
close all
clc
clear
vid1 = vision.VideoFileReader('video1.avi');
vid2 = vision.VideoFileReader('video2.avi');
vidP = vision.VideoPlayer;
while ~isDone(vid1)
frame1 = step(vid1);
frame2 = step(vid2);
frame = horzcat(frame1, frame2);
step(vidP,frame);
end
release(vid1);
release(vid2);
release(vidP);
UPDATE:
I'm assuming both input videos have the same length and frame dimension.
Ok, now if you want to record a new video from the first 2, with the same frame rate of the previous, it is better to use the following code,
close all
clc
clear
vid1 = VideoReader('video1.avi');
vid2 = VideoReader('video2.avi');
videoPlayer = vision.VideoPlayer;
% new video
outputVideo = VideoWriter('newvideo.avi');
outputVideo.FrameRate = vid1.FrameRate;
open(outputVideo);
while hasFrame(vid1) && hasFrame(vid2)
img1 = readFrame(vid1);
img2 = readFrame(vid2);
imgt = horzcat(img1, img2);
% play video
step(videoPlayer, imgt);
% record new video
writeVideo(outputVideo, imgt);
end
release(videoPlayer);
close(outputVideo);
If you have two video clips with different frame rates and resolutions or you would like to join/cut pairs of video clips, use the code below.
For example: My dashcam is able to record video in the front and the rear of the car at the same time. But the front and rear videos come in separate files. I want to show them side by side as well as to join 6 files (3 pairs) into a single file.
%Read video
clc
clear
vidObj_f = VideoReader('video1.mp4');
FrameRate_f = vidObj_f.FrameRate;
vidObj_b = VideoReader('video2.mp4');
FrameRate_b = vidObj_b.FrameRate;
%New video
outputVideo = VideoWriter('combinedvideo.avi');
outputVideo.FrameRate = 24;
open(outputVideo);
skip = 95; %first seconds
j=skip*24;
try
while 1
front = read(vidObj_f,1+round(j*FrameRate_f*1/24));
back = read(vidObj_b,1+round(j*FrameRate_b*1/24));
front = imresize(front,[720 1280]);
videoframe = [front;back];
writeVideo(outputVideo, videoframe);
j = j+1;
end
catch
disp('...end 1');
end
vidObj_f = VideoReader('video3.mp4');
FrameRate_f = vidObj_f.FrameRate;
vidObj_b = VideoReader('video4.mp4');
FrameRate_b = vidObj_b.FrameRate;
skip = 0; %first seconds
j=skip*24;
try
while 1
front = read(vidObj_f,1+round(j*FrameRate_f*1/24));
back = read(vidObj_b,1+round(j*FrameRate_b*1/24));
front = imresize(front,[720 1280]);
videoframe = [front;back];
writeVideo(outputVideo, videoframe);
j = j+1;
end
catch
disp('...end 2');
end
vidObj_f = VideoReader('video5.mp4');
FrameRate_f = vidObj_f.FrameRate;
vidObj_b = VideoReader('video6.mp4');
FrameRate_b = vidObj_b.FrameRate;
skip = 0; %first seconds
j=skip*24;
cut = 30; %after playing 'cut' seconds
try
while 1
front = read(vidObj_f,1+round(j*FrameRate_f*1/24));
back = read(vidObj_b,1+round(j*FrameRate_b*1/24));
front = imresize(front,[720 1280]);
videoframe = [front;back];
writeVideo(outputVideo, videoframe);
if j>cut*24
disp('...end 3 (cut)');
break
end
j = j+1;
end
catch
disp('...end 3');
end
close(outputVideo);

How to read images from folder, divide each image and store it in new folder serially

I am taking images from folder('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY) Images are stored as 1. gif , 2.gif , and so on
I am dividing it . I want to store these images with new name . eg image taken is 1.gif
after diving it into 4 parts, I want to name these new images as 1. gif , 2.gif , 3.gif , 4.gif
Please help me . I have tried my code but it shows the error :
Error in feb11try (line 25)
Img1(i,j) = NewImage(startr+i, startc+j);
Code is :
clc;
close all;
clear all;
srcFiles = dir('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY\*.gif'); % the folder in which ur images exists
%Taking the image from the given URL, it could have been the name of the file with extension if the root folder
n =2; %defining the number of rows
m =2; %defining the number of columns
for c = 1 : length(srcFiles)
NewImage = 'newimage.gif'; %granting permission to create a file and write in it
rf = floor(512/n); %generating the number of row pixels in the new file
cf = floor(512/m); %generating the number of row pixels in the new file
for v = 1:n
for s = 1:m %nXm files need to be made
startr = (v-1)*rf;
startc = (s-1)*cf;
for i = 1 : rf
for j = 1 : cf
Img1(i,j) = NewImage(startr+i, startc+j);
end
end
imwrite( NewImage,'c.gif');
end
end
end
You need to use imread() to read in the source image rather than treating the filename as an image. Based on what you have, I assume your image is single channel. I also made the assumption that your image is unsigned int. Also, keep in mind there are much faster ways to perform this using indexing.
I made a few changes to your sample that should get the job done under these assumptions. This is untested.
clc;
close all;
clear all;
srcFiles = dir('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY\*.gif');
n = 2; %defining the number of rows
m = 2; %defining the number of columns
count = 0;
for c = 1 : length(srcFiles)
srcFilename = ['E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY\' srcFiles(c).name];
srcImg = imread(srcFilename); % Read source image
[~,srcName] = fileparts(srcFilename);
rf = floor(512/n); %generating the number of row pixels in the new file
cf = floor(512/m); %generating the number of row pixels in the new file
% initialize output image
destImg = uint8(zeros(rf,cf));
for v = 1:n
for s = 1:m %nXm files need to be made
startr = (v-1)*rf;
startc = (s-1)*cf;
for i = 1 : rf
for j = 1 : cf
destImg(i,j) = srcImg(startr+i, startc+j);
end
end
count = count + 1;
imwrite( destImg, sprintf('%d.gif', count) );
end
end
end

Comparing several webcam snapshots and saving Them when there is motion Using matlab

My question is how would i get live snapshots from my webcam and only save an image when there is motion, I would like to save several images i.e whenever there is motion the images do not overwrite so that at the end i will be able to see various captured motions
well i was able to come up with a simple solution for my problem as below; it saves images only when there is motion and also adds a time stamp
clear;
clc;
vid = videoinput('winvideo', '1');
set(vid, 'ReturnedColorSpace', 'RGB');
start(vid)
%preview(vid)
while true;
img = getsnapshot(vid);
pause(2)
img1 = getsnapshot(vid);
img12 = rgb2gray(img1);
img13 = rgb2gray(img);
diff1 =sum(sum(img12 - img13));
if diff1>100000;
display ('motion')
counter = 1;
baseDir = 'H:\';
fname = [baseDir ,'Wysla_', num2str((strrep(datestr(clock),':','-'))),'.jpg'];
% make a file name
while exist(fname,'file')
counter = counter + 1;
fname = [baseDir ,'Wysla_', num2str(counter),'.jpg'];
end
imwrite(img1, fname);
else
display('No motion')
end;
end
This is all and it was very simple only thing i can mention is the strrep(datestr(clock),':','-') this a way of saving the image time stamped and since there are semi colons in the date string the strrep() replaecs them with the hyphen or anything else
av fun.

Batch Processing Videos (Matlab) - Issue

I've wrote some code that imports a video and takes some DCT coefficients form a specific region of the image frame by frame, it then output a binary file (containing the coefficients) using a separate function. This works fine when doing it individually but I've tried to implement a batch version of the code as I'm working with over 200 videos files.
However their is a problem, when it goes through the loop it output the wrong file each time. If the number of frames in the next iteration has less frames it writes to the previous frames. I was told that i need to initialise one of my for loops before it runs. However I'm sure how to do this, i think the problem lies in the 2nd for loop but I'm not sure.
Any suggestion would be greatly appreciated!
files = dir('Videos/*.mov');
for m = 1:numel(files);
readerobj = mmreader(files(m).name);
vidFrames = read(readerobj);
numFrames = get(readerobj, 'numberOfFrames');
% Create a MATLAB movie struct from the video frames.
for k = 1 : numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
end
firstFrame = mov(1).cdata;
rect = [172,225,271,143;];
numFrames = length(mov);
dctCoeff = zeros((10*10),numFrames);
for i = 1 : numFrames
frameImage = imcrop(mov(i).cdata, rect);
frameImage = rgb2gray(frameImage);
dctImage = dct2(frameImage);
dctImage = dctImage(1:10,1:10);
dctCoeff(:,i) = reshape(dctImage,1,(10*10));
end
sRate = (1/29.9701)*1e7;
[status, error] = htk_write_mfc(files(m).name, size(dctCoeff,2),sRate,4*size(dctCoeff,1),9,dctCoeff);
status
error
end
Just remove the
numFrames = length(mov);
and it should work fine!
I was told that i need to initialise one of my for loops before it runs
Jup! You're not initializing mov.
I would also recommend you to put the code for one file into a function which you call from your first loop. That is better code!
Try setting mov to [] for each movie that you load. It's going to keep frames from the previous mov and extend that array to match your longest video. Before your loop for k = 1 : ... try writing a line that says mov = [];.
I am not entirely clear on why you are creating the movie struct. You can use the read() function to read specific frames in the video file. You can try the code below:
files = dir('Videos/*.mov');
numDctCoeffs = 100;
for m = 1:numel(files)
readerObj = mmreader(files(m).name);
numFrames = readerObj.NumberOfFrames;
rect = [172,225,271,143];
sRate = (1/29.9701)*1e7;
dctCoeff = zeros(numDctCoeffs, numFrames);
for cnt = 1:numFrames
frameImage = imcrop(read(readerObj, cnt), rect);
frameImage = rgb2gray(frameImage);
dctImage = dct2(frameImage);
dctImage = dctImage(1:10,1:10);
dctCoeff(:, cnt) = dctImage(:);
[status, error] = htk_write_mfc(files(m).name, size(dctCoeff,2),sRate,4*size(dctCoeff,1),9,dctCoeff);
end
end
Also mmreader has been replaced with VideoReader in newer versions of MATLAB. The syntax is the same just a name change.
Hope this helps.
Dinesh