Library/opencv
RGB to HSV 색상변환
퓨림노
2021. 1. 26. 15:53
RGB to HSV 색상변환
<코드>
#define MATH_MIN3(x,y,z) ( (y) <= (z) ? ((x) <= (y) ? (x) : (y)) : ((x) <= (z) ? (x) : (z)) ) #define MATH_MAX3(x,y,z) ( (y) >= (z) ? ((x) >= (y) ? (x) : (y)) : ((x) >= (z) ? (x) : (z)) ) struct hsv_color { unsigned char h; // Hue: 0 ~ 255 (red:0, gree: 85, blue: 171) unsigned char s; // Saturation: 0 ~ 255 unsigned char v; // Value: 0 ~ 255 }; hsv_color RGB2HSV(unsigned char r, unsigned char g, unsigned char b) { unsigned char rgb_min, rgb_max; rgb_min = MATH_MIN3(b, g, r); rgb_max = MATH_MAX3(b, g, r); hsv_color hsv; hsv.v = rgb_max; if (hsv.v == 0) { hsv.h = hsv.s = 0; return hsv; } hsv.s = 255*(rgb_max - rgb_min)/hsv.v; if (hsv.s == 0) { hsv.h = 0; return hsv; } if (rgb_max == r) { hsv.h = 0 + 43*(g - b)/(rgb_max - rgb_min); } else if (rgb_max == g) { hsv.h = 85 + 43*(b - r)/(rgb_max - rgb_min); } else /* rgb_max == rgb.b */ { hsv.h = 171 + 43*(r - g)/(rgb_max - rgb_min); } return hsv; }
Reference
블로그1 http://zeal74.tistory.com/1180
WIKI : http://ko.wikipedia.org/wiki/HSV_%EC%83%89_%EA%B3%B5%EA%B0%84
블로그3 : http://darkpgmr.tistory.com/66