RGB-HSI,HSI-RGB

HSI----->>>RGB:


  
  
  1. function rgb = hsi2rgb(hsi)
  2. %HSI2RGB Converts an HSI image to RGB.
  3. % RGB = HSI2RGB(HSI) converts an HSI image to RGB, where HSI is
  4. % assumed to be of class double with:
  5. % hsi(:, :, 1) = hue image, assumed to be in the range
  6. % [ 0, 1] by having been divided by 2*pi.
  7. % hsi(:, :, 2) = saturation image, in the range [ 0, 1].
  8. % hsi(:, :, 3) = intensity image, in the range [ 0, 1].
  9. %
  10. % The components of the output image are:
  11. % rgb(:, :, 1) = red.
  12. % rgb(:, :, 2) = green.
  13. % rgb(:, :, 3) = blue.
  14. % Copyright 2002 -2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  15. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  16. % $Revision: 1.5 $ $ Date: 2003/ 10/ 13 01: 01: 06 $
  17. % Extract the individual HSI component images.
  18. H = hsi(:, :, 1) * 2 * pi;
  19. S = hsi(:, :, 2);
  20. I = hsi(:, :, 3);
  21. % Implement the conversion equations.
  22. R = zeros(size(hsi, 1), size(hsi, 2));
  23. G = zeros(size(hsi, 1), size(hsi, 2));
  24. B = zeros(size(hsi, 1), size(hsi, 2));
  25. % RG sector ( 0 <= H < 2*pi/ 3).
  26. idx = find( ( 0 <= H) & (H < 2*pi/ 3));
  27. B(idx) = I(idx) .* ( 1 - S(idx));
  28. R(idx) = I(idx) .* ( 1 + S(idx) .* cos(H(idx)) ./ cos(pi/ 3 - H(idx)));
  29. G(idx) = 3*I(idx) - (R(idx) + B(idx));
  30. % BG sector ( 2*pi/ 3 <= H < 4*pi/ 3).
  31. idx = find( ( 2*pi/ 3 <= H) & (H < 4*pi/ 3) );
  32. R(idx) = I(idx) .* ( 1 - S(idx));
  33. G(idx) = I(idx) .* ( 1 + S(idx) .* cos(H(idx) - 2*pi/ 3) ./ cos(pi - H(idx)));
  34. B(idx) = 3*I(idx) - (R(idx) + G(idx));
  35. % BR sector.
  36. idx = find( ( 4*pi/ 3 <= H) & (H <= 2*pi));
  37. G(idx) = I(idx) .* ( 1 - S(idx));
  38. B(idx) = I(idx) .* ( 1 + S(idx) .* cos(H(idx) - 4*pi/ 3) ./ cos( 5*pi/ 3 - H(idx)));
  39. R(idx) = 3*I(idx) - (G(idx) + B(idx));
  40. % Combine all three results into an RGB image. Clip to [ 0, 1] to
  41. % compensate for floating-point arithmetic rounding effects.
  42. rgb = cat( 3, R, G, B);
  43. rgb = max(min( rgb, 1), 0);


RGB---->>HSI


  
  
  1. function hsi = rgb2hsi(rgb)
  2. %RGB2HSI Converts an RGB image to HSI.
  3. % HSI = RGB2HSI(RGB) converts an RGB image to HSI. The input image
  4. % is assumed to be of size M- by-N- by -3, where the third dimension
  5. % accounts for three image planes: red, green, and blue, in that
  6. % order. If all RGB component images are equal, the HSI conversion
  7. % is undefined. The input image can be of class double ( with values
  8. % in the range [ 0, 1]), uint8, or uint16.
  9. %
  10. % The output image, HSI, is of class double, where:
  11. % hsi(:, :, 1) = hue image normalized to the range [ 0, 1] by
  12. % dividing all angle values by 2*pi.
  13. % hsi(:, :, 2) = saturation image, in the range [ 0, 1].
  14. % hsi(:, :, 3) = intensity image, in the range [ 0, 1].
  15. % Copyright 2002 -2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  16. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  17. % $Revision: 1.4 $ $ Date: 2003/ 09/ 29 15: 21: 54 $
  18. % Extract the individual component immages.
  19. rgb = im2double(rgb);
  20. r = rgb(:, :, 1);
  21. g = rgb(:, :, 2);
  22. b = rgb(:, :, 3);
  23. % Implement the conversion equations.
  24. num = 0.5*((r - g) + (r - b));
  25. den = sqrt((r - g).^ 2 + (r - b).*(g - b));
  26. theta = acos(num./(den + eps));
  27. H = theta;
  28. H(b > g) = 2*pi - H(b > g);
  29. H = H/( 2*pi);
  30. num = min(min(r, g), b);
  31. den = r + g + b;
  32. den(den == 0) = eps;
  33. S = 1 - 3.* num./den;
  34. H(S == 0) = 0;
  35. I = (r + g + b)/ 3;
  36. % Combine all three results into an hsi image.
  37. hsi = cat( 3, H, S, I);


 

猜你喜欢

转载自blog.csdn.net/qq_32790593/article/details/83858133
HSI