我有一個cv :: Mat的雙倍圖像,我已經截斷了0.0和4095.0之間。我想能夠轉換這個矩陣/創建一個新的矩陣,基於這個是12位的。 (保存0 - > 4095個整數值所需的最小int大小)。我只能得到原始緩衝區,但是我不確定矩陣內數據的格式。以OpenCV Mat <doube>並轉換爲12位值數組。
手動我可以執行以下操作:
cv::Mat new_matrix(/*type CV_8UC3, size (matrix.rows, matrix.cols/2)*/);
for(int i = 0; i < matrix.rows; ++i){
for(int j = 0; j < matrix.cols; ++j){
std::uint16_t upper_half = static_cast<std::uint16_t>(matrix.at<double>(j*2,i));
std::uint16_t lower_half = static_cast<std::uint16_t>(matrix.at<double>(j*2+1,i));
std::uint8_t first_byte = static_cast<std::uint8_t>(upper_half>>4);
std::uint8_t second_byte = static_cast<std::uint8_t>(upper_half<<4) | static_cast<std::uint8_t>(lower_half <<12>> 12);
std::uint8_t third_byte = static_cast<std::uint8_t>(lower_half>>4);
new_matrix.at<cv::Vec3B>(j, i) = cv::Vec3b(first_byte, second_byte, third_byte);
}
}
其基本上壓縮兩個雙值以一個用於上半部分,一個用於下,提取三個字節出來的(12 + 12 = 24,24/8 = 3)轉換成3字節的矩陣。我不能確定,如果內存佈局將匹配出打包12位的但是(我有一個偶數的cols的,所以劃分的cols/2是不是一個問題),我不知道如何確保這種服從字節序。
I might even be able to use a custom data type,但我需要確保,如果說我做了一個聯盟結構的12位類型或某事的元素是不能填補。轉換後
注意,我不打算再使用OpenCV中的12位值,然後我需要提取原始值,他們得到發送到另一個單獨的進程。
我可能會最終接受這個答案,但我不會需要實現這一點,直到後來,我將給予好評這個現在。 – snb
爲什麼'struct PackedSamples {int A:12; int B:12};'?如果(目標)字節順序是big-endian,你不需要寫'{char highA; char highB:4; char lowA:4; char lowB; }'? – chtz