How to use imageMagick to convert a ppm image to YUV422

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

How to use imageMagick to convert a ppm image to YUV422

Post by michaels »

Hi,

I am trying to use imageMagick to convert a ppm image in RGB format to YUV422 format:
I am using Bretagne1 ppm from:
http://www.openjpeg.org/index.php?menu=samples

I tried:
convert ~/Downloads/Bretagne1.ppm -colorspace YCbCr ~/Downloads/Bretagne2.ppm
convert ~/Downloads/Bretagne1.ppm -colorspace YUV ~/Downloads/Bretagne2.ppm

But when I display Bretagne2.ppm using imageMagick and go to ImageInfo, it still said it is using RGB format.

How can I fix it?

Thank you.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to use imageMagick to convert a ppm image to YUV422

Post by snibgo »

As far as I know, color pixels in PPM format always represent red, green and blue. It has no metadata to say they represent anything else. See http://en.wikipedia.org/wiki/Netpbm_format Hence IM will automatically convert from YUV to sRGB (or possibly RGB) before saving the result.

You need a file format that supports YUV. MIFF supports YUV, but is specific to IM.
snibgo's IM pages: im.snibgo.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to use imageMagick to convert a ppm image to YUV422

Post by glennrp »

The PPM format doesn't support YUV. You'll need to use a format that does, such as .yuv
(note that .yuv has no header so when you read it you need to use -size WxH to supply the
image dimensions).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use imageMagick to convert a ppm image to YUV422

Post by fmw42 »

I believe that tiff supports yuv, but with JPG compression.

I suspect that when you do -colorspace YUV image.ppm, the data is yuv, but it is not known to be, since there is no header to tell it that.

In, IM, you can read it in and specify that the colorspace is YUV, for example

convert yuv.ppm -set colorspace YUV -colorspace sRGB show:

That will convert the YUV data in the ppm from YUV to sRGB and then display it.

There was also a recent topic about YUV at viewtopic.php?f=1&t=24819
michaels
Posts: 11
Joined: 2014-01-16T14:47:22-07:00
Authentication code: 6789

Re: How to use imageMagick to convert a ppm image to YUV422

Post by michaels »

Thanks for all the answers.

If I understanding correctly, PPM image can't be in YUV 422 format, it can only in RGB format.

My next question is , can I use IM to convert PPM's RBG format to YUV422 and save Y , U, V's data into different files?
i.e. the output image is not an image but 3 planes of Y , U , V.

Thank you.
michaels
Posts: 11
Joined: 2014-01-16T14:47:22-07:00
Authentication code: 6789

Re: How to use imageMagick to convert a ppm image to YUV422

Post by michaels »

Thanks for the pointers.
I googled how to use 'interlace'.
From this http://www.chemie.fu-berlin.de/chemnet/ ... nvert.html, it seems like
it will create a interlaced GIF or progressive JPEG.

How can I save Y to a file, U to a file , V to a file?

Sorry that my question are from newbies. I am new to image processing field.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to use imageMagick to convert a ppm image to YUV422

Post by snibgo »

Code: Select all

convert Bretagne1.ppm -colorspace yuv -separate b.png
This creates b-0.png, b-1.png and b-2.png that represent Y, U and V as greyscale images. They can be glued back together to make an sRGB file with:

Code: Select all

convert b-0.png b-1.png b-2.png -set colorspace yuv -combine -colorspace sRGB b2.png
EDIT: Sorry, I mis-pasted that last command. Corrected.
snibgo's IM pages: im.snibgo.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to use imageMagick to convert a ppm image to YUV422

Post by glennrp »

michaels wrote: How can I save Y to a file, U to a file , V to a file?
The YUV format uses -interlace none to specify 4:2:2 (downsampled) and -interlace plane for 4:1:1 (not
downsampled). If you use -interlace partition, you'll get 3 files which are just what you want:
  • convert rose: -interlace partition roseip.yuv
    produces
    -rw-rw-r-- 1 glennrp glennrp 805 Jan 17 16:23 roseip.U
    -rw-rw-r-- 1 glennrp glennrp 805 Jan 17 16:23 roseip.V
    -rw-rw-r-- 1 glennrp glennrp 3220 Jan 17 16:23 roseip.Y
Note that U and V are downsampled.
Glenn
michaels
Posts: 11
Joined: 2014-01-16T14:47:22-07:00
Authentication code: 6789

Re: How to use imageMagick to convert a ppm image to YUV422

Post by michaels »

Thanks a lot for all your answers!
michaels
Posts: 11
Joined: 2014-01-16T14:47:22-07:00
Authentication code: 6789

