Cimage类处理图像像素(数据)的3种方式

来源:互联网 发布:nginx限制返回大小 编辑:程序博客网 时间:2024/05/01 04:33

这里只讨论对图像像素的处理,cimage类的具体用法查相关资料
#include <atlimage.h>   //VS2010以后不用加这个
 ……………………
CImage  m_Image;     //或CImage*  m_Image;  下面例子程序我用的CImage  m_Image; 只是一个用成员选择符,一个用指针操作,效率上可能有所差异

下面是3种方法:

一、用Cimage类的成员函数进行处理

这里假设你已经加载了图像位图,并与CImage对象m_Image相关联。相关成员函数主要有:     

 GetPixel 返回像素颜色                        

SetPixel 设置像素颜色

 如:m_Image.SetPixel(  i-1,  j-1,  RGB(rr,gg,bb));

SetPixelRGB 设置像素的红绿蓝

如:m_Image.SetPixelRGB(x,y,avg,avg,avg); 

SetColorTable 设置调色板颜色分量(红、绿、蓝)值

GetWidth 宽度(以像素为单位)                      

GetHeight 高度

1、程序示例

1)一个双线性插值放大程序。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if  (m_Image.IsNull())    
  2.      return;   
  3.     // 创建对话框  
  4.     DlgInterpolation TranPara;    
  5.     //显示对话框,提示用户设定量  
  6.     if (TranPara.DoModal() != IDOK)  
  7.         return;   
  8.     int k=TranPara.m_inter;  
  9.     BeginWaitCursor();  
  10.     CImage  m_Image1;  
  11.     if (! m_Image1.IsNull())   
  12.     {     
  13.          m_Image1.Destroy();   
  14.     }  
  15.     m_Image1.Create( m_Image.GetWidth()*k, m_Image.GetHeight()*k, 24,0);  
  16.     // 四个最临近象素的坐标  
  17.     int x1, x2;  
  18.     int y1, y2;  
  19.     // 四个最临近象素值  
  20.     unsigned char   f1, f2, f3, f4;  
  21.     // 二个插值中间值  
  22.     unsigned char   f12, f34;  
  23.     //计算结果  
  24.     int fr,fb,fg;  
  25.     double  epsilon = 0.001;      
  26.     COLORREF pixel11,pixel12,pixel21,pixel22;  
  27.     int nHeight1 = m_Image1.GetHeight();  
  28.     int nWidth1 = m_Image1.GetWidth();  
  29.     int nHeight = m_Image.GetHeight();  
  30.     int nWidth = m_Image.GetWidth();  
  31.     double m=((double)nWidth1-1)/((double)nWidth-1);  
  32.     for  (int  i=0;  i<nWidth1;  i++)   
  33.     {   
  34.             for  (int  j=0;  j<nHeight1;  j++)   
  35.             {   
  36.                 double x=double((double)i/m);  
  37.             double y=double((double)j/m);  
  38.             //计算四个最临近象素的坐标,+1向右下方移动  
  39.             x1 = (int) x;  
  40.             x2 = x1 + 1;  
  41.             y1 = (int) y;  
  42.             y2 = y1 + 1;      
  43.             if( (x < 0) || (x > nWidth - 1) || (y < 0) || (y > nHeight - 1))  
  44.             {  
  45.             //要计算的点不在源图范围内,返回-1  
  46.             continue;  
  47.             }  
  48.             else  
  49.             {  
  50.             if (fabs(x - nWidth + 1) <= epsilon )  
  51.             {  
  52.               // 要计算的点在图像右边缘上  
  53.               if (fabs(y -nHeight + 1) <= epsilon)  
  54.               {  
  55.                 // 要计算的点正好是图像最右下角那一个象素,直接返回该点象素值  
  56.                 pixel11 = m_Image.GetPixel(x1,y1);   
  57.                     f1 = (unsigned char)GetRValue(pixel11);  
  58.                     fr=(int)f1;  
  59.                 f1 = (unsigned char)GetGValue(pixel11);  
  60.                 fg=(int)f1;  
  61.                 f1 = (unsigned char)GetBValue(pixel11);  
  62.                 fb=(int)f1;  
  63.                }  
  64.                else  
  65.                {  
  66.                 // 在图像右边缘上且不是最后一点,直接一次插值即可  
  67.                 pixel11 = m_Image.GetPixel(x1,y1);  
  68.                 pixel12 = m_Image.GetPixel(x1,y2);   
  69.                 f1 = (unsigned char)GetRValue(pixel11);   
  70.                 f3 = (unsigned char)GetRValue(pixel12);               
  71.                 fr= (int) (f1 + (y -y1) * (f3 - f1));  
  72.                 f1 = (unsigned char)GetGValue(pixel11);   
  73.                 f3 = (unsigned char)GetGValue(pixel12);               
  74.                 fg= (int) (f1 + (y -y1) * (f3 - f1));  
  75.                 f1 = (unsigned char)GetBValue(pixel11);   
  76.                 f3 = (unsigned char)GetBValue(pixel12);               
  77.                 fb= (int) (f1 + (y -y1) * (f3 - f1));  
  78.                }  
  79.              }  
  80.              else if (fabs(y - nHeight + 1) <= epsilon)  
  81.              {  
  82.                       // 要计算的点在图像下边缘上且不是最后一点,直接一次插值即可  
  83.                pixel11 = m_Image.GetPixel(x1,y1);  
  84.                pixel21 = m_Image.GetPixel(x2,y1);   
  85.                f1 = (unsigned char)GetRValue(pixel11);   
  86.                f2 = (unsigned char)GetRValue(pixel21);   
  87.                fr=(int) (f1 + (x -x1) * (f2 - f1));  
  88.                f1 = (unsigned char)GetGValue(pixel11);   
  89.                f2 = (unsigned char)GetGValue(pixel21);   
  90.                fg=(int) (f1 + (x -x1) * (f2 - f1));  
  91.                f1 = (unsigned char)GetBValue(pixel11);   
  92.                f2 = (unsigned char)GetBValue(pixel21);   
  93.                fb=(int) (f1 + (x -x1) * (f2 - f1));  
  94.               }  
  95.               else  
  96.               {  
  97.                pixel11 = m_Image.GetPixel(x1,y1);   
  98.                        pixel12 = m_Image.GetPixel(x1,y2);     
  99.                        pixel21 = m_Image.GetPixel(x2,y1);     
  100.                        pixel22 = m_Image.GetPixel(x2,y2);   
  101.                // 计算四个最临近象素值  
  102.                f1 = (unsigned char)GetRValue(pixel11);   
  103.                f2 = (unsigned char)GetRValue(pixel21);   
  104.                    f3 = (unsigned char)GetRValue(pixel12);   
  105.                    f4 = (unsigned char)GetRValue(pixel22);   
  106.                f12 = (unsigned char) (f1 + (x - x1) * (f2 - f1));  
  107.                f34 = (unsigned char) (f3 + (x - x1) * (f4 - f3));  
  108.                fr= (int) (f12 + (y -y1) * (f34 - f12));  
  109.                f1 = (unsigned char)GetGValue(pixel11);   
  110.                f2 = (unsigned char)GetGValue(pixel21);   
  111.                f3 = (unsigned char)GetGValue(pixel12);   
  112.                f4 = (unsigned char)GetGValue(pixel22);   
  113.                f12 = (unsigned char) (f1 + (x - x1) * (f2 - f1));  
  114.                f34 = (unsigned char) (f3 + (x - x1) * (f4 - f3));  
  115.                fg= (int) (f12 + (y -y1) * (f34 - f12));  
  116.                f1 = (unsigned char)GetBValue(pixel11);   
  117.                f2 = (unsigned char)GetBValue(pixel21);   
  118.                f3 = (unsigned char)GetBValue(pixel12);   
  119.                f4 = (unsigned char)GetBValue(pixel22);   
  120.                f12 = (unsigned char) (f1 + (x - x1) * (f2 - f1));  
  121.                f34 = (unsigned char) (f3 + (x - x1) * (f4 - f3));  
  122.                fb= (int) (f12 + (y -y1) * (f34 - f12));  
  123.               }  
  124.             }  
  125.                 m_Image1.SetPixel(i,j,  RGB(fr,fg,fb));           
  126.        }  
  127.      }  
  128.      m_Image.Destroy();  
  129.      m_Image.Create( m_Image1.GetWidth(), m_Image1.GetHeight(), 24, 0);  
  130.      COLORREF pixel;  
  131.      for  (int  i=0;  i<nWidth1;  i++)   
  132.      {   
  133.           for  (int  j=0;  j<nHeight1;  j++)   
  134.           {   
  135.            pixel = m_Image1.GetPixel(i,j);   
  136.            int y=GetRValue(pixel);  
  137.            int p=GetGValue(pixel);  
  138.            int b=GetBValue(pixel);  
  139.                m_Image.SetPixelRGB(i,j,GetRValue(pixel),GetGValue(pixel),GetBValue(pixel));   
  140.       }  
  141.      }  
  142.     m_Image1.Destroy();  
  143.     Invalidate();  
  144.     EndWaitCursor();  

