- 從 C++ 端呼叫 Python 的一個 Function。
- 將從 C++ 用 OpenCV 讀進來的 Mat 資料結構轉換成 Numpy Array。
- 把 Numpy Array 當成參數傳進 Python Function。
Google Code Prettify
2017年9月2日 星期六
[C++] Pass an OpenCV Mat Image to a Python Function
最近因工作需要而研究了一陣子 C++ 與 Python 的互動,主要達成以下幾件事:
2015年8月10日 星期一
Image Features 轉換為 HTK File Format
鑑於最近研究用HMM進行手勢影像辨識,接觸了HTK Toolkit。該工具原是主要用於Speech Recognition,但只要將影像特徵轉換為適合的 File Format 則亦可進行 HMM 的訓練及使用。
要轉換的 feature vectors 基本上以如下格式儲存:
每行代表一個 feature vector ,檔頭的 135 表示每個 feature vector 的維度為 135 。
轉換的 code 是基於 LIACS 的 code 做一點修改,使之能處理小數的特徵值,並可將數張影像的 feature vectors 一次轉換為 HTK Format 。
以下只呈現修改過的部份:
frames 表示欲轉換的 feature vectors 數量(即影片 frame 數);
HTK_WriteHeader 的參數 3 為 feature vector 維度 * sizeof(float),參數 4 為HTK_USER 表示自訂特徵類型;
變數 x type 改為 float ,方可接受小數特徵值。
欲驗證輸出的 HTK File Format 是否正確,可使用 HTK Tools 裡的 HList:
參考文獻
要轉換的 feature vectors 基本上以如下格式儲存:
135 1 0.0328217 0.0100089 0.000778311 0 4.92805e-005 5.75842e-007 1 0.0777054 0.129443 0.0319161 0.0646412 0.0244341 0.0152372 0.00485908 0.0127946 0.0031298 0.00783502 0.0125285 0.00126586 0.0105069 0.00283716 0.00630043 0.0025311 0.00454009 0.00269783 0.00262739 0.00419814 0.00102193 0.00319554 0.00344698 0.00145266 0.00241564 0.00231722 0.00144811 0.000919769 0.00106583 0.00170757 0.00113995 0.00193437 0.00134729 0.000975564 0.00163894 0.00160577 0.0010904 0.000848026 0.00160142 0.00176397 0.00176891 0.00204451 0.000736588 0.000142591 0.000819708 0.000699803 0.000852419 0.000318257 0.0004559 0.00084382 0.00070141 0.000193684 0.000341556 0.00121724 0.00114435 0.000771485 0.00137698 0.00106348 0.001131 0.00066602 0.00113994 0.000994646 0.000689515 0.000134377 0.000611667 0.000816581 7.27596e-012 0.000455511 0.000862932 0.000542742 0.000790653 0.00015376 0.000667005 0.00185994 0.00118799 0.00195221 0.00048036 0.00203206 0.0017036 0.00115241 0.00159665 0.000456289 0.00245261 0.00115743 0.00212574 0.000759982 0.00161923 0.000970768 0.000446351 0.0025218 0.000559381 0.00170117 0.00145245 0.00123497 0.00162679 0.0014485 0.00113292 0.00180706 0.00172861 0.00261046 0.000791285 0.00385214 0.00245815 0.0020356 0.00148777 0.00122579 0.00334875 0.00294233 0.00460947 0.0024289 0.00304412 0.00934439 0.00441411 0.00604666 0.00464733 0.010632 0.0119068 0.0184638 0.0183619 0.0177572 0.0191143 0.027884 0.0404363 0.0691513 0.0750528 0.167004 0.469292 ...
每行代表一個 feature vector ,檔頭的 135 表示每個 feature vector 的維度為 135 。
轉換的 code 是基於 LIACS 的 code 做一點修改,使之能處理小數的特徵值,並可將數張影像的 feature vectors 一次轉換為 HTK Format 。
以下只呈現修改過的部份:
float reverse_float( const float inFloat )
{
float retVal;
char *floatToConvert = ( char* ) & inFloat;
char *returnFloat = ( char* ) & retVal;
// swap the bytes into a temporary buffer
returnFloat[0] = floatToConvert[3];
returnFloat[1] = floatToConvert[2];
returnFloat[2] = floatToConvert[1];
returnFloat[3] = floatToConvert[0];
return retVal;
}
int main(int argc, char* argv[])
{
...
int number, frames = 61;
int i, j; float x;
...
/* Write the binary HTK header file */
HTK_WriteHeader(out,(unsigned int) number, (unsigned int) 1, (short) number * sizeof(float), (short) HTK_USER);
for (j = 0; j < frames; ++j) {
/* Write the data samples with 2 bytes per sample */
for (i=0; i<number; i++)
{
fscanf(in, "%f", &x);
printf("%d: %f\n",i,x);
//x = byte_swap_short(x);
x = reverse_float(x);
fwrite(&(x),sizeof(float),1,out);
}
}
fclose(out);
fclose(in);
/* Test the HTK I/O */
//HTK_test(argv[2]);
}
return 0;
}
frames 表示欲轉換的 feature vectors 數量(即影片 frame 數);
HTK_WriteHeader 的參數 3 為 feature vector 維度 * sizeof(float),參數 4 為HTK_USER 表示自訂特徵類型;
變數 x type 改為 float ,方可接受小數特徵值。
欲驗證輸出的 HTK File Format 是否正確,可使用 HTK Tools 裡的 HList:
- hlist -h filename > log.txt
2013年4月3日 星期三
2013年3月19日 星期二
2013年3月18日 星期一
OpenCV 顯示影像(Display Image)
環境設定請參考:Visual Studio 2012 安裝 OpenCV 2.4.3
範例程式:C:\OpenCV2.4\samples\cpp\tutorial_code\introduction\display_image\display_image.cpp (內文的程式碼有稍作修改)
範例程式:C:\OpenCV2.4\samples\cpp\tutorial_code\introduction\display_image\display_image.cpp (內文的程式碼有稍作修改)
2013年2月2日 星期六
2012年12月6日 星期四
2011年1月7日 星期五
2011年1月4日 星期二
C - Linked List (鏈結串列) Part 1
作業要求:練習用結構指標做出一鏈結串列並對其做運算。
這作業我搞了整個週末才寫出個雛形,說寫了超過24小時一點也不為過。
由於第一次寫這麼大結構的程式,故程式碼有些冗長且重複使用性極低。僅供參考。
這作業我搞了整個週末才寫出個雛形,說寫了超過24小時一點也不為過。
由於第一次寫這麼大結構的程式,故程式碼有些冗長且重複使用性極低。僅供參考。
2010年12月19日 星期日
C - 矩陣運算
運算的內容包括:加法(1&4)、乘法(2&5)及反矩陣(3&6)。
作業要求:
使用一維指標來模擬二維陣列。
使用Command line argument list選擇尺寸、算法、輸入方式。
作業要求:
使用一維指標來模擬二維陣列。
使用Command line argument list選擇尺寸、算法、輸入方式。
2010年12月18日 星期六
2010年12月17日 星期五
C - 文章排版程式(行處理)
這次作業要求我們製作兩個Header File,及一個主程式,並使用Makefile來compile在一起。
共有六個檔:
word.h, line.h, word.c, line.c, justify.c, Makefile
共有六個檔:
word.h, line.h, word.c, line.c, justify.c, Makefile
2010年11月17日 星期三
2010年10月27日 星期三
訂閱:
文章 (Atom)