Re: How to use imageMagick to convert a ppm image to YUV422

Post by michaels »

Thanks a lot for your answer.

I get "save Y to a file, U to a file , V to a file" works.

But how can I save Y to a file , Cb to a file, Cr to a file?

I tried:
convert ~/Downloads/Bretagne1.ppm -interlace partition -colorspace YCbCr Bretagne2.yuv

does not work.

And I tried:
convert ~/Downloads/Bretagne1.ppm -interlace partition Bretagne2.ycbcr

I see it created Bretagne2.Y , Bretagne2.Cb, Bretagne2.Cr

But Cb and Cr do not get download sample as the U, V do (when I use YUV format instead of YCbCr format).

Thank you for your help again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use imageMagick to convert a ppm image to YUV422

Post by fmw42 »

michaels wrote:Thanks for all the answers.

If I understanding correctly, PPM image can't be in YUV 422 format, it can only in RGB format.


Thank you.
I do not believe that is true. The PPM image will store any 3-channel data in it. It could be YUV channels. The issue is that there is no header to tell any other program that it is any thing but RGB channels. In IM you can read the PPM YUV image data and convert it to RGB by telling IM that the data is YUV colorspace. For example

convert yuv.ppm -set colorspace YUV -colorspace sRGB rgb.png

You should also be able to create YUV channels or YCbCr channels by something like.

convert rgb.png -colorspace YUV -separate yuv_%d.ppm (or any other format).
or
convert rgb.png -colorspace YCbCr -separate yuv_%d.ppm (or any other format).

However, you still need to tell IM or any other tool that it is YUV when reading these channels back in. You may need to specify the -interlace none to get 4:2:2 as glennrp pointed out or -sampling-factor (see http://www.imagemagick.org/script/comma ... ing-factor). I do not really know much about that.
michaels
Posts: 11
Joined: 2014-01-16T14:47:22-07:00
Authentication code: 6789

Re: How to use imageMagick to convert a ppm image to YUV422

Post by michaels »

Thanks but it does not give me what I am looking for.

I am looking for what this does, but break it down in YCbCr instead of YUV:
convert ~/Downloads/Bretagne1.ppm -interlace partition Bretagne2.yuv
This is the output from the above comment. Each Y, U, V is smaller than the original image (921638)
307200 Jan 20 10:06 Bretagne2.Y
76800 Jan 20 10:06 Bretagne2.U
76800 Jan 20 10:06 Bretagne2.V
When I tried "convert Bretagne1.ppm -colorspace YUV -separate Bretagne_yuv_%d.ppm", it gives me
921639 Jan 20 10:13 Bretagne_yuv_1.ppm
921639 Jan 20 10:13 Bretagne_yuv_0.ppm
921639 Jan 20 10:13 Bretagne_yuv_2.ppm
Each of them are the same side as big as the original image.

Basically I want what
convert ~/Downloads/Bretagne1.ppm -interlace partition Bretagne2.yuv
gives me but instead of YUV , I want YCbCr.

Thank you.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to use imageMagick to convert a ppm image to YUV422

Post by glennrp »

Do you need the downsampling of Cb and Cr channels? Writing to file.ycbcr with -interlace partition gives
you 3 files, all full size. Oh, never mind, I see you tried that already. Looking at coders/ycbcr.c it appears
that downsampling is not an option. So you'd need to resize those two files afterwards as a separate
proces (convert -resize 50% file.Cb file-downsampled.Cb; convert -resize 50% file.Cr file-downsampled.Cr).
I guess you'd need to make sure that your input file has an even number of rows and columns.
michaels
Posts: 11
Joined: 2014-01-16T14:47:22-07:00
Authentication code: 6789

Re: How to use imageMagick to convert a ppm image to YUV422

Post by michaels »

Thanks Glen for your reply.

I tried that with my sample image Bretagne1 from http://www.openjpeg.org/index.php?menu=samples.
It has even row and column.

But I get this error:
$ convert ~/Downloads/Bretagne1.ppm -interlace partition -colorspace YCbCr Bretagne2.ycbcr
$ convert -resize 50% ~/Downloads/Bretagne2.Cb ~/Downloads/Bretagne2-ds.Cb
convert: no decode delegate for this image format `/home/mike/Downloads/Bretagne2.Cb' @ error/constitute.c/ReadImage/532.
convert: missing an image filename `/home/mike/Downloads/Bretagne2-ds.Cb' @ error/convert.c/ConvertImageCommand/3011.
Post Reply