C++编程完成图像在RGB、HSV、HSL颜色空间的转换程序。
时间: 2024-01-01 22:04:33 浏览: 103
C++实现图像处理RGB与HSV相互转换代码
以下是一个简单的C语言程序,用于将RGB颜色空间的图像转换为HSV和HSL颜色空间:
```c
#include <stdio.h>
void rgb_to_hsv(float r, float g, float b, float* h, float* s, float* v) {
float min, max, delta;
min = r < g ? r : g;
min = min < b ? min : b;
max = r > g ? r : g;
max = max > b ? max : b;
*v = max; // v
delta = max - min;
if (max != 0)
*s = delta / max; // s
else {
// r = g = b = 0 // s = 0, v is undefined
*s = 0;
*h = -1;
return;
}
if (r == max)
*h = (g - b) / delta; // between yellow & magenta
else if (g == max)
*h = 2 + (b - r) / delta; // between cyan & yellow
else
*h = 4 + (r - g) / delta; // between magenta & cyan
*h *= 60; // degrees
if (*h < 0)
*h += 360;
}
void rgb_to_hsl(float r, float g, float b, float* h, float* s, float* l) {
float min, max, delta;
min = r < g ? r : g;
min = min < b ? min : b;
max = r > g ? r : g;
max = max > b ? max : b;
*l = (max + min) / 2;
if (max == min) {
*s = 0;
*h = 0; // achromatic
}
else {
delta = max - min;
*s = *l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
if (r == max)
*h = (g - b) / delta; // between yellow & magenta
else if (g == max)
*h = 2 + (b - r) / delta; // between cyan & yellow
else
*h = 4 + (r - g) / delta; // between magenta & cyan
*h *= 60; // degrees
if (*h < 0)
*h += 360;
}
}
int main() {
float r, g, b;
float h, s, v, l;
printf("Enter RGB values (0-255): ");
scanf("%f %f %f", &r, &g, &b);
rgb_to_hsv(r / 255, g / 255, b / 255, &h, &s, &v);
printf("HSV values: %f %f %f\n", h, s, v);
rgb_to_hsl(r / 255, g / 255, b / 255, &h, &s, &l);
printf("HSL values: %f %f %f\n", h, s, l);
return 0;
}
```
该程序将RGB颜色值作为输入,并计算相应的HSV和HSL值。可以使用该程序将RGB颜色空间的图像转换为HSV和HSL颜色空间的图像。
阅读全文