Convert images to YUV NV12 format

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mschuckmann
Posts: 7
Joined: 2013-02-28T18:24:00-07:00
Authentication code: 6789

Convert images to YUV NV12 format

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert images to YUV NV12 format

Post by snibgo »

Code: Select all

convert infile -colorspace YUV outfile
... but I have no idea if this is NV12, or even what NV12 is.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images to YUV NV12 format

Post by fmw42 »

mschuckmann
Posts: 7
Joined: 2013-02-28T18:24:00-07:00
Authentication code: 6789

Re: Convert images to YUV NV12 format

Post 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.
mschuckmann
Posts: 7
Joined: 2013-02-28T18:24:00-07:00
Authentication code: 6789

Re: Convert images to YUV NV12 format

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert images to YUV NV12 format

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images to YUV NV12 format

Post 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.
mschuckmann
Posts: 7
Joined: 2013-02-28T18:24:00-07:00
Authentication code: 6789

Re: Convert images to YUV NV12 format

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images to YUV NV12 format

Post 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.
mschuckmann
Posts: 7
Joined: 2013-02-28T18:24:00-07:00
Authentication code: 6789

Re: Convert images to YUV NV12 format

Post 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.
mschuckmann
Posts: 7
Joined: 2013-02-28T18:24:00-07:00
Authentication code: 6789

Re: Convert images to YUV NV12 format

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Convert images to YUV NV12 format

Post by magick »

Use -sample instead of -resize.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images to YUV NV12 format

Post by fmw42 »

replace -resize with -scale or -sample

see
http://www.imagemagick.org/script/comma ... .php#scale
http://www.imagemagick.org/script/comma ... php#sample

Edit:
Seems that I posted at the same time as Magick
mschuckmann
Posts: 7
Joined: 2013-02-28T18:24:00-07:00
Authentication code: 6789

Re: Convert images to YUV NV12 format

Post 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.
michaels
Posts: 11
Joined: 2014-01-16T14:47:22-07:00
Authentication code: 6789

Re: Convert images to YUV NV12 format

Post 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?
Post Reply