Coloured YUV / YCbCr / YPbPr chroma components

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?".
Locked
into_space
Posts: 4
Joined: 2011-11-19T15:55:13-07:00
Authentication code: 8675308

Coloured YUV / YCbCr / YPbPr chroma components

Post by into_space »

Hi,

With the following code I was able to convert a RGB image and split it into its YUV components. But I don not want the coloured version of the croma components and not the grayscale version.

Code: Select all

convert rgb.png -colorspace YUV -sampling-factor 4:2:2 -separate YUV.png
Examples for what I want to do can be found here
http://en.wikipedia.org/wiki/File:Barn-yuv.png
http://en.wikipedia.org/wiki/File:Barns ... ration.jpg

How can this be done with IM for YUV / YCbCr / YPbPr?

Thanks for your help.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Coloured YUV / YCbCr / YPbPr chroma components

Post by fmw42 »

what is it you actually want? when you separate channels, each channel will be grayscale, that is Y,U, and V each will be grayscale. Do you only want the U,V grayscale versions? Do you want all 3 grayscale version, or do you want somehow to false colorize each channel?

This works fine for me to get the grayscale U,V channels only.


convert image.png -colorspace YUV -sampling-factor 4:2:2 -channel gb -separate +channel image_yuv_%d.png

into_space
Posts: 4
Joined: 2011-11-19T15:55:13-07:00
Authentication code: 8675308

Re: Coloured YUV / YCbCr / YPbPr chroma components

Post by into_space »

fmw42 wrote:do you want somehow to false colorize each channel?
Yes, that is what I want. The Y component is a grayscale image. That ist fine. But for the two croma components I want the coloured version, as in the links I provided.
In separated RGB-channels this can be done with -colorize. But what to do in YUV / YCbCr / YPbPr, as here the colour has to be calculated?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Coloured YUV / YCbCr / YPbPr chroma components

Post by fmw42 »

Once they are grayscale, you can colorize by any means IM has available. You need to find out how the examples you saw were colorized or combined to convert from grayscale to color.

into_space
Posts: 4
Joined: 2011-11-19T15:55:13-07:00
Authentication code: 8675308

Re: Coloured YUV / YCbCr / YPbPr chroma components

Post by into_space »

As you can see at the Wikipedia page http://en.wikipedia.org/wiki/YUV the "colour" is calculated from the RGB. If I want to colorize I need to know the RGB values of each pixel, calculate the UV channels and write to a file. JPEG uses a similar formula. As IM "knows" how it works, I thougth there will be an easy way to get those "coloured" UV channels.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Coloured YUV / YCbCr / YPbPr chroma components

Post by fmw42 »

RGB converted to YUV does use those formulae. But each channel ends up being grayscale and not colored. They only show as a colored image togther when displayed as if RGB. That is combine the individual grayscale channels into a 3-channel image and display as if RGB. But each channel Y,U,V are each grayscale. So I don't see anything in that reference that tells me how they colored the U and V channels by themselves.

There is some code on how the images were generated if one clicks on the picture at http://en.wikipedia.org/wiki/File:Barn-yuv.png, and I think I see what they have done. For each of U, V, they set one channel to 0, one to U and one to the negate of U (255-U). Then combine those 3 channels into a color (RGB) image. Similarly for V but a different order. However, this is still artificially coloring the U and V channels.

Nevetheless try the following:

barn.png
Image

# first line -- convert to and separate Y,U,V
# second line -- copy Y
# third line -- copy U and make all black
# fourth line -- copy U and invert (negate)
# fifth line -- copy U
# sixth line -- combine lines 3,4,5 as RGB colored version of U
# seventh line -- copy V
# eighth line -- copy V and negate (invert)
# ninth line -- copy V and make all black
# tenth line combine lines 7,8,9 as RGB colored version of V
# eleventh line -- delete unneeded copies and append the Y, colored U and colored V into output image
convert barn.png -colorspace YUV -sampling-factor 4:2:2 -separate \
\( -clone 0 \) \
\( -clone 1 -fill black -colorize 100% \) \
\( -clone 1 -negate \) \
\( -clone 1 \) \
\( -clone 4 -clone 5 -clone 6 -combine\) \
\( -clone 2 \) \
\( -clone 2 -negate \) \
\( -clone 2 -fill black -colorize 100% \) \
\( -clone 8 -clone 9 -clone 10 -combine \) \
-delete 0-2,4-6,8-10 -append \
barn_yuv_append.png

Image


The key lines of the code in the reference are:

convert to YUV, then make the colored versions of U and V and keep Y as grayscale (but 3 channels)

$yuv_img->setPixel($x, $height+$y, cached_allocate($yuv_img, $Y, $Y, $Y));
$yuv_img->setPixel($x, 2*$height+$y, cached_allocate($yuv_img, 0, 255-$U, $U));
$yuv_img->setPixel($x, 3*$height+$y, cached_allocate($yuv_img, $V, 255-$V, 0));
Last edited by fmw42 on 2011-11-20T19:30:16-07:00, edited 5 times in total.

into_space
Posts: 4
Joined: 2011-11-19T15:55:13-07:00
Authentication code: 8675308

Re: Coloured YUV / YCbCr / YPbPr chroma components

Post by into_space »

Impressive. I think I have to learn a lot more to understand color spaces as well as IM. Many thanks for your help.

Locked