Thread: Hue index Histogram

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    3

    Hue index Histogram

    Hai, I'm student in some college in indonesia

    I have some problem to make hue index histogram from RGB Image .Because i'm newbie in C#.

    I have problem to make code to convert from RGB to HSV color.

    Anyone can help me to make hue index histogram from RGB image ???

    Please !!!!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I couldn't find any C# functions which convert RGB to HSV, however there are lots of javascript examples on google. Here's one which I've converted to C# for you. I've not tested much but it should work.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace whatever
    {
        class Program
        {
            static void Main()
            {
                // test it
    
                HSVObject test = RGB2HSV(240, 123, 212);
                Console.WriteLine("{0} {1} {2}", test.H, test.S, test.V);
                Console.Read();
            }
    
            static HSVObject RGB2HSV(int red, int green, int blue)
            {
                decimal H, S, V;
    
                decimal r = (decimal)(red / 255.0);
                decimal g = (decimal)(green / 255.0);
                decimal b = (decimal)(blue / 255.0);
                decimal min = Math.Min(Math.Min(r, g), b);
                decimal max = Math.Max(Math.Max(r, g), b);
                decimal delta = max - min;
    
                V = max;
    
                if (delta == 0)
                {
                    H = 0;
                    S = 0;
                }
                else
                {
                    S = delta / max;
    
                    decimal dR = ((((max - r) / 6) + (delta / 2)) / delta);
                    decimal dG = ((((max - g) / 6) + (delta / 2)) / delta);
                    decimal dB = ((((max - b) / 6) + (delta / 2)) / delta);
    
                    if (r == max)
                        H = dB - dG;
                    else if (g == max)
                        H = (1 / 3) + dR - dB;
                    else
                        H = (2 / 3) + dG - dR;
    
                    if (H < 0)
                        H += 1;
    
                    if (H > 1)
                        H -= 1;
                }
    
                return new HSVObject(H, S, V);
            }
        }
    
        class HSVObject
        {
            public int H;
            public int S;
            public int V;
    
            public HSVObject(decimal H, decimal S, decimal V)
            {
                this.H = (int)Math.Round(H * 360);
                this.S = (int)Math.Round(S * 100);
                this.V = (int)Math.Round(V * 100);
            }
        }
    }

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3
    Thank For your Reply

    I will try it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  3. Reiventing the wheel (again)
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2007, 03:26 AM
  4. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  5. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 02:47 AM

Tags for this Thread