-1

I have an bayer image .bmp I would like to debayer with FFMpeg, I thought that possibly FFMpeg knows to debayer it by default so I tried to use such a very simple query

ffmpeg -i input.bmp output.png

but output.png looks gray, so ffmpeg doesn't apply debayer automatically. I tried to figure out if is it possible with ffmpeg, but there is almost nothing about it on google.

Image example (it is too large to upload it here): https://drive.google.com/file/d/1V8HwOuIo9PBX3ix0eKFQFGimskU_H0mN/view?usp=sharing

How to do it?

UPD

this is what I tried to do

ffmpeg -y -i D:\Buffer\Bayer\Time0000000_img.bmp -pix_fmt gray D:\Buffer\Bayer\bmp\test11.y -hide_banner
ffmpeg -y -pixel_format bayer_rggb8 -video_size 4104x3006 -i D:\Buffer\Bayer\bmp\test11.y D:\Buffer\Bayer\bmp\result1.png -hide_banner

and there is an error I get

Input #0, image2, from 'D:\Buffer\Bayer\bmp\test11.y':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 1013196 kb/s
    Stream #0:0: Video: rawvideo ([186]RG[8] / 0x84752BA), bayer_rggb8, 4104x3006, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> png (native))
Press [q] to stop, [?] for help
[rawvideo @ 0000027da39d3f80] Invalid buffer size, packet size 5065984 < expected frame_size 12336624
Error while decoding stream #0:0: Invalid argument
Output #0, image2, to 'D:\Buffer\Bayer\bmp\result1.png':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: png, rgb24, 4104x3006, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 png
frame=    0 fps=0.0 q=0.0 Lsize=N/A time=00:00:00.00 bitrate=N/A speed=   0x
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
Conversion failed!

if I change the resolution to 1920x1080 like this

ffmpeg -y -pixel_format bayer_rggb8 -video_size 1920x1080 -i D:\Buffer\Bayer\bmp\test11.y D:\Buffer\Bayer\bmp\result1.png -hide_banner

I don't get an error, but the output is wrong

enter image description here

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121

1 Answers1

0

It doesn't look like FFmpeg has a Debayer filter.
The only documented option is selecting Bayer as input pixel format.

I found a solution executing FFmpeg twice:

  • Convert from BMP to raw Bayer:

     ffmpeg -y -i Time0000005_img.bmp -pix_fmt gray tmp.y
    
  • Convert from raw Bayer to RGB (Debayer):

     ffmpeg -y -pixel_format bayer_rggb8 -video_size 4104x3006 -i tmp.y rgb.png
    

Single line solution (using pipes):

ffmpeg -y -i Time0000005_img.bmp -vf format=gray -f rawvideo pipe: | ffmpeg -y -f rawvideo -pixel_format bayer_rggb8 -video_size 4104x3006 -i pipe: rgb.png

You may also try gamma correction:

ffmpeg -y -i Time0000005_img.bmp -vf format=gray -f rawvideo pipe: | ffmpeg -y -f rawvideo -pixel_format bayer_rggb8 -video_size 4104x3006 -i pipe: -vf eq=gamma=2.2 rgb.png

Note:
I manually sets video_size to 4104x3006.
Setting video_size automatically is possible using FFprobe, but it makes the solution more complicated.


Result (after gamma correction) resized by a factor of 1/10:
enter image description here

Rotem
  • 30,366
  • 4
  • 32
  • 65
  • I verified my solution before posting, and it works. **1.** Download the BMP image from your link (in some cases images are get modified when uploaded). **2.** Download the latest version of FFmpeg (I am using version `n4.3.1-221-gd08bcbffff`). **3.** Make sure the resolution of the BMP image is `4104x3006`. – Rotem May 19 '21 at 08:18
  • I also found out that this query works much faster `ffmpeg -i D:\Buffer\Bayer\ddd\Time0000005_img.bmp -f rawvideo pipe: | ffmpeg -y -f rawvideo -s 4104x3006 -pix_fmt bayer_rggb8 -i pipe: -pix_fmt rgb24 -frames 1 D:\Buffer\Bayer\bmp\result3.bmp -hide_banner` - do you know what is a diff? – Sirop4ik May 19 '21 at 09:48
  • I guess (not sure) that `-frames 1` is faster because it tells the second `ffmpeg` to expect only one frame. The second `ffmpeg` receives one frame, flushes the pipe and terminates. I suppose that without `-frames 1`, there are buffering mechanisms. The second `ffmpeg` waits for the first `ffmpeg` to terminate, and only then flushes the pipe. – Rotem May 19 '21 at 10:17
  • I checked it on the bench of images it shows me `speed=0.265x` while on the second implementation `speed=1.16x`, **BUT** after I changed the extension (on your implementation) from `.png` to `.bmp` it also performed the same speed – Sirop4ik May 19 '21 at 10:30
  • I am new in this field, and it is interesting why output format has such an impact on the speed, I assume it happens because the ffmpeg uses different encoders related to different formats... – Sirop4ik May 19 '21 at 10:32
  • 1
    See: [ffmpeg override output file if exists](https://stackoverflow.com/questions/39788972/ffmpeg-override-output-file-if-exists) – Rotem May 20 '21 at 08:29
  • it was found out that there is a problem with ffmpeg debayer approach, like ffmpeg debayer algorithm not enough efficient and as a result images have like blur effect. I posted the question here https://superuser.com/q/1657175/678484 and would like to know what do you think about it? Because currently looks like that it is better to use other approaches for debayer... – Sirop4ik Jun 16 '21 at 18:06
  • You can try using [OpenCV](https://docs.opencv.org/4.5.2/d8/d01/group__imgproc__color__conversions.html). OpenCV supports at least 3 types of Demosaicing algorithms. The simplest way to use OpenCV is by a Python script. You may also use OpenCV with C++, and there is also a JAVA binding (and more). – Rotem Jun 16 '21 at 18:42