無法理解,表達Vxy和Vxy_nocastOpenCV中的saturate_cast如何工作?
uchar init_m0[] = {10,10,30};
cv::Mat m0(3,1,CV_8UC1,init_m0,sizeof(uchar));
uchar& Vxy = m0.at<uchar>(0);
uchar& Vxy_nocast = m0.at<uchar>(1);
std::cout << m0 << std::endl;
Vxy = cv::saturate_cast<uchar>((Vxy-128)*2 + 128);
Vxy_nocast = (Vxy_nocast-128)*2 + 128;
std::cout << m0 << std::endl;
結果
[ 10;
10;
30]
[ 0;
148;
30]
飽和轉換將確保沒有發生整數溢出或下溢。通常情況下,如果你有一個無符號字符變量值200,並添加56(+ X),你會得到一個溢出,變量將有值0(+ X),這是很多算法非常糟糕。 – Micka
在這種情況下,我會說[code](https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/saturate.hpp#L102)是最好看的地方尋求答案。 –