2)处理视频帧

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ……  
  2. Defog(imageprosses, nimgWidth, nimgheigt);/*我加的一个雾天图像增强的动态库,imageprosses是视频的一帧,输入imageprosses处理,并输出imageprosses*/  
  3. int  rr  =  0,  gg  =  0,  bb  =  0;   
  4. for  (int i  =  0;  i  < nimgWidth;  i++)  
  5. {   
  6.     for  (int j  =  1;  j  <= nimgheigt;  j++)  
  7.     {   
  8.          bb=(int)imageprosses[3*i*j];  
  9.          gg=(int)imageprosses[3*i*j+1];  
  10.          rr=(int)imageprosses[3*i*j+2];  
  11.          m_Image.SetPixel(i,  j-1,  RGB(rr,gg,bb));/*设置一帧图像的像素值用来显示*/                 
  12.     }  
  13. }  
  14. ……  

2、比较:非常慢。一个图像数据一般很大的,函数调用、参数传递会更加耗时。

二、直接对内存进行操作

相关成员函数:

GetPitch 行距

GetBPP 每像素位数,用时记得GetBPP()/8

GetBits 返回图像像素数据指针

1、程序示例

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ……  
  2. Defog(imageprosses, nimgWidth, nimgheigt);  
  3. //调用cimage类  
  4. if  (m_Image.IsNull())   
  5.     m_Image.Create( nimgWidth, nimgheigt, 24,0);  
  6.     //地址访问方式  
  7. byte* pRealData;  
  8. //首地址    
  9. pRealData=(byte*)m_Image.GetBits();  
  10. //行距  
  11. int pit=m_Image.GetPitch();  
  12. for  (int i  =  0;  i  <nimgWidth;  i++)  
  13. {   
  14.     for  (int j  =  0;  j  < nimgheigt;  j++)  
  15.     {   
  16.     *(pRealData + pit*j+ i*3)=(int)imageprosses[3*(nimgheigt-1-j)*nimgWidth+3*i];      
  17.     *(pRealData + pit*j +i*3 +1)=(int)imageprosses[3*(nimgheigt-1-j)*nimgWidth+3*i+1];    
  18.     *(pRealData + pit*j + i*3 +2)=(int)imageprosses[3*(nimgheigt-1-j)*nimgWidth+3*i+2];   
  19.     }  
  20. }  
  21. m_Image.Draw(pDC->m_hDC,0,0,nWidth,nheigt);    
  22. ……  

