Implement hsi and RGB conversion using Matlab

Source: Internet
Author: User
% Convert RGB image to HSI image % display HSI image % histogram equalization HSI image % convert HSI image back to RGB image function rgbtohsi (x) F = imread (X ); F = im2double (f); r = f (:,:, 1); G = f (:,:, 2); B = f (:,:, 3 ); th = ACOs (0.5 * (r-g) + (r-B ))). /(SQRT (r-g ). ^ 2 + (r-B ). * (G-B) + EPS); H = th; H (B> G) = 2 * pi-H (B> G ); H = H/(2 * PI); S = 1-3. * (min (R, G), B )). /(R + G + B + EPS); I = (R + G + B)/3; HSI = CAT (3, H, S, I ); he = H * 2 * PI; He = histeq (HE); He = He/(2 * PI); Se = histeq (s); Ie = histeq (I ); choice = input ('1: Convert RGB to HSI \ N2: Show HSI image \ N3: Convert HSI to RGB image \ N4: Tone balanced \ N5: Saturation balanced \ N6: brightness balance \ N7: HSI balance \ N input your choice: '); Switch choicecase 1 figure, imshow (F), title ('rgb image'); figure, imshow (HSI), title ('hsi image'); Case 2 figure, imshow (F), title ('rgb image'); figure, imshow (H ), title ('hue image'); figure, imshow (s), title ('saturation image'); figure, imshow (I), title ('brightness image '); case 3C = hsitorgb (HSI); figure, imshow (HSI), title ('hsi image'); figure, imshow (C), title ('rgb image '); case 4rv = CAT (3, he, s, I); C = hsitorgb (RV); figure, imshow (HSI), title ('hsi image'); figure, imshow (F), title ('rgb image'); figure, imshow (C), title ('rgb image-Hue equalized'); Case 5rv = CAT (3, H, se, I); C = hsitorgb (RV); figure, imshow (HSI), title ('hsi image'); figure, imshow (f ), title ('rgb image'); figure, imshow (C), title ('rgb saturation balanced '); Case 6rv = CAT (3, H, S, ie ); C = hsitorgb (RV); figure, imshow (HSI), title ('hsi image'); figure, imshow (F), title ('rgb image'); figure, imshow (C), title ('rgb brightness balance'); Case 7rv = CAT (3, he, Se, ie); C = hsitorgb (RV); figure, imshow (HSI), title ('hsi image'); figure, imshow (F), title ('rgb image'); figure, imshow (c ), title ('rgb HSI balance'); otherwisedisplay ('select error'); endend % function, convert HSI image to RGB image Function C = hsitorgb (HSI) HV = HSI (:,:, 1) * 2 * PI; Sv = HSI (:,:, 2); IV = HSI (:,:, 3 ); R = zeros (SIZE (HV); G = zeros (SIZE (HV); B = zeros (SIZE (HV )); % RG sectorID = find (0 <= HV) & (HV <2 * PI/3); B (ID) = IV (ID ). * (1-sv (ID); R (ID) = IV (ID ). * (1 + SV (ID ). * Cos (HV (ID )). /cos (PI/3-hv (ID); G (ID) = 3 * IV (ID)-(R (ID) + B (ID )); % BG sectorID = find (2 * PI/3 <= HV) & (HV <4 * PI/3); R (ID) = IV (ID ). * (1-sv (ID); G (ID) = IV (ID ). * (1 + SV (ID ). * Cos (HV (ID)-2 * PI/3 ). /cos (Pi-HV (ID); B (ID) = 3 * IV (ID)-(R (ID) + g (ID )); % BR sectorID = find (4 * PI/3 <= HV) & (HV <2 * PI); G (ID) = IV (ID ). * (1-sv (ID); B (ID) = IV (ID ). * (1 + SV (ID ). * Cos (HV (ID)-4 * PI/3 ). /cos (5 * PI/3-hv (ID); R (ID) = 3 * IV (ID)-(G (ID) + B (ID )); C = CAT (3, R, G, B); C = max (min (C, 1), 0); End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.