Cimg是一套用c++寫出來的image processing library. 只有一個將近四萬行header檔,但是可以協助做許多影像處理功能.不過因為source全集中在一個檔案,所以沒有文件有一點閱讀不易.
 

一個hello world的程式:

#include "CImg.h"
using namespace cimg_library;
int main() 
{
    // 640x400 的彩色圖像並定義3channel顏色
    CImg<unsigned char> img(640, 400, 1, 3);  

    //將顏色全設定為黑色
    img.fill(0); 

    // 紫色
    unsigned char purple[] = { 255, 0, 255 };
    
    // 在座標(100, 100)輸出紫色的“Hello world”
    img.draw_text(100, 100, "Hello World",purple);  
    
    // 透過CImgDisplay顯示一個視窗
    img.display("My first CImg code");                  
    
    return 0;
}

內含主要四種類別:

CImg
CImgDisplay
CImgException
CImgList

先列出一些CImg建構子:

CImg (const unsigned int dx, const unsigned int dy=1, const unsigned int dz=1, const unsigned int dc=1)

 Constructs a new image with given size (dx,dy,dz,dc). (建構一個空白圖片)
 dx = width
 dy = height
 dz = depth /* Classical 2d images have depth defined to 1 */, 如果2d圖片值就是1
 dc = spectrum /*Scalar-valued images (one value per pixel) have spectrum defined to 1. */ ,一個點需要幾個channel數,簡白一點就是如果RGB三色就是3 channles or bytes

CImg (const unsigned int dx, const unsigned int dy, const unsigned int dz, const unsigned int dc, const T val)

 Construct an image with given size (dx,dy,dz,dc) and with pixel having a default value val.(每個值都固定填上 val.)

CImg (const unsigned int dx, const unsigned int dy, const unsigned int dz, const unsigned int dc, const int val0, const int val1,...)

 Construct an image with given size (dx,dy,dz,dc) and with specified pixel values (int version).

CImg (const unsigned int dx, const unsigned int dy, const unsigned int dz, const unsigned int dc, const char const values, const bool repeat_values)

 Construct an image with given size and with specified values given in a string.

CImg (const T const data_buffer, const unsigned int dx, const unsigned int dy=1, const unsigned int dz=1, const unsigned int dc=1, const bool shared=false)

 

  從data_buffer裡面輸入,但是在這個library,它是一個channel填完之後,換下個channel, 例如 R...G...B...; 一般情況影像的都是RGBRGB ...排列的,所以輸入時要做一個轉換.所以我的作法可能是先宣告一個空的Img,再利用提供的macro把值填入對應的channel

CImg<BYTE> img(m_width, m_height, 1, 3);
cimg_forXYC(img,x,y,v) { img(x,y,v) = body[(y*m_width+x)*img.spectrum()+(2-v)]; }
img.save_bmp("test.bmp");
<pre style='color:#000000;background:#f1f0f0;'><span style='color:#004a43; '>#</span><span style='color:#004a43; '>include </span><span style='color:#800000; '>"</span><span style='color:#40015a; '>CImg.h</span><span style='color:#800000; '>"</span>
<span style='color:#400000; font-weight:bold; '>using</span> namespace cimg_library<span style='color:#806030; '>;</span>
<span style='color:#400000; font-weight:bold; '>int</span> <span style='color:#800000; font-weight:bold; '>main</span><span style='color:#806030; '>(</span><span style='color:#806030; '>)</span>
<span style='color:#806030; '>{</span>
    <span style='color:#c34e00; '>// 640x400 的彩色圖像並定義3channel顏色</span>
    CImg<span style='color:#806030; '>&lt;</span><span style='color:#400000; font-weight:bold; '>unsigned</span> <span style='color:#400000; font-weight:bold; '>char</span><span style='color:#806030; '>></span> img<span style='color:#806030; '>(</span><span style='color:#c00000; '>640</span><span style='color:#806030; '>,</span> <span style='color:#c00000; '>400</span><span style='color:#806030; '>,</span> <span style='color:#c00000; '>1</span><span style='color:#806030; '>,</span> <span style='color:#c00000; '>3</span><span style='color:#806030; '>)</span><span style='color:#806030; '>;</span>  

    <span style='color:#c34e00; '>//將顏色全設定為黑色</span>
    img<span style='color:#806030; '>.</span>fill<span style='color:#806030; '>(</span><span style='color:#c00000; '>0</span><span style='color:#806030; '>)</span><span style='color:#806030; '>;</span>

    <span style='color:#c34e00; '>// 紫色</span>
    <span style='color:#400000; font-weight:bold; '>unsigned</span> <span style='color:#400000; font-weight:bold; '>char</span> purple<span style='color:#806030; '>[</span><span style='color:#806030; '>]</span> <span style='color:#806030; '>=</span> <span style='color:#806030; '>{</span> <span style='color:#c00000; '>255</span><span style='color:#806030; '>,</span> <span style='color:#c00000; '>0</span><span style='color:#806030; '>,</span> <span style='color:#c00000; '>255</span> <span style='color:#806030; '>}</span><span style='color:#806030; '>;</span>
    
    <span style='color:#c34e00; '>// 在座標(100, 100)輸出紫色的“Hello world”</span>
    img<span style='color:#806030; '>.</span>draw_text<span style='color:#806030; '>(</span><span style='color:#c00000; '>100</span><span style='color:#806030; '>,</span> <span style='color:#c00000; '>100</span><span style='color:#806030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#e60000; '>Hello World</span><span style='color:#800000; '>"</span><span style='color:#806030; '>,</span>purple<span style='color:#806030; '>)</span><span style='color:#806030; '>;</span>  
    
    <span style='color:#c34e00; '>// 透過CImgDisplay顯示一個視窗</span>
    img<span style='color:#806030; '>.</span>display<span style='color:#806030; '>(</span><span style='color:#800000; '>"</span><span style='color:#e60000; '>My first CImg code</span><span style='color:#800000; '>"</span><span style='color:#806030; '>)</span><span style='color:#806030; '>;</span>                  
    
    <span style='color:#400000; font-weight:bold; '>return</span> <span style='color:#c00000; '>0</span><span style='color:#806030; '>;</span>
<span style='color:#806030; '>}</span>
</pre>
arrow
arrow
    全站熱搜

    Person 發表在 痞客邦 留言(0) 人氣()