2、比较: 对地址直接操作最快,不需要多余的转换。

三、用数组进行处理

       如果处理比较复杂的话,可把所有点颜色全部读到一个二维数组里面,然后对这个数组进行读写和处理。 再把处理后的图像显示出来。最方便的是可以进行一些自己需要的预处理,比如我是这样做的。

      首先定义一个相关头文件和源文件处理相关内存操作

//MYIMAGE.h

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #if !defined(MyIMAGE)  
  2. #define MyIMAGE  
  3. #if _MSC_VER > 1000  
  4. #pragma once  
  5. #endif // _MSC_VER > 1000  
  6. #include "windows.h"  
  7.   
  8. // 灰度图  
  9. typedef byte **MYIMAGE;/*二维数组的数组名就是一个指向指针的指针,a的作用相当于**a,而a->[0],相当于a[0][0]*/  
  10. //彩色图  
  11. typedef struct MYCOLORIMAGESTRUCT  
  12. {   
  13.      MYIMAGE r, g, b;   
  14. }MYCOLORIMAGE;  
  15.   
  16. ////// 8bit图像操作  
  17. // 8bit图像分配  
  18. MYIMAGE MyImageAlloc(int height,int width);  
  19.   
  20. // 8bit图像释放  
  21. void MyImageFree(MYIMAGE a, int height);  
  22.   
  23. // 8bit图像拷贝  
  24. bool MyImageCopy(MYIMAGE dest, MYIMAGE source, int width, int height);  
  25.   
  26. // 8bit图像设置  
  27. void MyImageSet(MYIMAGE a, int value, int width, int height);  
  28.   
  29. ////// 24bit图像操作  
  30. // 24bit图像分配  
  31. MYCOLORIMAGE MyColorImageAlloc(int height,int width);  
  32.   
  33. // 24bit图像释放  
  34. void MyColorImageFree(MYCOLORIMAGE a, int height);  
  35.   
  36. // 24bit图像拷贝  
  37. bool MyColorImageCopy(MYCOLORIMAGE dest, MYCOLORIMAGE source, int width, int height);  
  38.   
  39. // 24bit图像设置  
  40. void MyColorImagSet(MYCOLORIMAGE a, int value, int width, int height);  
  41.   
  42. // 彩色图像转灰度图象  
  43. bool MyColorToGray(MYIMAGE outGrayImg, MYCOLORIMAGE inColorImg, int width, int Height);  
  44.   
  45. #endif MyIMAGE  

