OpenCV之格式化輸出方法(C 實現)

2021-10-11 02:10:39 字數 1489 閱讀 8462

目錄

1、opencv預設風格

2、python風格

3、csv逗號分隔風格

4、numpy風格

5、c語言風格

opencv提供了不同風格的格式化輸出方法。為了方便演示,首先用randu()函式產生隨機數來填充mat矩陣,需要給定乙個上限和乙個下限來確保隨機數在期望的範圍內:

mat r(3, 4, cv_8uc2);

randu(r, scalar::all(0), scalar::all(255));

初始化完r矩陣,下面對輸出風格進行講解。

**:

cout << " opencv預設風格:" << r << endl;
輸出:

opencv預設風格:[ 91,   2,  79, 179,  52, 205, 236,   8;

181, 239,  26, 248, 207, 218,  45, 183;

158, 101, 102,  18, 118,  68, 210, 139]

**:

cout << "python風格:" << format(r, formatter::fmt_python) << endl;
輸出:

python風格:[[[ 91,   2], [ 79, 179], [ 52, 205], [236,   8]],

[[181, 239], [ 26, 248], [207, 218], [ 45, 183]],

[[158, 101], [102,  18], [118,  68], [210, 139]]]

**:

cout << "csv風格:" << format(r, formatter::fmt_csv) << endl;
輸出:

csv風格: 91,   2,  79, 179,  52, 205, 236,   8

181, 239,  26, 248, 207, 218,  45, 183

158, 101, 102,  18, 118,  68, 210, 139

**:

cout << "numpy風格:" << format(r, formatter::fmt_numpy) << endl;
輸出:

numpy風格:array([[[ 91,   2], [ 79, 179], [ 52, 205], [236,   8]],

[[181, 239], [ 26, 248], [207, 218], [ 45, 183]],

[[158, 101], [102,  18], [118,  68], [210, 139]]], dtype='uint8')

**:

cout << "c語言風格:" << format(r, formatter::fmt_c) << endl;
輸出:

c語言風格:

python格式化輸出方法。

1.format 方法。字串型別格式化採用format 方法,基本使用格式是 模板字串 format 逗號分隔的引數 呼叫format 方法後會返回乙個新的字串,引數從0 開始編號。a int input b int input print n n format a b a b a b format...

C 基礎之格式化輸出

在c語言裡面,使用printf 函式格式化輸出。例如printf d f 2.5f a,b,c 但是在c 裡面就尷尬了,cout a b c endl 輸出的都是預設格式,這些往往不是我們需要的格式。在c 裡面也提供了格式化輸出的方法,但是方法比c語言稍微麻煩。c 格式化輸出需要引入標頭檔案 inc...

C 格式化輸出

int a 12345678 格式為sring輸出 label1.text string.format asdfadsfadsfasdf a label2.text asdfadsf a.tostring adsfasdf label1.text string.format asdfadsfadsf...