这是我发布的问题的后续问题 here ,尽管它的不同足以保证发布一个新问题。
我有一个 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);
关于C++ - 将 unsigned char* 复制到新的 unsigned char* 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369415/