Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert to BW

Convert to BW

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vandana7
    wrote on last edited by
    #1

    How to convert grayscale image(.tif )to black & white ?

    enhzflepE 1 Reply Last reply
    0
    • V vandana7

      How to convert grayscale image(.tif )to black & white ?

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      To do that, you just need to de-saturate the image. The RGB colour-space does not easily lend itself to this operation, and the HSL colour model is far more useful here. In an entirely non optimized method, you would: Repeat loop for all pixels in source image (1) convert pixel RGB to HSL (2) drop the S value to 0 (3) convert back to RGB (4) dump the converted pixel back Here's some routines for colour-space conversion:

      // Input: h, s, v in range [0..1]
      // Outputs: r, g, b in range [0..1]
      void hsvToRgb(float h, float s, float v, float *r, float *g, float *b)
      {
      int i;
      float aa, bb, cc, f;

       if (s == 0.0) // greyscale
          \*r = \*g = \*b = v;
       else
       {
           if (h == 1.0) h = 0.0;
           h \*= 6.0;
           i = floor(h);
           f = h - (float)i;
           aa = v \* (1.0 - s);
           bb = v \* (1.0 - (s \* f));
           cc = v \* (1.0 - (s \* (1.0 - f)));
           switch(i)
           {
             case 0: \*r = v; \*g = cc; \*b = aa; break;
             case 1: \*r = bb; \*g = v; \*b = aa; break;
             case 2: \*r = aa; \*g = v; \*b = cc; break;
             case 3: \*r = aa; \*g = bb; \*b = v; break;
             case 4: \*r = cc; \*g = aa; \*b = v; break;
             case 5: \*r = v; \*g = aa; \*b = bb; break;
           }
       }
      

      }

      #define myMin(a,b) (a
      #define myMax(a,b) (a>b?a:b)
      #define noHue 0

      // Inputs: r, g, b in range [0..1]
      // Outputs: h, s, v in range [0..1]
      void rgbToHsv(float r, float g, float b, float *h, float *s, float *v)
      {
      float max = myMax(r, myMax(g, b)), min = myMin(r, myMin(g, b));
      float delta = max - min;

       \*v = max;
       if (max != 0.0)
          \*s = delta / max;
       else 
            \*s = 0.0;
       if (\*s == 0.0) \*h = noHue;
       else
       {
           if (r == max)
              \*h = (g - b) / delta;
           else if (g == max)
              \*h = 2 + (b - r) / delta;
           else if (b == max)
              \*h = 4 + (r - g) / delta;
           \*h \*= 60.0;
           if (\*h < 0) \*h += 360.0;
           \*h /= 360.0;
       }
      

      }

      I'll leave it to you to make them work with RGB values in the range [0..255]

      N 1 Reply Last reply
      0
      • enhzflepE enhzflep

        To do that, you just need to de-saturate the image. The RGB colour-space does not easily lend itself to this operation, and the HSL colour model is far more useful here. In an entirely non optimized method, you would: Repeat loop for all pixels in source image (1) convert pixel RGB to HSL (2) drop the S value to 0 (3) convert back to RGB (4) dump the converted pixel back Here's some routines for colour-space conversion:

        // Input: h, s, v in range [0..1]
        // Outputs: r, g, b in range [0..1]
        void hsvToRgb(float h, float s, float v, float *r, float *g, float *b)
        {
        int i;
        float aa, bb, cc, f;

         if (s == 0.0) // greyscale
            \*r = \*g = \*b = v;
         else
         {
             if (h == 1.0) h = 0.0;
             h \*= 6.0;
             i = floor(h);
             f = h - (float)i;
             aa = v \* (1.0 - s);
             bb = v \* (1.0 - (s \* f));
             cc = v \* (1.0 - (s \* (1.0 - f)));
             switch(i)
             {
               case 0: \*r = v; \*g = cc; \*b = aa; break;
               case 1: \*r = bb; \*g = v; \*b = aa; break;
               case 2: \*r = aa; \*g = v; \*b = cc; break;
               case 3: \*r = aa; \*g = bb; \*b = v; break;
               case 4: \*r = cc; \*g = aa; \*b = v; break;
               case 5: \*r = v; \*g = aa; \*b = bb; break;
             }
         }
        

        }

        #define myMin(a,b) (a
        #define myMax(a,b) (a>b?a:b)
        #define noHue 0

        // Inputs: r, g, b in range [0..1]
        // Outputs: h, s, v in range [0..1]
        void rgbToHsv(float r, float g, float b, float *h, float *s, float *v)
        {
        float max = myMax(r, myMax(g, b)), min = myMin(r, myMin(g, b));
        float delta = max - min;

         \*v = max;
         if (max != 0.0)
            \*s = delta / max;
         else 
              \*s = 0.0;
         if (\*s == 0.0) \*h = noHue;
         else
         {
             if (r == max)
                \*h = (g - b) / delta;
             else if (g == max)
                \*h = 2 + (b - r) / delta;
             else if (b == max)
                \*h = 4 + (r - g) / delta;
             \*h \*= 60.0;
             if (\*h < 0) \*h += 360.0;
             \*h /= 360.0;
         }
        

        }

        I'll leave it to you to make them work with RGB values in the range [0..255]

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        enhzflep wrote:

        Here's some routines for colour-space conversion:

        These functions does the same, right? ColorRGBToHLS ColorHLSToRGB

        Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

        enhzflepE 1 Reply Last reply
        0
        • N Nibu babu thomas

          enhzflep wrote:

          Here's some routines for colour-space conversion:

          These functions does the same, right? ColorRGBToHLS ColorHLSToRGB

          Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          enhzflepE Offline
          enhzflepE Offline
          enhzflep
          wrote on last edited by
          #4

          :-\ After looking at: http://msdn.microsoft.com/en-us/library/bb773851(VS.85).aspx[^] It would certainly appear so. In fact, they'd be even better, since they deal with WORDs instead of floats. That'll teach me for using a 11 year old API help file.. Thanks for the better solution.

          N 1 Reply Last reply
          0
          • enhzflepE enhzflep

            :-\ After looking at: http://msdn.microsoft.com/en-us/library/bb773851(VS.85).aspx[^] It would certainly appear so. In fact, they'd be even better, since they deal with WORDs instead of floats. That'll teach me for using a 11 year old API help file.. Thanks for the better solution.

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #5

            enhzflep wrote:

            That'll teach me for using a 11 year old API help file..

            :)

            Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups