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 color image to grayscale image ?

convert color image to grayscale image ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
12 Posts 5 Posters 3 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.
  • G Offline
    G Offline
    gentleguy
    wrote on last edited by
    #1

    dear all howt to convert color image to grayscale image in c++? anyone has such code? thanks a lot

    Li Zhiyuan

    I CPalliniC S 3 Replies Last reply
    0
    • G gentleguy

      dear all howt to convert color image to grayscale image in c++? anyone has such code? thanks a lot

      Li Zhiyuan

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      There are several articles on codeproject about turning RGB colours into HSL (hue, saturation, luminence) colours - and to get grey scale, you can just extract the luminence component for your needs. Or search for rgb and grayscale on codeproject for some other mentions of your problem; http://www.google.com/search?hl=en&q=rgb+grayscale+%2Bsite%3Acodeproject.com&btnG=Search[^] (Some people may spell it greyscale too. Good luck, Iain.

      Iain Clarke appearing in spite of being begged not to by CPallini.

      1 Reply Last reply
      0
      • G gentleguy

        dear all howt to convert color image to grayscale image in c++? anyone has such code? thanks a lot

        Li Zhiyuan

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Can't you simply average the color components, i.e.

        COLORREF color, gray;
        //...
        BYTE avg = (GetRValue(color) + GetGValue(color) + GetBValue(color))/3;
        gray = RGB(avg, avg, avg);

        ?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        In testa che avete, signor di Ceprano?

        R 1 Reply Last reply
        0
        • CPalliniC CPallini

          Can't you simply average the color components, i.e.

          COLORREF color, gray;
          //...
          BYTE avg = (GetRValue(color) + GetGValue(color) + GetBValue(color))/3;
          gray = RGB(avg, avg, avg);

          ?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #4

          then (R = 100, G = 0, B = 0) and (R= 0, G =100, B = 0) and (R=0, g= 0, b= 100) all gives the same value, that is if the image has 3 region each filled with Red, Green, Blue respectively, then the whole image has same gray value (can't identify the regions)

          modified on Thursday, February 21, 2008 5:06 AM

          CPalliniC 1 Reply Last reply
          0
          • R Rajkumar R

            then (R = 100, G = 0, B = 0) and (R= 0, G =100, B = 0) and (R=0, g= 0, b= 100) all gives the same value, that is if the image has 3 region each filled with Red, Green, Blue respectively, then the whole image has same gray value (can't identify the regions)

            modified on Thursday, February 21, 2008 5:06 AM

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Color to Gray space conversion is lossy by definition: RGB Color space allows 2^24 different values for color image pixels, while the constraint RGB(x,x,x) implies 2^8 different values for gray pixels. Thus we have a mapping of a 2^24 set to 2^8 set, i.e. a lossy mapping. Average is a very simple method to obtain a grayscale image, maybe also a naive (though working) one, but the lossy nature of the conversion is unavoidable. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            In testa che avete, signor di Ceprano?

            R 1 Reply Last reply
            0
            • CPalliniC CPallini

              Color to Gray space conversion is lossy by definition: RGB Color space allows 2^24 different values for color image pixels, while the constraint RGB(x,x,x) implies 2^8 different values for gray pixels. Thus we have a mapping of a 2^24 set to 2^8 set, i.e. a lossy mapping. Average is a very simple method to obtain a grayscale image, maybe also a naive (though working) one, but the lossy nature of the conversion is unavoidable. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              I know that. but averaging u meantion cannot differentiate even very distinct colours RED, GREEN, BLUE. I personally don't believe that averaging is a solution. eventhough the actual solution is some sort of weighted average :)

              modified on Thursday, February 21, 2008 5:52 AM

              CPalliniC 1 Reply Last reply
              0
              • R Rajkumar R

                I know that. but averaging u meantion cannot differentiate even very distinct colours RED, GREEN, BLUE. I personally don't believe that averaging is a solution. eventhough the actual solution is some sort of weighted average :)

                modified on Thursday, February 21, 2008 5:52 AM

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Rajkumar R wrote:

                but averaging u meantion cannot differentiate even very distinct colours RED, GREEN, BLUE.

                The above is obviously true.

                Rajkumar R wrote:

                I personally don't believe averaging is a solution.

                You're wrong. It is a quite acceptable solution, because (as I've already written) is a function mapping the color space set to the the grayspace one. It is also a reasonable mapping. Of course there are many possible solutions and the better one maybe application-dependent. Now a simple question: are you able to distinguish a full red from a full green in a B/W movie? Does it matter to the Director? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                In testa che avete, signor di Ceprano?

                R 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Rajkumar R wrote:

                  but averaging u meantion cannot differentiate even very distinct colours RED, GREEN, BLUE.

                  The above is obviously true.

                  Rajkumar R wrote:

                  I personally don't believe averaging is a solution.

                  You're wrong. It is a quite acceptable solution, because (as I've already written) is a function mapping the color space set to the the grayspace one. It is also a reasonable mapping. Of course there are many possible solutions and the better one maybe application-dependent. Now a simple question: are you able to distinguish a full red from a full green in a B/W movie? Does it matter to the Director? :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  R Offline
                  R Offline
                  Rajkumar R
                  wrote on last edited by
                  #8

                  CPallini wrote:

                  distinguish a full red from a full green in a B/W movie? Does it matter to the Director?

                  lol !!! :laugh: , instead he can put black screen, save light energy. :)

                  G 1 Reply Last reply
                  0
                  • R Rajkumar R

                    CPallini wrote:

                    distinguish a full red from a full green in a B/W movie? Does it matter to the Director?

                    lol !!! :laugh: , instead he can put black screen, save light energy. :)

                    G Offline
                    G Offline
                    gentleguy
                    wrote on last edited by
                    #9

                    thanks all friends how to blur or mask a color image, actually i load a jpeg image to computer, now i would like to smooth via Gaussion function, but one of friend said i have to convert color image to grayscale image and then i can blur, so now i don't how to do? anyone has such code or suggestion? thanks a lot

                    Li Zhiyuan

                    R 1 Reply Last reply
                    0
                    • G gentleguy

                      dear all howt to convert color image to grayscale image in c++? anyone has such code? thanks a lot

                      Li Zhiyuan

                      S Offline
                      S Offline
                      swarup
                      wrote on last edited by
                      #10

                      search for "MakeInactiveDemo" in codeproject he has a function to convert image to grayscale

                      G 1 Reply Last reply
                      0
                      • G gentleguy

                        thanks all friends how to blur or mask a color image, actually i load a jpeg image to computer, now i would like to smooth via Gaussion function, but one of friend said i have to convert color image to grayscale image and then i can blur, so now i don't how to do? anyone has such code or suggestion? thanks a lot

                        Li Zhiyuan

                        R Offline
                        R Offline
                        Rajkumar R
                        wrote on last edited by
                        #11

                        Image Processing for Dummies with C# and GDI+ Part 2 - Convolution Filters [^] although written in C#, it is easy to understand the gaussian blur implementation, i think it is not converting to greyscale before blur.

                        1 Reply Last reply
                        0
                        • S swarup

                          search for "MakeInactiveDemo" in codeproject he has a function to convert image to grayscale

                          G Offline
                          G Offline
                          gentleguy
                          wrote on last edited by
                          #12

                          thanks friend anyone know how to convolution of image? i have a little problem of concept, how to multiply two arrays? and how to put new value in new array, thanks a lot, urgent

                          Li Zhiyuan

                          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