imshow shows rotated image - matlab

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

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

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.

Prevent object from "jumping" in video

I want to make a video with Matlab using the VideoWriter class. I have several frames of an object as single point clouds and I want to put the frames one after another into my video. As for now, I am doing this like this:
function [] = makeMyVideo(videoPath, framerate, filenamestoplot)
writerObj = VideoWriter(videoPath);
writerObj.FrameRate = framerate;
open(writerObj);
figure;ptHandles = onePlot(filenamestoplot);axis off;view(54,12);
axis tight
set(gca,'nextplot','add');
set(gcf,'Renderer','zbuffer');
firstCameraPos = campos;
for k = 1:numel(filenamestoplot)
pause(0.1);
delete(ptHandles);
ptHandles = onePlot(filenamestoplot(k));axis off;view(54,12);
campos(firstCameraPos);
frame = getframe(gcf);
writeVideo(writerObj,frame);
end
close(writerObj);
end
This works, but my 3D object is "making little jumps". I tried to fix this by setting the camera position for every frame, but unfortunately this did not solve the problem. Do you have any idea, how to fix this?
Thanks!

I am not getting exactly gray image on converting video to grayscale?

This is my code. The output is not exactly gray. Can someone tell me what is the problem in the code ?
obj = VideoReader('shaky_cars.avi');
height = obj.Height;
width = obj.Width;
factor = 200/height;
num=10;
intendedFrame = 20;
video = read(obj , [intendedFrame , intendedFrame+num]);
for i = 1:1+num
grayVideo(:,:,i) = double(imresize((rgb2gray(video(:,:,:,i))) , factor));
end
[height , width] = size(grayVideo(:,:,1));
figure(1);imagesc(grayVideo(:,:,1));
And this is the output
A few questions: Have you printed out the images before and after to see what's happening at each step?
Why are you converting the frames to type double?
Suggestion: Try converting the image to uint8 by doing uint8(grayVideo(:,:,i)) and use a gray colormap colormap(gray(256)).
Let me know if this helps
Instead of using imagesc, you can use
imshow(uint8(grayVideo(:,:,1)))
to display your grayscale images.