//MYIMAGE.cpp

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "StdAfx.h"  
  2. #include "MYIMAGE.h"  
  3. //灰度图像  
  4. /**************my分配内存空间***************/  
  5. MYIMAGE MyImageAlloc(int height,int width)  
  6. {     
  7.     MYIMAGE a = (MYIMAGE) new MYIMAGE*[height];//数组指针  
  8.     int i;  
  9.     byte* pTemp;  
  10.     pTemp = new UCHAR[height*width];  
  11.       
  12.     for(i = 0; i < height; i++)  
  13.     {  
  14.         a[i] = pTemp + width * i;  
  15.     }  
  16.           
  17.     return a;  
  18. }  
  19. /*********释放内存空间***************/  
  20. void MyImageFree(MYIMAGE a, int height)  
  21. {  
  22.     delete[] a[0];  
  23.     delete a;  
  24. }  
  25. /*******拷贝************/  
  26. bool MyImageCopy(MYIMAGE dest, MYIMAGE source, int width, int height)  
  27. {  
  28.     if(dest == NULL || source == NULL || width <= 0 || height <= 0)  
  29.         return false;  
  30.   
  31.     int i = 0;  
  32.     for(i = 0; i < height; i++)  
  33.     {  
  34.         memcpy(dest[i], source[i], sizeof(UCHAR) * width);  
  35.     }  
  36.       
  37.     return true;  
  38. }  
  39. /*******赋值*******/  
  40. void MyImageSet(MYIMAGE a, int value, int width, int height)  
  41. {  
  42.     int i;   
  43.     for(i = 0; i < height; i++)  
  44.     {  
  45.         memset(a[i], value, sizeof(UCHAR) * width);  
  46.     }  
  47. }  
  48. //彩色图像  
  49. /**************my彩色图像分配内存空间**********************/  
  50. MYCOLORIMAGE MyColorImageAlloc(int height,int width)  
  51. {  
  52.     //MYCOLORIMAGE a = (MYCOLORIMAGE) new char[sizeof(MYIMAGE) * 3];  
  53.     MYCOLORIMAGE a;  
  54.     a.r = MyImageAlloc(height,width);  
  55.     a.g = MyImageAlloc(height,width);  
  56.     a.b = MyImageAlloc(height,width);  
  57.   
  58.     return a;  
  59. }  
  60. /****************my彩色图像空间内存释放*********************/  
  61. void MyColorImageFree(MYCOLORIMAGE a, int height)  
  62. {  
  63.     /* 
  64.     MyImageFree(a->r, height); 
  65.     MyImageFree(a->g, height); 
  66.     MyImageFree(a->b, height); 
  67.     delete a; 
  68.     */  
  69.     MyImageFree(a.r, height);  
  70.     MyImageFree(a.g, height);  
  71.     MyImageFree(a.b, height);  
  72. }  
  73. /***************my彩色图像拷贝******************/  
  74. bool MyColorImageCopy(MYCOLORIMAGE dest, MYCOLORIMAGE source, int width, int height)  
  75. {  
  76.     /* 
  77.     if(dest == NULL || source == NULL || width <= 0 || height <= 0) 
  78.     return false; 
  79.     int i = 0; 
  80.     for(i = 0; i < height; i++) 
  81.     { 
  82.         memcpy(dest->r[i], source->r[i], sizeof(UCHAR) * width); 
  83.         memcpy(dest->g[i], source->g[i], sizeof(UCHAR) * width); 
  84.         memcpy(dest->b[i], source->b[i], sizeof(UCHAR) * width); 
  85.     } 
  86.     return true; 
  87.     */  
  88.     if(dest.r == NULL || source.r == NULL || width <= 0 || height <= 0)  
  89.     return false;  
  90.     for(int i = 0; i < height; i++)  
  91.     {  
  92.         memcpy(dest.r[i], source.r[i], sizeof(UCHAR) * width);  
  93.         memcpy(dest.g[i], source.g[i], sizeof(UCHAR) * width);  
  94.         memcpy(dest.b[i], source.b[i], sizeof(UCHAR) * width);  
  95.     }  
  96.     return true;  
  97. }  
  98. /**********my彩色图像赋值*****************/  
  99. void MyColorImagSet(MYCOLORIMAGE a, int value, int width, int height)  
  100. {  
  101.     int i;   
  102.     for(i = 0; i < height; i++)  
  103.     {  
  104.         memset(a.r[i], value, sizeof(UCHAR) * width);  
  105.         memset(a.g[i], value, sizeof(UCHAR) * width);  
  106.         memset(a.b[i], value, sizeof(UCHAR) * width);  
  107.     }  
  108. }  
  109. /**********my彩色图转为灰度图**************/  
  110. bool MyColorToGray(MYIMAGE outGrayImg, MYCOLORIMAGE inColorImg, int width, int Height)  
  111. {  
  112.     if(outGrayImg == NULL || inColorImg.r == NULL || width <= 0 || Height <= 0)  
  113.         return false;  
  114.   
  115.     for(int j = 0; j < Height; j++)  
  116.     {  
  117.         for(int i = 0; i < width; i++)  
  118.         {  
  119.             outGrayImg[j][i] = (int)(0.3 * inColorImg.r[j][i] + 0.59 * inColorImg.g[j][i] + 0.11 * inColorImg.b[j][i]);  
  120.         }  
  121.     }  
  122.   
  123.     return TRUE;  
  124. }  

            然后一些预处理操作,如格式间转换:

 //ImageTransform.h

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #if !defined(ImageTransform)  
  2. #define ImageTransform  
  3.   
  4. #if _MSC_VER > 1000  
  5. #pragma once  
  6. #endif // _MSC_VER > 1000  
  7.   
  8. //#include "ximage.h"  
  9. #include "MYIMAGE.h"  
  10. #include "atlimage.h"   
  11. //CImage到MYIMAGE之间的格式转换  
  12. //灰度  
  13. // CImage转换成MYIMAGE  
  14. BOOL CxImageToMYIMAGE(MYIMAGE destpImgbuf, CImage&  sourpCxImage, int nImgWidth, int nImgHeight);  
  15. // MYIMAGE转换成CImage  
  16. BOOL MYIMAGEToCxImage(CImage&  destpCxImage,  MYIMAGE sourpImgbuf, int nImgWidth, int nImgHeight);  
  17.   
  18. //彩色  
  19. // CImage转换成MYCOLORIMAGE  
  20. BOOL CxxImageToMYIMAGE(MYCOLORIMAGE destpImgbuf, CImage&  sourpCxImage, int nImgWidth, int nImgHeight);  
  21. // MYCOLORIMAGE转换成CImage  
  22. BOOL MYIMAGEToCxxImage(CImage&  destpCxImage,  MYCOLORIMAGE sourpImgbuf, int nImgWidth, int nImgHeight);  
  23.   
  24. #endif ImageTransform  

