Page 1 of 1
Convert images to YUV NV12 format
Posted: 2013-02-28T18:26:05-07:00
by mschuckmann
I need to convert some jpeg images into raw YUV images in the NV12 format.
The NV12 format: 8-bit Y plane followed by an interleaved U/V plane with 2x2 subsampling.
Can this be done with imagemagick?
Thanks,
Matt S.
Re: Convert images to YUV NV12 format
Posted: 2013-02-28T19:39:43-07:00
by snibgo
Code: Select all
convert infile -colorspace YUV outfile
... but I have no idea if this is NV12, or even what NV12 is.
Re: Convert images to YUV NV12 format
Posted: 2013-02-28T19:55:58-07:00
by fmw42
Re: Convert images to YUV NV12 format
Posted: 2013-02-28T20:28:47-07:00
by mschuckmann
Thanks for trying but that doesn't do what I'm looking for.
I described the NV12 format in the original post.
To illustrate YYYYY.... UVUVUV.... (The u and v data is 1/2 the height and width of the Y data.)
The command you posted will produce YUV with the 1/2 height and width U and V planes, but in planiar format
i.e. YYYYYY.... UUUU ..... VVVV....
Matt S.
Re: Convert images to YUV NV12 format
Posted: 2013-02-28T20:33:57-07:00
by mschuckmann
Thanks for the links but I've read through those and haven't found anything.
I was thinking that someone more knowledgeable of IM might know some way to split the planes out and then recombine them the way I want them.
Thanks
Matt S.
Re: Convert images to YUV NV12 format
Posted: 2013-02-28T21:09:37-07:00
by snibgo
Some fancy bit-twiddling in fx might do this.
Or maybe one of the pix-fmts in ffmpeg (which can input one still image and output another) is what you want.
Re: Convert images to YUV NV12 format
Posted: 2013-02-28T21:53:57-07:00
by fmw42
That looks like an interleave in x and y. For something similar see
http://www.imagemagick.org/Usage/video/#deinterlace
http://www.fmwconcepts.com/imagemagick/ ... /index.php
http://www.fmwconcepts.com/imagemagick/ ... /index.php
You should be able to do something similar to interweave by tiling out a 2x2 checkerboard pattern and then using that as a mask to composite the two U,V channels.
http://www.imagemagick.org/Usage/canvas/#tile
convert -size 1x1 xc:black xc:white -append \
\( -clone 0 -rotate 180 \) +append -write mpr:cell +delete \
-size 100x100 tile:mpr:cell mask.gif
convert uchannel vchannel mask.gif -compose over -composite result.
But that will make your image only have two channels, the full Y and the combined UV. What would you put in the third channel? Black?
IM 6 cannot deal with two channels.
Perhaps I misunderstand the proper YUV format.
Re: Convert images to YUV NV12 format
Posted: 2013-02-28T22:25:09-07:00
by mschuckmann
Hmm interesting:
convert src.gif -interlace Partition dest.yuv
Will produce 3 files dest.y, dest.u, dest.v, the .u and .v files are the desired 1/4 size, I just need to figure out how to interleave them into one file after that I can just concatenate the .y file with the combined file and I'm done.
Thanks
Matt S.
Re: Convert images to YUV NV12 format
Posted: 2013-03-01T09:55:17-07:00
by fmw42
I just need to figure out how to interleave them into one file
Was my masking technique not what you need? If not please explain and perhaps I can suggest a modification.
Re: Convert images to YUV NV12 format
Posted: 2013-03-01T10:33:19-07:00
by mschuckmann
It may work I just got to my computer (I was reading your msgs via phone last night) so I haven't had a chance to try it or even understand it.
I tired it, it didn't work right off the bat but that might be because I don't understand a few things, I'm pretty much a newbie to IM
Here is what I did.
Started with an image in.gif that is 1200x1536 pixels.
Run: convert in.gif -interlace Partition foo.yuv
This produces foo.Y, foo.U, foo.V with file sizes foo.y=1843200 (1200x1536), foo.U = foo.V = 460800 (600x768)
I then used your command to generate the mask modifying the size to be 600x768, but I'm not sure if that was correct since I don't fully understand the masking operation.
Next I ran your final command:
convert foo.U foo.V mask.gif -compose over -compsite result.uv
I was hoping that this would give me a file result.uv that is 921600 (460800 x 2) that would be the combined UV planes and then I could just concatenate foo.Y and result.uv together with dd or something.
But as I'm sure you know it didn't work and I got the following errors:
convert: no decode delegate for this image format `foo.U' @ error/constitute.c/ReadImage/533.
convert: no decode delegate for this image format `foo.V' @ error/constitute.c/ReadImage/533.
convert: missing an image filename `result' @ error/convert.c/ConvertImageCommand/3017.
It looks like I need to somehow tell IM that foo.U, foo.V, and result.uv are all simple 8bit raw Gray scale images. Also do have the mask right, should it be the same size as U and V or the size of result.uv (i.e. twice the width of U and V)?
Thanks,
Matt S.
Re: Convert images to YUV NV12 format
Posted: 2013-03-01T11:04:29-07:00
by mschuckmann
The following sequence almost works to produce the interleaved UV plane.
convert -size 1x1 xc:black xc:white -append \( -clone 0 -rotate 180 \) +append -write mpr:cell +delete -size 1200x768 tile:mpr:cell mask.gif
convert in.gif -interlace Partition foo.yuv
mv foo.U foo.U.R
mv foo.V foo.U.R
convert -size 600x768 -depth 8 Gray:foo.U.R -resize 1200x768 Gray:foo.U2.R
convert -size 600x768 -depth 8 Gray:foo.V.R -resize 1200x768 Gray:foo.V2.R
convert -size 1200x768 -depth 8 Gray:foo.U2.R foo.V2.R mask.gif -compose over -composite Gray:result.UV.R
cat foo.Y result.UV.R > foo.nv12
The only problem is the resize operation on foo.U and foo.V appear to do a linear interpolation between points, I want it to do a simple duplication of points so that when we sample the V plane in the compose operation we get the original pixel values instead of the interpolated values.
There has got to be a way to do this.
Matt S.
Re: Convert images to YUV NV12 format
Posted: 2013-03-01T11:17:20-07:00
by magick
Use -sample instead of -resize.
Re: Convert images to YUV NV12 format
Posted: 2013-03-01T11:19:30-07:00
by fmw42
Re: Convert images to YUV NV12 format
Posted: 2013-03-01T11:27:48-07:00
by mschuckmann
I think I found sample instead of resize at the same moment you guys were posting.
The final sequence of operations is:
convert -size 1x1 xc:black xc:white -append \( -clone 0 -rotate 180 \) +append -write mpr:cell +delete -size 1200x768 tile:mpr:cell mask.gif
convert in.gif -interlace Partition foo.yuv
mv foo.U foo.U.R
mv foo.V foo.U.R
convert -size 600x768 -depth 8 Gray:foo.U.R -sample 1200x768! Gray:foo.U2.R
convert -size 600x768 -depth 8 Gray:foo.V.R -sample 1200x768! Gray:foo.V2.R
convert -size 1200x768 -depth 8 Gray:foo.U2.R foo.V2.R mask.gif -compose over -composite Gray:result.UV.R
cat foo.Y result.UV.R > foo.nv12
In case your wondering why I needed to do this, I'm trying to simulate the output of a CCD Imager for input into a video encoder from a sequence of still images. Pretty much all encoders operate in the YUV color space with some sub-sampling of the U and V planes. Different encoders want the YUV images in different layouts some want this NV12 layout, others want everything broken out by color planes, some want everything interleaved i.e. YUYVYUYV..., etc.
Thanks for your help
Matt S.
Re: Convert images to YUV NV12 format
Posted: 2014-04-03T00:05:29-07:00
by michaels
Can you please tell me how can I convert a ppm image to a YUV NV12 format?
Let's say I have a ppm image with size 800X600, called sage_1.ppm, what to do to convert it to a yuv nv12 format?