常用的CxImage函数及简单的例子



1、Load:reads from disk the image in a specific format;

2、Save:saves to disk the image in a specific format;

3、Filter:2D linear filter(图像锐化,可提高图像清晰度);

4、Copy:copies the image from an exsisting source;

5、Resample:resizes the image ;

6、Saturate:changes the saturation of the image(调整图像饱和度);

7、GrayScale:图像灰度化;

8、Flip:图像上下翻动;

9、Mirror:图像左右翻动;

10、Negative:颜色取反(255-原值);

11、Dither:converts the image to B&W using the desired method ;

12、Threshold:converts the image to B&W(二值化);

13、AdaptiveThreshold:converts the image to B&W, using an optimal threshold mask(自适应阈值法);

14、Colorize:replaces the original hue and saturation values;

15、Solarize:convert all colors above a given lightness level into their negative ;

16、Light:changes the brightness and the contrast of the image(图像亮度和对比度调整);

17、Threshold2:filters only the pixels with a lightness less (or more) than the threshold level, and preserves the   colors for the unfiltered pixels;
18、Dilate:enhance the light areas of the image;
19、Erode:enhance the dark areas of the image;
20、Contour:enhance the variations between adjacent pixels;similar results can be achieved using Filter(), but the algorithms are different both in Edge() and in Contour();
21、Edge:enhance the variations between adjacent pixels(注意线性和非线性的区别);similar results can be achieved using Filter(), but the algorithms are different both in Edge() and in Contour();
22、Trace:finds the contour of an object with a given color;
23、Noise:adds an uniform noise to the image;
24、Jitter:adds a random offset to each pixel in the image;

25、Draw:draws the image in the specified device context, with support for alpha channel, alpha palette, transparency, opacity;
26、GetWidth:获得图像的宽带;
27、GetHeight:获得图像的高度;
28、GetXDPI:获得图像x轴分辨率(支持TIFF、JPEG、PNG、BMP格式);
29、GetYDPI:获得图像y轴分辨率(支持TIFF、JPEG、PNG、BMP格式);
30、IsValid:checks if the image is correctly initializated;
31、SetJpegQuality:set quality level for JPEG and JPEG2000;
32、GetJpegQuality:get quality level for JPEG and JPEG2000;
33、GetNumColors:returns 2, 16, 256; 0 for RGB images;
34、GetLastError:returns the last reported error;
35、SetPixelColor:设置像素颜色值;


1、打开一张图

bool CxImage::Load(const TCHAR * filename, uint32_t imagetype)

2、保存一张图
bool CxImage::Save(LPCWSTR filename, DWORD imagetype=0)
参数和上面是一样的。
3、得到图形数据,以便在OpenGL中使用材质
 BYTE* CxImage::GetBits(DWORD row = 0);
4、得到图形大小
long GetSize();
5、得到图形高度和宽度
 DWORD CxImage::GetHeight();
 DWORD CxImage::GetWidth();
6、得到文件类型
 DWORD CxImage::GetType()  const;
7、得到最后一个错误
 char* CxImage::GetLastError();
8、在界面中绘制出来
long CxImage::Draw(HDC hdc, const RECT& rect, RECT* pClipRect=NULL)
HDC 绘图设备,rect 绘图的区域,确定绘图的左上角和右下角坐标。pClipRect,裁剪区域,一般可以和绘图区域一样大小,除非特殊需要。

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
	CxImage image;
	string imageName = "1.jpg";
	string imageSave = "2.tif";

	image.Load(imageName.c_str(), CXIMAGE_FORMAT_JPG);

	cout<<image.GetBpp()<<endl;

	if (image.IsValid()) {
		image.GrayScale();
		image.Save(imageSave.c_str(), CXIMAGE_FORMAT_TIF);

		cout<<"success"<<endl;
	}

	cout<<"ok"<<endl;
	return 0;
}