//ImageTransform.cpp

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "StdAfx.h"  
  2. #include "ImageTransform.h"  
  3. //灰度  
  4. //  CImage转换成MYIMAGE  
  5. BOOL CxImageToMYIMAGE(MYIMAGE destpImgbuf, CImage&  sourpCxImage, int nImgWidth, int nImgHeight)  
  6. {  
  7.     // 参数检查  
  8.     if(destpImgbuf == NULL || sourpCxImage == NULL || nImgWidth <= 0 || nImgHeight <= 0)  
  9.     return FALSE;  
  10.     int i, j;  
  11.     COLORREF rgb;  
  12.     //8位灰度图转存为8位灰度图  
  13.      if(sourpCxImage.GetBPP()/8 ==1)   
  14.     {  
  15.         for(j = 0; j < nImgHeight; j++)  
  16.         {  
  17.             for(i = 0; i < nImgWidth; i++)  
  18.             {      
  19.              destpImgbuf[j][i] = (byte)sourpCxImage.GetPixel(i, j);  
  20.             }  
  21.         }  
  22.   
  23.     }  
  24.     //24位灰度图象转换为8位灰度图  
  25.     else if(sourpCxImage.GetBPP()/8==3)  
  26.     {  
  27.         for(j = 0; j < nImgHeight; j++)  
  28.         {  
  29.             for(i = 0; i < nImgWidth; i++)  
  30.             {  
  31.                 rgb = sourpCxImage.GetPixel(i, j);  
  32.                 destpImgbuf[j][i] = GetRValue(rgb);  
  33.             }  
  34.         }  
  35.     }  
  36.      return TRUE;  
  37. }  
  38. // MYIMAGE转换成CImage  
  39. BOOL MYIMAGEToCxImage(CImage& destpCxImage, MYIMAGE sourpImgbuf, int nImgWidth, int nImgHeight)  
  40. {  
  41.     // 参数检查  
  42.     if(destpCxImage == NULL || sourpImgbuf == NULL || nImgWidth <= 0 || nImgHeight <= 0)  
  43.     return FALSE;  
  44.     int i, j;  
  45.     //8位灰度图转换为24位灰度图  
  46.     for(j = 0; j < nImgHeight; j++)  
  47.     {  
  48.         for(i = 0; i < nImgWidth; i++)  
  49.         {  
  50.             destpCxImage.SetPixelRGB(i, j, sourpImgbuf[j][i],sourpImgbuf[j][i],sourpImgbuf[j][i]);  
  51.         }  
  52.     }  
  53.   
  54.     return TRUE;  
  55. }  
  56.   
  57.   
  58. //24位真彩色  
  59. //CImage转换成MYCOLORIMAGE  
  60. BOOL CxxImageToMYIMAGE(MYCOLORIMAGE destpImgbuf, CImage& sourpCxImage, int nImgWidth, int nImgHeight)  
  61. {  
  62.     // 参数检查  
  63.     if(destpImgbuf.r == NULL || sourpCxImage == NULL || nImgWidth <= 0 || nImgHeight <= 0)  
  64.         return FALSE;  
  65.       
  66.     int i, j;  
  67.     COLORREF rgb;  
  68.     //24位转换为24位存储  
  69.     for(j = 0; j < nImgHeight; j++)  
  70.     {  
  71.         for(i = 0; i < nImgWidth; i++)  
  72.         {  
  73.             rgb=sourpCxImage.GetPixel(i, j);  
  74.             destpImgbuf.r[j][i] = GetRValue(rgb);  
  75.             destpImgbuf.g[j][i] = GetGValue(rgb) ;  
  76.             destpImgbuf.b[j][i] = GetBValue(rgb) ;  
  77.         }  
  78.     }  
  79.     return TRUE;  
  80. }  
  81.   
  82. //MYCOLORIMAGE转换成CImage  
  83. BOOL MYIMAGEToCxxImage(CImage& destpCxImage, MYCOLORIMAGE sourpImgbuf, int nImgWidth, int nImgHeight)  
  84. {  
  85.     // 参数检查  
  86.     if(destpCxImage == NULL || sourpImgbuf.r == NULL || nImgWidth <= 0 || nImgHeight <= 0)  
  87.         return FALSE;  
  88.   
  89.     int i, j;  
  90.     //24位转换为24位存储  
  91.     for(j = 0; j < nImgHeight; j++)  
  92.     {  
  93.         for(i = 0; i < nImgWidth; i++)  
  94.         {  
  95.             destpCxImage.SetPixelRGB(i, j,sourpImgbuf.r[j][i],sourpImgbuf.g[j][i],sourpImgbuf.b[j][i]);  
  96.         }  
  97.     }  
  98.   
  99.     return TRUE;  
  100. }  

