首页 技术 正文
技术 2022年11月11日
0 收藏 393 点赞 2,462 浏览 3210 个字

首先先介绍一下ColorMatrix结构体:表示颜色的变换关系,定义如下:

typedef struct {  REAL m[][];} ColorMatrix;

ColorMatrix结构体一般和ImageAttribute类配合使用,使用的方式是先调用ImageAttibute::SetColorMatrix,运用该颜色变化矩阵,然后在绘制函数中将ImageAttribute对象作为DrawImage函数参数。以下的图像色彩变换都会用到这个结构体。

获取对应编码器的CLSID
   int GetEncoderClsid(const WCHAR* format, CLSID* pClisd); // 获取对应编码器的CLSID  int CMyDlg::GetEncoderClsid(const WCHAR* format, CLSID* pClisd) // 获取对应编码器的CLSID
{
  UINT num = ; // 图像编码器的数量
  UINT size = ; // 图像编码器数组的字节数   Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
  GetImageEncodersSize(&num, &size);
  if (size == )
  return -;
  pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));   GetImageEncoders(num, size, pImageCodecInfo);
  for(UINT j = ;j<num;++j)
  {
    if (wcscmp(pImageCodecInfo[j].MimeType, format) == )
    {
      *pClisd = pImageCodecInfo[j].Clsid;
      free(pImageCodecInfo);
      return j;
    }
  }
  free(pImageCodecInfo);
  return -;
}

一、改变图像的透明度:只需要缩放Alpha分量就能到达效果。

BOOL ChangeImageAlpha(const CString& imagePath, REAL alpha, const CString& savePath); 
/**********************************************************************************
* 作用:改变指定图像的透明度,并将新图像按照指定的图像格式和路径保存
* 参数:imagePath 为原图路径
* alpha 为分量缩放系数
* savePath 为用于保存处理后图像的路径
* 返回值: 转换是否成功
**********************************************************************************/
BOOL CMyGDIDlg::ChangeImageAlpha(const CString& imagePath, REAL alpha, const CString& savePath)
{
  Bitmap bitmap(imagePath);
  if (bitmap.GetLastStatus() != Ok)
  return false;  int nWidth = bitmap.GetWidth();
  int nHeight = bitmap.GetHeight();  // 构建新图像对象
  Bitmap image(nWidth, nHeight);
  Rect rect(, , nWidth, nHeight);
  // 利用新图像对象绘制
  Graphics graph(&image);  // 构建颜色变化矩阵
  ColorMatrix colorMatrix = {
  , , , , ,
  , , , , ,
  , , , , ,
  , , , alpha, ,
  , , , ,
  };  ImageAttributes imageAttr;
  imageAttr.SetColorMatrix(&colorMatrix);  // 运用颜色变换矩阵绘制新图像
  graph.DrawImage(&bitmap, rect, , , nWidth, nHeight, UnitPixel, &imageAttr);  CLSID encoderClsid; // 文件编码器的CLSID
  CString strExt = savePath.Right();
  strExt.MakeLower();
  // 根据扩展名获得不同的CLSID
  if (strExt == _T("png"))
    GetEncoderClsid(_T("image/png"), &encoderClsid);
  else if (strExt == _T("jpg"))
    GetEncoderClsid(_T("image/jpg"), &encoderClsid);
  else
    GetEncoderClsid(_T("image/bmp"), &encoderClsid);  if (image.Save(savePath, &encoderClsid, NULL) == Ok)
    return true;
  else
    return false;
}

调用: ChangeImageAlpha(_T(“E:\\素材\\jpg\\1.jpg”), 0.5, _T(“D:\\1.png”));

二、将图像转换为灰度图:原理就是使图中红、绿、蓝3个分量值相等。一般有3中方式:

(1)平均值法:使每个像素的三原色值等于红、绿、蓝3分量的平均值

  R = G = B = (R + G +B) / 3

(2)最大值法:每个像素的三原色等于红、绿、蓝3分量的最大值

  R = G = B = max(R, G, B)

(3)加权平均值法:给予红、绿、蓝3分量不同的权值然后相加

  R = G = B = WrR + WgG + WbB

  人眼对于三原色的敏感度从高到底分别是绿、红、蓝,所以三原色权值取值关系应该是 Wg > Wr > Wb。

  依据YUV颜色空间可知当 R = G = B = 0.299R + 0.587 + 0.114B时能够的到最合理的灰度图。

  算法与前文类似,在这里只需修改一下颜色变化矩阵即可: 

 // 构建颜色变化矩阵  ColorMatrix colorMatrix = {
    0.299f, 0.299f, 0.299f, , ,
    0.587f, 0.587f, 0.587f, , ,
    0.114f, 0.114f, 0.114f, , ,
    , , , , ,
    , , , ,
  };

三、改变图像的亮度:是通过改变红、绿、蓝颜色分量的增量来实现的。公式如下:  

// brightness 为亮度变化量  REAL f = brightness / 255.0f;
  // 构建颜色变化矩阵
  ColorMatrix colorMatrix = {
    , , , , ,
    , , , , ,
    , , , , ,
    , , , , ,
    f, f, f, ,
  };

四、改变图像的对比度:一般来说对比度越大,图像越清晰醒目,色彩也越鲜明艳丽;而对比度越小,则会让图画显得比较灰暗。

  图像的对比度变化公式如下:其中f为对比度,默认为1.

  Rt = 128 + (R – 128)f

  Gt = 128 + (G -128)f

  Bt = 128+ (B – 128)f 

 REAL f = 0.0f;
  if (contrast >= )
    f = (contrast + 10.0f) / 10.0f;
  else
    f = ( + contrast) / 255.0f;
  // 构建颜色变化矩阵
  ColorMatrix colorMatrix = {
    f, , , , ,
    , f, , , ,
    , , f, , ,
    , , , , ,
    0.5f*(-f), 0.5f*( - f), 0.5f*( - f), ,
  };
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905