How to read avi file in mexopencv - matlab

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

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);

Save pictures in .pdf, .fig, and .jpeg with different paper orientation

I want to save the same picture in different formats (.pdf, .jpeg, .fig) with diferent orientation.
Here is my current code:
A = readtable('RON.xlsx');
tempo_A = A{:,1:2:end};
[n,m] = size(A);
variavel_original = A{:,2:2:end};
var = {'Y1','Y2','Y3','Y4'};
guardar = {'Y1','Y2','Y3','Y4'};
guardar2 = {'YY'};
var_original = {'Y1','Y2','Y3','Y4'};
n_var = length(var);
for j=1:n_var
pdf=figure;
set(pdf,'PaperUnits','centimeters','PaperPosition',[0 0 29 21],'PaperOrientation','landscape');
plot(tempo_A(:,j),variavel_original(:,j),'-b*');
title(var(j));
grid on
xlabel('tempo');
ylabel('Y');
legend(var_original(j),'Location','best','Orientation','vertical');
saveas(pdf,fullfile('C:\Users\TiagoAlexandre\Documents\MATLAB\GALP\RON\Originais', guardar{j}), 'pdf');
end
So, in the set command I choose the option landscape, it creates a pdf file landscape of size almost A4 which is OK for the purpose.
Questions:
I wanted to save the jpeg file as 'portrait', how would I do that? (I want the jpeg file the same way as the pdf file, with the plot left to right).
I would want to save the .fig file a bit bigger, like a maximized window.
As i posted it saves a pdf landscape, to make a landscape of a jpg file the code is the following:
orient portrait
saveas(pdf,fullfile('C:\Users\TiagoAlexandre\Documents\MATLA‌​B\GALP\RON\Originais‌​', guardar{j}), 'jpg');

I want to save the detected face as a jpg image in matalb

I have detected the face from an image. Now I want to store that detected face as a jpg image in matlab. Plzz some one guide me.
code for face detection is:
img = imread('C:\Users\Anmol\Desktop\face_recognition\Pgm\image1');
facedetector = vision.CascadeObjectDetector()
BBOX = step(facedetector,img)
B = insertObjectAnnotation(img,'rectangle',BBOX,'Face');
title('detected faces');
n = size(BBOX,1);
string = num2str(n);
str = strcat('no of face=',string);
disp(str);
I would be very thankful is some could provide me the code for storing the detected face as a jpg image
You can crop the face using imcrop, and then save the cropped image using imwrite.
faceImage = imcrop(img, BBOX(1,:))
imwrite(img, 'face.jpg');

present video and record from webcam - matlab

I wrote a script that present movie and record from the webcam. The problem is that it rolls the movie in slow motion. I don't think the problem is from taking the snapshot ( I checked it). This is the code:
clear cam
v = VideoReader('movie.MP4');
cam = webcam;
vidWriter = VideoWriter('webcam.avi');
open(vidWriter);
%pre loading the frames
for i=1:50
vtemp = readFrame(v);
vid{i}=vtemp;
end
for index = 1:50
% Acquire frame for processing
img = snapshot(cam);
% Write frame to video
writeVideo(vidWriter, img);
%show the vid frame
imshow(vid{index});
end
close(vidWriter);
clear cam
Any assistance will be much appreciated.

imshow shows rotated image

I'm using Matlab to show the frames in a video sequence , below is my code:
seq=sprintf('walk%d.avi',v); % video's name
videoReader = vision.VideoFileReader(seq);
vidObj = VideoReader(seq);
numFrames = vidObj.NumberOfFrames
for i = 1:numFrames
frame = step(videoReader); % read the next video frame
imshow(frame)
end
Actually it worked fine previously, and i have no idea since when and what caused it to show rotated image . Hope you guys can help me. thank you.
Most updated Matlab function to vertically flip back the frame is:
FlippedFrame = flip(frame,1);