对于初学者要入门图像处理这一行,想必大家都遇到这样的问题:
1,单纯用C/C++语言进行图像处理,苦于各种图像读写保存的库的加载调用,或者说实在不想用MFC,不想用CImage等,但是又没有办法;
2,想用高级语言,比如C#/JAVA等,因为他们有强大的图像加载保存API,可以直接方便的调用,但是,算法处理还是C/C++为主,毕竟用C#/JAVA做图像处理项目的太少太少;
3,如果想要自己实现图像的读写/保存算法,没有一定的功底,几乎不太可能,严重阻碍了初学者学习的热情;
对于一个初学者,或者小白来说,要搞清楚这些东西,简直如同噩梦,针对这些问题,本文给出C语言实现的一个更适合初学者入门的基础图像处理框架:
①,BMP/JPG/PNG/TGA格式图像读写/保存代码;
②,点/线/圆/三角形/长方形/多边形等常用形状的绘制代码;
上述两点,均由C语言实现,不依赖任何第三方库,极大的方便了大家学习使用;
1,BMP/JPG/PNG/TGA格式图像读写
这部分代码来自于github上的开源代码:https://github.com/XiuSdk/stb
stb代码框架中包含了各种图像读写操作,所有代码基于C语言实现,不依赖任何第三方,这里,我进行了简单的封装,接口如下:
/************************************************************
*Function: f_SF_ImgBase_ImageLoad
*Description: Image loading
*Params: fileName-image file path,eg:"C:\\test.jpg".
* width-image width.
* height-image height.
* component-the bits per pixel.
* 1 grey
* 2 grey, alpha
* 3 red, green, blue
* 4 red, green, blue, alpha
*Return: image data.
************************************************************/
unsigned char* f_SF_ImgBase_ImageLoad(char* fileName, int* width, int* height, int* component);
/************************************************************
*Function: f_SF_ImgBase_ImageSave
*Description: Image loading
*Params: fileName-image file path,eg:"C:\\save.jpg".
* width-image width.
* height-image height.
* data-the result image data to save, with format BGRA32.
* format-image format,0-BMP,1-JPG,2-PNG,3-TGA
*Return: 0-OK.
************************************************************/
int f_SF_ImgBase_ImageSave(char const *fileName, int width, int height, const void* data, int format);
简单的两个接口,调用也非常简单,这两个接口,图像buffer都是bgra32格式,调用如下:
char* srcPath = "Test\\test.jpg";
char* savePath = "Result\\res.jpg";
int width = 0, height = 0, stride = 0, component = 0, mWidth = 0, mHeight = 0, mStride = 0, mComponent = 0;
unsigned char* srcData = f_SF_ImgBase_ImageLoad(srcPath, &width, &height, &component);
stride = width * 4;
//srcData为bgra32数据
f_SF_ImgBase_ImageSave(savePath, width, height, srcData, 1);
有了这两个接口,初学者再也不用接入什么MFC/CImage/LIBJPG/LIBPNG等复杂的东西了!
2,图像基础绘制
有了图像读写,我们在实际应用中,往往需要在图像中绘制一些标签,比如人脸检测的人脸框,绘制一个三角形,矩形,圆形,或者更复杂的绘制任意一个多边形等等,这些东西,以往我们都是使用opencv或者使用上层语言的GDI+绘制来实现的,但是,这里,本人用C语言实现了这些功能,一共350行代码,包含如下功能:
①绘制实心点和空心点
②绘制线段,允许设置线的宽度
③绘制三角形/矩形/多边形/圆形/椭圆,允许设置画笔宽度
④填充三角形/矩形/多边形/圆形/椭圆
功能接口以输入bgra32格式为主,接口如下:
/********************************************************************
*Function: ImgDrawPoints
*Description: Draw points using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* points-points array,eg:[x0,y0,x1,y1...].
* pointNum-number of points.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
* radius-size of point.
********************************************************************/
void ImgDrawPoints(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int points[], int pointNum, int r, int g, int b, int radius);
/********************************************************************
*Function: ImgDrawHollowPoints
*Description: Draw hollow points using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* points-points array,eg:[x0,y0,x1,y1...].
* pointNum-number of points.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
* penSize-size of pen color.
* radius-size of point.
********************************************************************/
void ImgDrawHollowPoints(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int points[], int pointNum, int r, int g, int b, int penSize, int radius);
/********************************************************************
*Function: ImgDrawLine
*Description: Draw line using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* x1-x position of start point.
* y1-y position of start point.
* x2-x position of end point.
* y2-y position of end point.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
* penSize-size of line.
********************************************************************/
void ImgDrawLine(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride,int x1, int y1, int x2, int y2, int r, int g, int b, int penSize);
//draw rectangle
/********************************************************************
*Function: ImgDrawRectangle
*Description: Draw rectangles using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* x-x position of start point.
* y-y position of start point.
* w-width of rectangle.
* h-height of rectangle.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
* penSize-size of lines.
********************************************************************/
void ImgDrawRectangle(unsigned char*srcData, int srcWidth, int srcHeight, int srcStride, int x, int y, int w, int h, int r, int g, int b, int penSize);
/********************************************************************
*Function: ImgDrawTriangle
*Description: Draw triangles using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* x-x position of frist point.
* y-y position of frist point.
* x1-x position of second point.
* y1-y position of second point.
* x2-x position of third point.
* y2-y position of third point.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
* penSize-size of lines.
********************************************************************/
void ImgDrawTriangle(unsigned char*srcData, int srcWidth, int srcHeight, int srcStride, int x, int y, int x1, int y1, int x2, int y2, int r, int g, int b, int penSize);
/********************************************************************
*Function: ImgDrawCircle
*Description: Draw circle using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* centerX-x position of center point.
* centerY-y position of center point.
* radius-radius of circle.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
* penSize-size of pen color.
********************************************************************/
void ImgDrawCircle(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int centerX, int centerY, int radius, int r, int g, int b, int penSize);
/********************************************************************
*Function: ImgDrawFillCircle
*Description: Fill circle using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* centerX-x position of center point.
* centerY-y position of center point.
* radius-radius of circle.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
********************************************************************/
void ImgDrawFillCircle(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int centerX, int centerY, int radius, int r, int g, int b);
/********************************************************************
*Function: ImgDrawFillTriangle
*Description: Fill triangles using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* x-x position of frist point.
* y-y position of frist point.
* w-width of rectangle.
* h-height of rectangle.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
********************************************************************/
void ImgDrawFillRectangle(unsigned char*srcData, int srcWidth, int srcHeight, int srcStride, int x, int y, int w, int h, int r, int g, int b);
/********************************************************************
*Function: ImgDrawFillTriangle
*Description: Fill triangles using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* x-x position of frist point.
* y-y position of frist point.
* x1-x position of second point.
* y1-y position of second point.
* x2-x position of third point.
* y2-y position of third point.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
********************************************************************/
void ImgDrawFillTriangle(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int x, int y, int x1, int y1, int x2, int y2, int r, int g, int b);
/********************************************************************
*Function: ImgDrawPolygon
*Description: Draw Polygon using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* polygonPoints-points array of polygon, eg:[x0, y0, x1, y1,...].
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
* penSize-size of pen color.
********************************************************************/
void ImgDrawPolygon(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int polygonPoints[], int pointNum, int r, int g, int b, int penSize);
/********************************************************************
*Function: ImgDrawFillPolygon
*Description: Fill Polygon using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* polygonPoints-points array of polygon, eg:[x0, y0, x1, y1,...].
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
********************************************************************/
void ImgDrawFillPolygon(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int polygonPoints[], int pointNum, int r, int g, int b);
/********************************************************************
*Function: ImgDrawEllipse
*Description: Draw Ellipse using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* centerX-center x of eclipse.
* centerY-center y of eclipse.
* A-A of eclipse(A > B).
* B-B of eclipse(B < A).
* penSize-size of pen color.
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
********************************************************************/
void ImgDrawEllipse(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int centerX, int centerY, int A, int B, int r, int g, int b, int penSize);
/********************************************************************
*Function: ImgDrawFillEclipse
*Description: Fill Ellipse using specified color.
*Params:
* srcData-source image with bgra32 pixelformat.
* srcWidth-width of source image.
* srcHeight-height of source image.
* srcStride-stride of source image.
* centerX-center x of eclipse.
* centerY-center y of eclipse.
* A-A of eclipse(A > B).
* B-B of eclipse(B < A).
* r-Red component of pen color.
* g-Green component of pen color.
* b-Blue component of pen color.
********************************************************************/
void ImgDrawFillEllipse(unsigned char* srcData, int srcWidth, int srcHeight, int srcStride, int centerX, int centerY, int A, int B, int r, int g, int b);
调用举例如下:
//Drawing Rectangle
int x = 100, y = 100, w = 50, h = 50;
ImgDrawRectangle(srcData, width, height, stride, x, y, w, h, penColorR, penColorG, penColorB, 3);
//Drawing Triangle
ImgDrawTriangle(srcData, width, height, stride, x, y, x + 100, y, x, y + 100, penColorG, penColorR, penColorB, 1);
//fill rectangle
ImgDrawFillRectangle(srcData, width, height, stride, 100, 300, w, h, penColorR, penColorG, penColorB);
//fill triangle
ImgDrawFillTriangle(srcData, width, height, stride, x, y + 100, x + 100, y + 100, x, y + 200, penColorG, penColorR, penColorB);
int polyPoints[8] = {200, 200, 250, 130, 280, 220, 230, 300};
//draw polygon
ImgDrawPolygon(srcData, width, height, stride, polyPoints, 4, 200, 100, 50, 2);
int polyPoints1[8] = {300, 400, 350, 130, 480, 220, 330, 400};
//fill polygon
ImgDrawFillPolygon(srcData, width, height, stride, polyPoints1, 4, 200, 100, 250);
//fill ellipse
ImgDrawFillEllipse(srcData, width, height, stride, 500, 500, 100, 50, 155, 245,67);
//draw ellipse
ImgDrawEllipse(srcData, width, height, stride, 200, 500, 100, 50, 255, 45,67, 1);
对应效果图如下:
整体算法编译DLL不足100K,可谓轻量级的库。
在使用时,只需要把库ImageProcessBasicFramework.dll和ImageProcessBasicFramework.lib以及头文件ImgBasic文件夹放到工程路径下,点击工程属性,配置link-input与VC++目录即可,如下所示:
最后,给出代码库及DEMO下载链接:https://download.csdn.net/download/trent1985/11218979
代码库为VS2010 编译,DEMO为VS2010工程。