//下面是正确的

 CxImage imgsrc2("e:\\8888.bmp",CXIMAGE_FORMAT_BMP);
 CxImage tmp;
 tmp.Copy(imgsrc2);
 tmp.GrayScale();//灰度化
 tmp.Save("e:\\grayscal.bmp",CXIMAGE_FORMAT_BMP);

 
 long lXOffset=0,lYOffset=0;
 bool bMixAlpha=false;
 tmp.Mix( imgsrc2,  CxImage::ImageOpType::OpAdd, lXOffset, lYOffset, bMixAlpha);
 tmp.Erode(2);
 bool falg;

 falg=tmp.AdaptiveThreshold( 0, 64, 0 ,0 , 0.5f);//cximage自适应二值化
 tmp.Dilate(2);
 tmp.Save("e:\\imgsrc3.bmp",CXIMAGE_FORMAT_BMP);


下面介绍应用它进行图像类型转换的方式:

1.从一种图像文件类型转换为另一种文件类型(convert from a format to another)

CxImage image;   // 定义一个CxImage对象

// 从bmp文件转换为jpg文件(bmp -> jpg)
image.Load("image.bmp", CXIMAGE_FORMAT_BMP);   //先装载bmp文件,需要指定文件类型
    // 判断加载的bmp文件是否存在。
if (image.IsValid())......{
       // Returns true if the image has 256 colors and a linear grey scale palette.
    if(!image.IsGrayScale()) image.IncreaseBpp(24);   // param nbit: 4, 8, 24
    image.SetJpegQuality(99);                // 设置图像的压缩质量参数(从0到100,数值越大,质量越高)
    image.Save("image.jpg",CXIMAGE_FORMAT_JPG);          // 把压缩后的图像以jpg文件类型保存起来。
}

// 从png文件转换为tif文件(png -> tif)
image.Load("image.png", CXIMAGE_FORMAT_PNG);
if (image.IsValid())...{
    image.Save("image.tif",CXIMAGE_FORMAT_TIF);
}

2。加载程序资源图像(load an image resource)

即从程序的资源图像中构建CxImage对象,有如下几种方式:

// Load the resource IDR_PNG1 from the PNG resource type
CxImage* newImage = new CxImage();
newImage->LoadResource(FindResource(NULL,MAKEINTRESOURCE(IDR_PNG1),
                       "PNG"),CXIMAGE_FORMAT_PNG);
或者

//Load the resource IDR_JPG1 from DLL
CxImage* newImage = new CxImage();
HINSTANCE hdll=LoadLibrary("imagelib.dll");
if (hdll)...{
    HRSRC hres=FindResource(hdll,MAKEINTRESOURCE(IDR_JPG1),"JPG");
    newImage->LoadResource(hres,CXIMAGE_FORMAT_JPG,hdll);
    FreeLibrary(hdll);
}
或者

//Load a bitmap resource;
HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),
                              MAKEINTRESOURCE(IDB_BITMAP1)));
CxImage *newImage = new CxImage();
newImage->CreateFromHBITMAP(bitmap);
3。在内存缓冲中的图像类型转换

(1)把内存缓冲中的数据解码成一个Image对象(decode an image from memory)

有如下几种方式:
------

CxImage image((BYTE*)buffer,size,image_type);//把内存缓冲buffer中的数据构造成Image对象

//或:

CxMemFile memfile((BYTE*)buffer,size); // 显式使用CxMemFile对象
CxImage image(&memfile,image_type);

//或:

CxMemFile memfile((BYTE*)buffer,size);
CxImage* image = new CxImage();
image->Decode(&memfile,type);


(2)把Image编码存放到内存缓冲中(encode an image in memory)

--------

long size=0;//得到图像大小
BYTE* buffer=0;//存储图像数据的缓冲
image.Encode(buffer,size,image_type);//把image对象中的图像以type类型数据copy到buffer
...
free(buffer);

或:

CxMemFile memfile;            // 显式使用CxMemFile对象
memfile.Open();
image.Encode(&memfile,image_type);
BYTE* buffer = memfile.GetBuffer();
long size = memfile.Size();
...
free(buffer);

---------------

4。处理系统粘贴板中的图像(copy/paste an image)

