對於初學者要入門圖像處理這一行,想必大家都遇到這樣的問題:
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工程。