# Divide BMP to YUV channels

**URL:** <https://www.ruby-forum.com/t/divide-bmp-to-yuv-channels/185892>\
**Category:** Ruby\
**Created:** [March 28, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/divide-bmp-to-yuv-channels/185892 "2010-03-28T18:02:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pearce89](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@pearce89](https://www.ruby-forum.com/u/pearce89)\
**Post date:** [March 28, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/divide-bmp-to-yuv-channels/185892/1 "2010-03-28T18:02:23Z")

</div>

Hey guys!  
I have an univercity exersise:  
I’m trying to get YUV channels from BMP picture, and output it like 3  
grey images.  
I know the proportion:  
RGB to YUV  
Y = (0.257 \* R) + (0.504 \* G) + (0.098 \* B) + 16  
V = (0.439 \* R) - (0.368 \* G) - (0.071 \* B) + 128  
U = -(0.148 \* R) - (0.291 \* G) + (0.439 \* B) + 128

But don’t know how to get it works with Ruby.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![pearce89](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@pearce89](https://www.ruby-forum.com/u/pearce89)\
**Post date:** [March 28, 2010, 7:56pm UTC](https://www.ruby-forum.com/t/divide-bmp-to-yuv-channels/185892/2 "2010-03-28T19:56:33Z")

</div>

Hello,

I dont think the ruby-ml, or the ruby-forums, are an exersise solving  
machine…

But i can give you the following overall picture of your programm  
(more pseudocode than ruby)

==== BEGIN

image = open\_source\_image

y\_image = open\_new\_image  
u\_image = open\_new\_image  
v\_image = open\_new\_image

for pixel\_in\_image in image do  
y,u,v = calculate\_yuv\_for\_pixel(pixel)  
write\_to\_image(y\_image,y)  
write\_to\_image(u\_image,u)  
write\_to\_image(v\_image,v)  
end

==== END

greetings,

Markus

---

<div class="post-metadata">

**Author:** ![pearce89](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@pearce89](https://www.ruby-forum.com/u/pearce89)\
**Post date:** [March 28, 2010, 8:01pm UTC](https://www.ruby-forum.com/t/divide-bmp-to-yuv-channels/185892/3 "2010-03-28T20:01:50Z")

</div>

Hola again,

Im reposting the pseudocode, since the first one leads to  
uninitialized local variable ‘pixel’, renamed it to ‘pixel\_in\_image’

image = open\_source\_image

y\_image = open\_new\_image  
u\_image = open\_new\_image  
v\_image = open\_new\_image

for pixel\_in\_image in image do  
y,u,v = calculate\_yuv\_for\_pixel(pixel\_in\_image)  
write\_to\_image(y\_image,y)  
write\_to\_image(u\_image,u)  
write\_to\_image(v\_image,v)  
end