1、程序示例,一个Laplacian算子进行锐化的例子。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //模版系数取1   
  2.      if  (m_Image.IsNull())   
  3.      return;   
  4.      BeginWaitCursor();  
  5.      if  (!m_Img.IsNull())   
  6.      m_Img.Destroy();  
  7.      m_Img.Create( m_Image.GetWidth(), m_Image.GetHeight(), 24,0);  
  8.      int  nWidth  =  m_Image.GetWidth();   
  9.      int  nHeight  =  m_Image.GetHeight();   
  10.      MYIMAGE RData=NULL;  
  11.      MYIMAGE GData=NULL;  
  12.      MYIMAGE BData=NULL;  
  13.      RData=MyImageAlloc(nHeight,nWidth);  
  14.      GData=MyImageAlloc(nHeight,nWidth);  
  15.      BData=MyImageAlloc(nHeight,nWidth);  
  16.      COLORREF color ;   
  17.        
  18.      for(int j = 0;j<nWidth;j++)  
  19.      for(int i = 0;i<nHeight; i++)  
  20.          {  
  21.           color = m_Image.GetPixel(j,i);   
  22.           RData[i][j]= GetRValue(color);      
  23.           GData[i][j]= GetGValue(color);     
  24.           BData[i][j]= GetBValue(color);   
  25.          }  
  26.            
  27.      int  templ[9]  =  {0,-1,0,-1,4,-1,0,-1,0};       
  28.       for  (int i  =  1;  i  <  nWidth-1;  i++)  
  29.     {   
  30.          for  (int j  =  1;  j  <  nHeight-1;  j++)  
  31.          {   
  32.                  int  r =  0,  g=  0,  b =  0;   
  33.                  int  index  =  0;   
  34.                  //模版1  
  35.                  for  (int  col  =  -1;  col  <=  1;  col++)  
  36.                  {   
  37.                        for  (int  row  =  -1;  row  <=  1;  row++)  
  38.                        {   
  39.                              r+=  RData[j+row][i+col] * templ[index];   
  40.                              g+=  GData[j+row][i+col] * templ[index];   
  41.                              b+=  BData[j+row][i+col] * templ[index];   
  42.                              index++;  
  43.                        }  
  44.                  }  
  45.                  if   (  r  <  0  )  r  =  -r;   
  46.                  else  if (  r  >  255  )  r  =  255;  
  47.                  if   (  g  <  0  )  g  = -g;   
  48.                  else  if  (  g  >  255  )  g  =  255;  
  49.                  if  (  b  <  0  )  b  = -b;   
  50.                  else  if  (  b  >  255  )  b  =  255;   
  51.                  m_Img.SetPixelRGB(i,j,r,g,b);  
  52.          }  
  53.      }  
  54.      MyImageFree(RData, nHeight);  
  55.      MyImageFree(GData, nHeight);  
  56.      MyImageFree(BData, nHeight);  
  57.      Invalidate();  
  58.      EndWaitCursor();    

2、比较:比较方便也比较快

0 0
原创粉丝点击