This series darknet parse source code, as it relates to the RGB image, YUV, HSV format image.c, we will resolve image.c rgb_to_yuv involved in () function and yuv_to_rgb () function herein.
- RGB format converted to YUV format
The corresponding function is as follows:
void rgb_to_yuv(image im)
{
assert(im.c == 3);
int i, j;
float r, g, b;
float y, u, v;
for(j = 0; j < im.h; ++j){
for(i = 0; i < im.w; ++i){
r = get_pixel(im, i , j, 0);
g = get_pixel(im, i , j, 1);
b = get_pixel(im, i , j, 2);
y = .299*r + .587*g + .114*b;
u = -.14713*r + -.28886*g + .436*b;
v = .615*r + -.51499*g + -.10001*b;
set_pixel(im, i, j, 0, y);
set_pixel(im, i, j, 1, u);
set_pixel(im, i, j, 2, v);
}
}
}
- Converted RGB format YUV format
void yuv_to_rgb(image im)
{
assert(im.c == 3);
int i, j;
float r, g, b;
float y, u, v;
for(j = 0; j < im.h; ++j){
for(i = 0; i < im.w; ++i){
y = get_pixel(im, i , j, 0);
u = get_pixel(im, i , j, 1);
v = get_pixel(im, i , j, 2);
r = y + 1.13983*v;
g = y + -.39465*u + -.58060*v;
b = y + 2.03211*u;
set_pixel(im, i, j, 0, r);
set_pixel(im, i, j, 1, g);
set_pixel(im, i, j, 2, b);
}
}
}
[References]