JuliaIO / VideoIO.jl

Reading and writing of video files in Julia via ffmpeg
https://juliaio.github.io/VideoIO.jl/stable
Other
125 stars 53 forks source link

Reading cropped frames #387

Open juliohm opened 1 year ago

juliohm commented 1 year ago

Is it possible to read crops of frames from disk, say frame[1:100,300:500]? We have these large images that we would like to read as efficiently as possible. Only a specific area is relevant for the calculations, and so reading the full frame to discard tons of pixels isn't very nice.

IanButterworth commented 1 year ago

It's a little out of scope of this package, but if they're TIFFs you can via mmaping https://tamasnagy.com/TiffImages.jl/stable/examples/mmap_lazyio/#Memory-mapping-and-lazy-loading

May be possible for other image formats too

IanButterworth commented 1 year ago

Or by images do you mean frames from a video file?

juliohm commented 1 year ago

I mean frames from a video file. Currently I don't know how to read just portions of a frame. I am reading whole frames and discarding most pixels.

IanButterworth commented 1 year ago

I don't think this is supported in VideoIO. It might be possible but would require looking into whether av_image_copy_to_buffer or an equivalent can be set to target a crop region (into an appropriate buffer size)

spm-sudo-echo commented 1 year ago

I also face the same issue; I want to crop my video in time too. I have stocked the desired images and saved them on the computer. However, making a video of those images is not encoded correctly, and certain portions must be cut. I don't know why. I am using the following code: imgnames = filter(x->occursin("jpg", x), readdir()) # Populate list of all images and sort them based on first numerics intstrings = map(x->split(x,".")[1],imgnames) # Extract index from filenames p = sortperm(parse.(Int,intstrings)) #sort files numerically imgstack = [] for imgname in imgnames[p] push!(imgstack,load(imgname)) end

encoder_options = (crf=15, preset="fast") VideoIO.save("video.mp4", imgstack, framerate=25, encoder_options=encoder_options)