Demo entry 6677506

hsv

   

Submitted by lyk on Dec 04, 2017 at 05:05
Language: C++. Code size: 1.7 kB.

    struct RGB2HSV_f  
    {  
        typedef float channel_type;  
          
        RGB2HSV_f(int _srccn, int _blueIdx, float _hrange)  
        : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange) {}  
          
        void operator()(const float* src, float* dst, int n) const  
        {  
            int i, bidx = blueIdx, scn = srccn;  
            float hscale = hrange*(1.f/360.f);  
            n *= 3;  
          
            for( i = 0; i < n; i += 3, src += scn )  
            {  
                float b = src[bidx], g = src[1], r = src[bidx^2];  
                float h, s, v;  
                  
                float vmin, diff;  
                  
                v = vmin = r;  
                if( v < g ) v = g;  
                if( v < b ) v = b;       // v = max(b, g, r)  
                if( vmin > g ) vmin = g;  
                if( vmin > b ) vmin = b;  
                  
                diff = v - vmin;  
                s = diff/(float)(fabs(v) + FLT_EPSILON);  // s = 1 - min/max  
                diff = (float)(60./(diff + FLT_EPSILON));  
                if( v == r )  
                    h = (g - b)*diff;  
                else if( v == g )  
                    h = (b - r)*diff + 120.f;  
                else  
                    h = (r - g)*diff + 240.f;  
                  
                if( h < 0 ) h += 360.f;  // h 求值  
                  
                dst[i] = h*hscale;  
                dst[i+1] = s;  
                dst[i+2] = v;  
            }  
        }  
          
        int srccn, blueIdx;  
        float hrange;  
    };  

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).