//copy(到粘贴板)
HANDLE hDIB = image->CopyToHandle();
if (::OpenClipboard(AfxGetApp()->m_pMainWnd->GetSafeHwnd())) ...{
    if(::EmptyClipboard()) ...{
        if (::SetClipboardData(CF_DIB,hDIB) == NULL ) ...{
            AfxMessageBox( "Unable to set Clipboard data" );
     }
CloseClipboard();

//paste(从粘贴板粘贴出来)
HANDLE hBitmap=NULL;
CxImage *newima = new CxImage();
if (OpenClipboard()) hBitmap=GetClipboardData(CF_DIB);
if (hBitmap) newima->CreateFromHANDLE(hBitmap);
CloseClipboard();

5。在picture box中显示一个png格式的文件

HBITMAP m_bitmap = NULL;
CxImage image("myfile.png", CXIMAGE_FORMAT_PNG);
...
m_bitmap = image.MakeBitmap(m_picture.GetDC()->m_hDC);
m_picture.SetBitmap(m_bitmap);
...
if (m_bitmap) DeleteObject(m_bitmap);

四。其它

一个CxImage对象是一个扩展了的位图。作者只是在位图结构上添加了一些起存储信息作用的成员变量。一个CxImage对象(同时)也是一组层。每个层只有在需要时才会分配相应的缓冲区。CxImage::pDib代表着背景图像,CxImage::pAlpha代表着透明层,CxImage:: pSelection代表着被选中的层,被用来创建图像处理时让用户感兴趣的区域。在这三个特殊层面的基础上,你可以增加一些额外的层,这些层可以存储在 CxImage::pLayers中。一般说来,层是一个完整的CxImage对象。因此,你可以构造很复杂的嵌套层。下面是CxImage的一些成员变量:

class CxImage
{
...
protected:
void* pDib;            //包含文件头,调色板等等
BITMAPINFOHEADER head; //标准的文件头(位图)
CXIMAGEINFO info;      //扩展了的信息
BYTE* pSelection;      //用户选中的区域
BYTE* pAlpha;          //alpha通道
CxImage** pLayers;     //通用层
};


typedef struct tagCxImageInfo
{
DWORD   dwEffWidth;       //DWORD 扫描线宽
BYTE*   pImage;           //图像位数
void*   pGhost;           //if this is a ghost, pGhost point to the body
DWORD   dwType;           //原图像的格式
char    szLastError[256]; //出错信息
long    nProgress;        //监视循环的次数
long    nEscape;          //跳出标志
long    nBkgndIndex;      //GIF, PNG, MNG格式使用
RGBQUAD nBkgndColor;      //RGB三原色透明度
BYTE    nQuality;         //JPEG格式使用
long    nFrame;           //TIF, GIF, MNG使用 :实际的帧数
long    nNumFrames;       //TIF, GIF, MNG使用 :帧总数
DWORD   dwFrameDelay;     //GIF, MNG使用
long    xDPI;             //水平分辨率
long    yDPI;             //垂直分辨率
RECT    rSelectionBox;    //选中的矩形区
BYTE    nAlphaMax;        //阴影的最大不透明度
bool    bAlphaPaletteEnabled; //如果调色板中有Alpha通道则为真
bool    bEnabled;         //打开绘图函数
long    xOffset;
long    yOffset;
DWORD   dwEncodeOption;   //一些编码选项
RGBQUAD last_c;           //一些优化选项
BYTE    last_c_index;
bool    last_c_isvalid;
long    nNumLayers;
DWORD   dwFlags;
} CXIMAGEINFO;



1   亮度调整: bool Light(long brightness, long contrast = 0);                

brightness 的取值范围是:-255到255,如果 brightness >0  亮度增加,    brightness <0 亮度 减少
 constrast 对比度的变化:-100 to 100, 中立值是 0.
用法:
void CimageDoc::OnCximageLight()
{
// TODO: 在此添加命令处理程序代码
myImage.Light(-20,0);
UpdateAllViews(0);
}
//Light 原函数内容
bool CxImage ::Light (long brightness , long contrast )
{
       if (! pDib) return false;
       float c=(100 + contrast)/100.0f;
       brightness+=128;

       BYTE cTable[256]; //<nipper>
       for ( int i=0; i<256; i++) {
             cTable[ i] = ( BYTE) max(0, min(255,( int)(( i-128)* c + brightness + 0.5f)));
      }
      return Lut( cTable);
}
/Light原函数内容


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值