Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / MFC

Converting Color to Grayscale Using ATL::CImage

Rate me:
Please Sign up or sign in to vote.
3.07/5 (7 votes)
5 Nov 2009CPOL 37.6K   1.9K   9   6
Demonstrates how the ATL::CImage class can be used to do color-to-grayscale conversion on images.
Image 1

Introduction

This is just a simple MFC application that demonstrates how you can use the ATL::CImage class to do color-to-grayscale conversion.

Background

Conversion is done by:

  1. obtaining the red, green, and blue values in each pixel's color information,
  2. adding together 30% of the red value, 59% of the green value, and 11% of the blue value,
  3. using the sum obtained in step 2 as the new red, green, and blue values.

Using the Code

There are two implementations for doing the conversion:

  • MakeGrayscale_Slow() --> simple but slow
  • MakeGrayscale_Fast() --> a little complicated but fast

Points of Interest

MakeGrayscale_Slow() and MakeGrayscale_Fast() produce different grayscales for the same color image. I have no idea why this happens. Anyone who knows?

History

  • 2009/11/06 - Uploaded this article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Philippines Philippines
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Mahdi Nejadsahebi1-Feb-14 23:07
Mahdi Nejadsahebi1-Feb-14 23:07 
GeneralMy vote of 1 Pin
SteveScanlon9-Nov-09 9:24
SteveScanlon9-Nov-09 9:24 
GeneralMy vote of 2 Pin
Nick Gorlov9-Nov-09 0:36
Nick Gorlov9-Nov-09 0:36 
General"Anyone who knows?" Pin
Axel Rietschin6-Nov-09 15:32
professionalAxel Rietschin6-Nov-09 15:32 
GeneralSome comments [modified] PinPopular
mluri6-Nov-09 0:08
mluri6-Nov-09 0:08 
GeneralRe: Some comments Pin
Darryl Bryk9-Aug-10 11:45
Darryl Bryk9-Aug-10 11:45 
Just a side note on using CImage::GetPixelAddress()...

I haven't seen anybody write on this topic here (maybe I should), but for really fast access to the pixels in a CImage (assuming you want to process every pixel the same way) get a pointer to CImage::GetBits and simply increment the pointer to access each pixel. If the image is a typical bottom-up, like bmp's, then you will need to first covert the image to a top-down style bitmap (use the CImage::Create(sizex, -(int)sizey, 32, 0) with a neg. y size and copy the rows into it), but then every time you modify it you have extremely fast manipulations. In my image processing app. I can do entire image normalizations, thresholds, and color mappings, interactively, while moving a slider! For larger images it lags the slider a bit, but makes it easier to see what your doing to the image in almost real-time.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.