![](/img/trans.png)
[英]Copying generic values to unsigned char array and copying back in C++
[英]C++ - Copying unsigned char* to a new unsigned char* array
這是我在這里發布的問題的后續問題,盡管它有足夠的不同以保證發布新問題。
我有一個OpenCV
( cv::Mat
)矩陣變量(它只是一個已知大小的unsigned char *
)。 我使用引用的問題中的答案使用代碼將std::string
復制到我的unsigned char*
數組:
int initial_offset = 10;
unsigned char *r_record = new unsigned char[1024]();
std::copy(title.begin(), title.end(), r_record + initial_offset);
這按預期工作。 但是現在我想循環遍歷cv::Mat
變量的每一行,並將那些unsigned char值插入到同一個r_record
數組中。
我有代碼:
int data_offset = TITLE_OFFSET + 1;
cv::Mat &img = <from somewhere else>
for (int row=0; row<=img.rows; row++) {
unsigned char* i_row = img.row(row).data;
std::copy(???, ???, r_record + data_offset);
data_offset += img.cols; //This happens to be += 32 in every case that I am using
}
但我不知道是什么代替???
引用i_row
數據。 可以這樣做嗎?
此外,使用顯然是new unsigned char[1024]()
是一個壞主意,但我不知道如何用不壞的東西取代它。
更新:
看起來下面@juanchopanza的回復不太合適。 這是我有的:
std::string = "Title 1";
cv::Mat mat = <from somewhere else>;
//Test case
desc.data[1] = (unsigned char)65;
//Write the image information to the database
unsigned char *image_record = new unsigned char[1024]();
std::copy(title.begin(), title.end(), r_record + title_offset);
std::copy(mat.begin<unsigned char>(), mat.end<unsigned char>(), r_record + 33);
但無論如何, r_record
只包含: "Title 1"
。 有沒有人有任何想法?
像這樣的東西:
std::copy(img.begin<unsigned char>(), img.end<unsigned char>(), r_record + data_offset);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.