本系列为darknet源码解析,由于在image.c中涉及到图像的RGB,YUV,HSV格式,在本文我们将image.c中涉及到的rgb_to_hsv()函数以及hsv_to_rgb()函数进行解析.
RGB格式转为HSV格式
如果max=0
如果max!=0
遗漏一些公式,需要查找网络补齐
对应源码如下:
void rgb_to_hsv(image im)
{
assert(im.c == 3);
int i, j;
float r, g, b;
float h, s, 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);
float max = three_way_max(r,g,b);//获取rgb最大值
float min = three_way_min(r,g,b);
float delta = max - min;
v = max;
if(max == 0){
s = 0;
h = 0;
}else{
s = delta/max;
if(r == max){
h = (g - b) / delta;
} else if (g == max) {
h = 2 + (b - r) / delta;
} else {
h = 4 + (r - g) / delta;
}
if (h < 0) h += 6;
h = h/6.;
}
set_pixel(im, i, j, 0, h);//指定对应位置的对应通道的像素值
set_pixel(im, i, j, 1, s);
set_pixel(im, i, j, 2, v);
}
}
}
HSV格式转为RGB格式
如果S==0:
如果S != 0:
对应源码如下:
void hsv_to_rgb(image im)
{
assert(im.c == 3);
int i, j;
float r, g, b;
float h, s, v;
float f, p, q, t;
for(j = 0; j < im.h; ++j){
for(i = 0; i < im.w; ++i){
h = 6 * get_pixel(im, i , j, 0);
s = get_pixel(im, i , j, 1);
v = get_pixel(im, i , j, 2);
if (s == 0) {
r = g = b = v;
} else {
int index = floor(h);
f = h - index;
p = v*(1-s);
q = v*(1-s*f);
t = v*(1-s*(1-f));
if(index == 0){
r = v; g = t; b = p;
} else if(index == 1){
r = q; g = v; b = p;
} else if(index == 2){
r = p; g = v; b = t;
} else if(index == 3){
r = p; g = q; b = v;
} else if(index == 4){
r = t; g = p; b = v;
} else {
r = v; g = p; b = q;
}
}
set_pixel(im, i, j, 0, r);
set_pixel(im, i, j, 1, g);
set_pixel(im, i, j, 2, b);
}
}
}
完.
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/caicaiatnbu/article/details/104070129