我知道很多關於這個參數的帖子,我已經閱讀了很多帖子,但是我仍然感到困惑。問題是類型(哎呀,該死的,這是C,我不得不處理數據類型!;-))。在cv :: Mat中使用C++ api訪問元素(x,y)
我使用新的過冷模板C++ API函數at
:
Mat mat32f(3, 3, CV_32F);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
mat32f.at<float>(i,j) = i;
cout << "Matrix of type: " << mat32f.type() << endl;
cout << mat32f << endl;
cout << mat32f.at<float>(1,2) << endl;
OK,這裏有浮動與1路,沒問題,輸出是明確的:
Matrix of type: 5
[0, 0, 0;
1, 1, 1;
2, 2, 2]
1
現在只是讓一些過濾器:
Mat mask;
inRange(mat32f, 1, 1, mask);
cout << "Mask of type: " << mask.type() << endl;
cout << mask << endl;
對於這裏,alles klar,輸出正是我想要的:
Mask of type: 0
[0, 0, 0;
255, 255, 255;
0, 0, 0]
現在我想檢查一下某個點(鼠標點擊嗎?不管)在範圍內。
int value2 = mask.at<char>(1,2);
cout << value2 << endl;
unsigned int value3 = mask.at<unsigned int>(1,2);
cout << value3 << endl;
我從第一臺計算機科學類,一char
是unsigned int
相同大小的(因爲我的面具型的是0 == CV_8U
)記得那麼使用字符。但cout
抓住它的字符,所以讓我看看一些無用的非ASCII符號,所以把它存儲在int
。
這裏的輸出中:
-1
256
發生了什麼?一團糟。爲什麼-1?或者,再說一次,爲什麼256?發生了什麼?你是從Mat<float>
得到什麼,當你使用at<char>
或at<unsigned int>
可能是不確定的行爲(無論是由C
// template methods for read-write or read-only element access.
// note that _Tp must match the actual matrix type -
// the functions do not do any on-fly type conversion
所以++:
char的大小與unsigned int的大小並不相同。 – juanchopanza 2012-04-26 14:14:28