Monday, May 27, 2013

basic image processing in MFC

Everybody knows that MFC+OpenCV is very convenient for image processing. But how about only using MFC? Most of the operation for image processing is based on bitmap in previous MFC version, say VC++6.0.

In VS2008, there is already CImage class, which could greatly facilitate the image processing.
 (1) get basic info of one image
  #include <atlimage.h>
  ...
  CImage img; //image object
  img.Load(FilePathName); // load a local image
  img_hight=img.GetHeight();//get hight
  img_width=img.GetWidth();//get width
  img_bits=img.GetBPP();//get bit counts for each pixel, say 8,24,32...
  COLORREF rgb=img.GetPixel(10,10);// get the pixel color
 

  CImage img_new; //another image object
  img_new.Create(img_width, img_hight, 24, 0);//create a image with the same size
  img.SetPixel(10,10,rgb);// set the pixel color

(2) get the r,g,b value from the COLORREF value:

#define MyGetRValue(rgb) ((BYTE)(rgb))
#define MyGetGValue(rgb) ((BYTE)(((WORD)(rgb)) >> 8))
#define MyGetBValue(rgb) ((BYTE)((rgb)>>16))

 COLORREF rgb=img.GetPixel(j,i);
 int r = MyGetRValue(rgb);
 int g=MyGetGValue(rgb);
 int b=MyGetBValue(rgb);

(3) convert the 24bit color image into gray image:

 #define MyRGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))

 BYTE cl = (BYTE)(0.299 * r + 0.587 * g + 0.114 * b);
 int clr=(int)cl;
 COLORREF cf = MyRGB(clr,clr,clr);
  img_gray.SetPixel(j,i,cf);


 
 

No comments:

Post a Comment