bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.56k stars 1.58k forks source link

Java2DFrameConverter getBufferedImage throws NullPointerException with crop filtered frame #1621

Open zouxiaodong opened 3 years ago

zouxiaodong commented 3 years ago

frame video filter:fps=fps=0.2,crop=639:216:0:0 crop=639:216:0:0 will cause NullPointerExcepttion

**filter.push(frame);
Frame frameImg;
if ((frameImg = filter.pull()) != null) {
    if (frameImg.image != null) {
        log.info("frame image width:{} height:{}", frameImg.imageWidth, frameImg.imageHeight);
        log.info("save image to file:{}", filename);
        int type = Java2DFrameConverter.getBufferedImageType(frameImg);
        double gamma = type == BufferedImage.TYPE_CUSTOM ? 1.0 : inverseGamma;
        bufferedImage = converter.getBufferedImage(frameImg, gamma, flipChannels, null);
        ImageIO.write(bufferedImage, "jpg", new File(filename));
    }
}**

ERROR] 2021-04-09 13:04:58.306 [grab--0] Grabber - java.lang.NullPointerException at java.awt.image.ComponentColorModel.bitsHelper(ComponentColorModel.java:359) at java.awt.image.ComponentColorModel.(ComponentColorModel.java:273) at java.awt.image.ComponentColorModel.(ComponentColorModel.java:351) at org.bytedeco.javacv.Java2DFrameConverter.getBufferedImage(Java2DFrameConverter.java:639)

saudet commented 3 years ago

Make sure that the output frames are in a format supported by Java 2D such as "bgr24".

zouxiaodong commented 3 years ago

Make sure that the output frames are in a format supported by Java 2D such as "bgr24".

converter.getBufferedImage work well ,if filter only fps=fps=0.2,

grabber.getPixelFormat() is AV_PIX_FMT_BGR24

FFmpegFrameFilter filter = new FFmpegFrameFilter(videoFilter, null, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels()); filter.setPixelFormat(grabber.getPixelFormat()); filter.setSampleFormat(grabber.getSampleFormat()); filter.setFrameRate(grabber.getFrameRate()); filter.setSampleRate(grabber.getSampleRate());

saudet commented 3 years ago

The format filter should be able to do that with "format=pix_fmts=bgr24": https://ffmpeg.org/ffmpeg-filters.html#format-1

zouxiaodong commented 3 years ago

The format filter should be able to do that with "format=pix_fmts=bgr24": https://ffmpeg.org/ffmpeg-filters.html#format-1

”fps=fps=0.2,crop=639:216:0:0,format=pix_fmts=bgr24“ not work yet. why need to set format filter? the input video format is AV_PIX_FMT_BGR24

saudet commented 3 years ago

What does System.out.println(frameImg.image[0]) prints out?

zouxiaodong commented 3 years ago

What does System.out.println(frameImg.image[0]) prints out?

java.nio.DirectByteBuffer[pos=0 lim=1244160 cap=1244160] frameImg.imageChannels is 9, so colorSpec is NULL? but why frameImg.imageChannels is 9 with crop filter?

saudet commented 3 years ago

Hum, I guess it's a bug when settings the imageChannels, where it assumes that the stride covers the width: https://github.com/bytedeco/javacv/blob/master/src/main/java/org/bytedeco/javacv/FFmpegFrameFilter.java#L727 You can probably set it to 3 manually and it's going to work.

So, we need to figure out how we're supposed to get the number of channels with FFmpeg, correctly...

zouxiaodong commented 3 years ago

Hum, I guess it's a bug when settings the imageChannels, where it assumes that the stride covers the width: https://github.com/bytedeco/javacv/blob/master/src/main/java/org/bytedeco/javacv/FFmpegFrameFilter.java#L727 You can probably set it to 3 manually and it's going to work.

So, we need to figure out how we're supposed to get the number of channels with FFmpeg, correctly... I Made a Mistake, the name option crop filter also not work.
frameImg.imageChannels = 3; that works , thanks.