Hi all, I have to create a program that allows me to resize a video by 50%. Does somebody have any tips? How could I set the code?

Respuestas (4)

Image Analyst
Image Analyst el 31 de En. de 2017

2 votos

See my attached demo. In the loop over frame, just resize the frame just read in and write it out with writeVideo(). If you can't figure it out, let me know what frame size you want your output in.

4 comentarios

Alessandro Clocchiatti
Alessandro Clocchiatti el 2 de Feb. de 2017
Your code is excellent, but it doesn't save the frame and it doesn't ask me if save or not the video.
Image Analyst
Image Analyst el 16 de Nov. de 2017
Attached is demo code that resizes a movie in MATLAB.
Abdullah
Abdullah el 15 de Oct. de 2020
Editada: Image Analyst el 15 de Oct. de 2020
Here these two files are good. I ran them, but I am looking for video resizing in dimensions, not in resolution.
Image Analyst
Image Analyst el 15 de Oct. de 2020
Not sure what that means. Image frames have a certain number of pixels across (wide) and high (tall). If you reduce those, you will reduce both the pixel size across and tall, as well as the spatial resolution (meters per pixel) in real world units, meaning the ability to distinguish between two points in meters (or whatever unit you're using). Increasing the number of pixels will give you more pixels across and tall but will not increase the real world spatial resolution, meaning that you'll have more pixels per meter, but how close two points can be and still be resolved will not be changed.

Iniciar sesión para comentar.

Takuji Fukumoto
Takuji Fukumoto el 30 de En. de 2017

0 votos

imresize function is fine. Here is the example.
vidobj = vision.VideoFileReader('moviename')
viewer = vision.DeployableVideoPlayer;
while (1)
A = step(vidobj);
viewframe = imresize(A,0.5);
step(viewer, viewframe);
end
release(vidobj);
release(viewer);

2 comentarios

Alessandro Clocchiatti
Alessandro Clocchiatti el 31 de En. de 2017
Hey takij, believe me, you've been really helpful. I have one last question for you, how can i save the function?
Wenxuan Liang
Wenxuan Liang el 31 de Ag. de 2020
While(1)?~?!!?!?!

Iniciar sesión para comentar.

Takuji Fukumoto
Takuji Fukumoto el 31 de En. de 2017

0 votos

I think you can write like this
function myfunc()
%code
end

4 comentarios

Alessandro Clocchiatti
Alessandro Clocchiatti el 31 de En. de 2017
Oh I'm sorry, I did not explain this fully earlier. How can I save the video in this function?
Takuji Fukumoto
Takuji Fukumoto el 31 de En. de 2017
Alessandro Clocchiatti
Alessandro Clocchiatti el 2 de Feb. de 2017
Including "videoFWriter = vision.VideoFileWriter" in the loop, the VideoWriter function didn't work. Could you tell me why?
Takuji Fukumoto
Takuji Fukumoto el 2 de Feb. de 2017
Not in the loop. When you use system object, you should use step function. This is a sample code to save file.
% Set up the reader and writer objects.
videoFReader = vision.VideoFileReader('viplanedeparture.mp4');
videoFWriter = vision.VideoFileWriter('myFile.avi','FrameRate',...
videoFReader.info.VideoFrameRate);
Write the first 50 frames from original file into a newly created AVI file.
for i=1:50
videoFrame = step(videoFReader);
step(videoFWriter,videoFrame);
end
%%Close the input and output files.
release(videoFReader);
release(videoFWriter);

Iniciar sesión para comentar.

Abdullah Amer Mohammed Salih
Abdullah Amer Mohammed Salih el 24 de Nov. de 2020

0 votos

Thanks for your code, I need to change video in dimentions means the video for example is 100 x 100 it will be 30 x30. that is a liniear transformation.
I also need to change the shape of the video means I change it from rectangle to polygon for example. any clue how I do that?

10 comentarios

Image Analyst
Image Analyst el 24 de Nov. de 2020
Who are you replying to? Attached is my code for resizing a video. Adapt as needed. You can't have non-rectangular videos, however you can mask the frames so that they are black (or whatever color you want) outside the polygon. Here is the code:
[rows, columns, numberOfColorChannels] = size(maskedRgbImage)
mask = poly2mask(x, y, rows, columns);
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
Where x and y are the coordinates of the vertices of your polygon.
Abdullah Amer Mohammed Salih
Abdullah Amer Mohammed Salih el 24 de Nov. de 2020
Thanks a lot for your reply, but how if I want to change the video to heart shate or diognal shape for example, you mean all of that cannot be done?
Image Analyst
Image Analyst el 24 de Nov. de 2020
Like I said, you can have a rectangular video with black outside a heart-shaped portion that has stuff inside it. You just need to mask your frame. You can crop it to the bounding box of the shape if you want. Start a new question with your video and polygon coordinates attached if you can't figure it out.
Abdullah Amer Mohammed Salih
Abdullah Amer Mohammed Salih el 24 de Nov. de 2020
Dear @Image Analyst
Thanks for your reply
Can you guide me step by step please?
Image Analyst
Image Analyst el 24 de Nov. de 2020
Here is the code, attached. If you like it, please Vote for my answer (not yours) above. Thanks in advance.
Abdullah Amer Mohammed Salih
Abdullah Amer Mohammed Salih el 24 de Nov. de 2020
MAN...thanks a lot seriously, I am so thankful....I already voted for your answer...
Now how if I want to change the amask shape just share with me the commands for the shapes and I will figure it out..
Thanks alot
if I want to change the shape of the mask I change this block
% Create x and y for the heart, and then create a binary image mask using poly2mask().
[X, Y] = MakeHeart(false);
% Rescale x and y to span the image.
X = rescale(X, 1, inputVideoColumns);
Y = rescale(Y, 1, inputVideoRows);
mask = flipud(poly2mask(X, Y, inputVideoRows, inputVideoColumns));
imshow(mask, []);
with this ?
[rows, columns, numberOfColorChannels] = size(maskedRgbImage)
mask = poly2mask(x, y, rows, columns);
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
Image Analyst
Image Analyst el 25 de Nov. de 2020
There are no votes for my Answer. Not sure what you voted for but it was not my answer.
You can replace that with that, IF you have somehow assigned x and y, which you have not shown.
Abdullah Amer Mohammed Salih
Abdullah Amer Mohammed Salih el 25 de Nov. de 2020
Please check the vote now,
Can you explain to me how to change the shape of mask?
I saw the function that you put at the end of the code MakeHeart, how it should be if it is any other shape? can you share the concept?
Thanks a lot
Image Analyst
Image Analyst el 25 de Nov. de 2020
Thanks.
You can make whatever list of (x,y) coordinates you want. Or you can just start with a binary image (mask) that you got from thresholding or wherever.
Are you going to be
  1. creating a mask from a list of (x,y) coordinates (in which case you'll use poly2mask), or
  2. creating a mask by some other means, like thresholding or some other image segmentation method?
If you need help, attach either the list of (x,y) coordinates in a .mat file with the paper clip icon, or attach the mask image itself.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 30 de En. de 2017

Comentada:

el 25 de Nov. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by