Log in

View Full Version : MPEG-2 Y Channel -> 8-bit Image File?


SMurf
3rd February 2009, 00:41
Hello,

I am attempting to do some image analysis as part of a university project. To this end I would like to be able to obtain each frame's Y channel from an MPEG-2 Elementary Stream (.m2v) and convert it into an 8-bit image file such as .bmp or (preferably) .png.

One of my objectives is to minimize the errors introduced through colour space conversion. Although I could achieve this task with e.g. DGIndex -> Avisynth -> VirtualDub -> .bmp, the problem that I have with this process is that VirtualDub would convert the frame's colour space to RGB32 when saving the .bmp file, then I have to open it up in an image editor and convert it back to greyscale, which is really two lossy transformations too many.

If there is no direct way of saving an 8-bit image file, is there a way that I can extract the raw Y channel data for each frame to convert myself?

Guest
3rd February 2009, 00:51
vid=
tweak(sat=0)
imagewriter(type="png")

Sagekilla
3rd February 2009, 00:52
Oops, neuron2 got to it before me.

If you do conversion from YV12 --> Grayscale (setting the chroma components to nothing) it's a completely lossless conversion from RGB24/32 <-> YV12.

SMurf
4th February 2009, 01:26
The script supplied generated an error as apparently DevIL requires RGB32 input to write PNGs.

However I did instead upgrade to Avisynth 2.6 (unstable) and used ConvertToY8() instead, although DevIL insists on writing compressed images upside-down. Those crazy cats... :rolleyes:

Thanks for the clues guys.

drmpeg
4th February 2009, 12:30
If there is no direct way of saving an 8-bit image file, is there a way that I can extract the raw Y channel data for each frame to convert myself?
Assuming you have a C compiler, I would suggest you take a look at the old MSSG decoder.

http://www.mpeg.org/pub_ftp/mpeg/mssg/mpeg2v12.zip

First, it has an option to directly output the decoder Y component to a file. Second, you can just modify the YCbCr to RGB conversion routines in src/mpeg2dec/store.c to test your ideas.

You can get a Windows command line executable here:

http://www.w6rz.net/reference_decoder.zip

Ron

J_Darnley
4th February 2009, 12:52
The script supplied generated an error as apparently DevIL requires RGB32 input to write PNGs.
ConvertToRGB32(). Also, do you want to prevent range conversions? Then you need to add matrix="PC.601" into the convert.

So that would make your script:
Source()
Greyscale()
ConvertToRGB32() #or ConvertToRGB32(matrix="PC.601")
ImageWriter()