Hi,
I'm have a video .avi file that i want to crop for these dimensions: 767 410 166 165. This is the code I have now but I keep getting a error. I've learned that I can't do it with VideoReader or vision.VideoReader. Any suggestions are welcomed.
vid=VideoReader('bubbles.mov','Tag', 'My object');
vid_croppped=vid('767 410 166 165')

Réponses (1)

Image Analyst
Image Analyst le 8 Mai 2021

1 vote

See attached example and make the obvious simple modifications.
Write back if you can't figure it out.

12 commentaires

Katrina German
Katrina German le 8 Mai 2021
I don't need to shrink it, I have the dimensions. I inserted my video file but the output video is blurry. Any other suggestions.
Image Analyst
Image Analyst le 8 Mai 2021
It's not clear what you want to do. So you already "have the dimensions" to crop one video, and you cropped it. Then it sounds like you inserted that cropped video file into another one (because you said "inserted my video file"). OK, fine - you inserted a smaller frame onto a larger frame of another video. But it should not be blurry. It should be just a pixel for pixel replacement, so no blur unless it was blurred to start with.
Katrina German
Katrina German le 8 Mai 2021
I ran your file which already had the video file that I'm using (cells.avi) but when I run the code it's blurry. The original file isn't blurry. And yes i have the dimensions i want to crop to: (767 410 166 165) so I want to insert that instead of shrinking it by a factor.
Image Analyst
Image Analyst le 8 Mai 2021
@Katrina German Again, it will not be blurry. If it is blurry, you're not looking at the inserted/cropped part of the video with the same size on the screen as your original video. Obviously if you make that part of the image 16 times as big, it will seem blurry because it has to interpolate the video to make it bigger. But if that's the case, then if you stretched the original video window to the same size as the video (so that the cropped/inserted video in the output video, and the same part of the scene in the input video were both the same size on the screen), then the original video would look blurry too. If you don't believe that, then attach your original video, the cropping coordinates, the output video onto which the cropped portion will be pasted, and your m-file for doing the copying and pasting and I'll take a look at it. If the videos are too big to attach, then use Google drive.
Katrina German
Katrina German le 8 Mai 2021
Sorry for the confusion. I found something else that helped me. I used
roiRect = imrect;
roiPosition = [778 205 180 199]; %dimensions selected
to select the area i wanted and then a loop to go through each of the frames.
for frame = 1:vidObj.NumberOfFrames
disp(frame);
frameObj = read(vidObj, frame);
roiMask = poly2mask(roiPosition(1), roiPosition(2),...
roiPosition(3), roiPosition(4));
% Apply mask to all 3 dimensions/channels
mask_3chan = repmat(roiMask, [1,1,3]);
% Set all other pixels to 0
frameObj(~mask_3chan) = 0;
well = imcrop(frameObj, roiPosition);
imshow(well)
end
So that it displays the cropped video. I appreciate the help anyways.
Image Analyst
Image Analyst le 9 Mai 2021
OK. I don't see any question so I assume we're done now.
David Gill
David Gill le 21 Nov 2022
@Katrina German Hi, can you explain in more detail how your code above works please? I need to crop an AVI video as well. I dont know the exact dimensions, just that i want to remove the bottom 3 layers of pixels. Any help would be great, thanks.
Image Analyst
Image Analyst le 21 Nov 2022
@David Gill you can just use my attached demo but after the line
% Extract the frame from the movie structure.
thisInputFrame = read(inputVideoReaderObject, frame);
add this line to crop off the last 3 rows:
thisInputFrame = thisInputFrame(1:end-3, :, :);
David Gill
David Gill le 22 Nov 2022
Modifié(e) : David Gill le 22 Nov 2022
Hi, thanks for the help. I proccessed it as you recommended but the output resolution is significantly lower. Any idea why? The input frames were 1044 rows by 1280 columns whereas the output is 261 rows by 320 columns, guessing this reduction in rows/columns has something to do with it?
additional info from command window:
General Properties:
Name: 'Newtrial 3_20220208.avi'
Path: 'C:\Users\davidgill\Desktop\WhiskerTracking [custom parameters]'
Duration: 250
CurrentTime: 0
NumFrames: 7500
Video Properties:
Width: 320
Height: 261
FrameRate: 30
BitsPerPixel: 24
VideoFormat: 'RGB24'
David Gill
David Gill le 22 Nov 2022
@Image Analyst update: I reduced the shrinkfactor to 1 and the resolution was drastically improved. Still not perfect though.
Image Analyst
Image Analyst le 22 Nov 2022
@David Gill you were supposed to modify the demo. You were supposed to realize that the resizing line in the demo is not what you want and to delete that line. If you do that there will be no change in resolution, only that the output video will be three lines shorter vertically.
David Gill
David Gill le 22 Nov 2022
Ah I see. thank you for clarifying.

Connectez-vous pour commenter.

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by