C++ - 将 unsigned char* 复制到新的 unsigned char* 数组

标签 c++ arrays opencv std unsigned-char

这是我发布的问题的后续问题 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 变量的每一行,并将这些 un​​signed 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/

相关文章:

c++ - 使 TCHAR* 与 char* 兼容

c++ - constexpr 构造函数和函数的文字类编译错误(不同 vc、g++)

c++ - Objective-C 还是 C++?

php - 是否有更好的 PHP 方法可以从数组(字典)中按键获取默认值?

python - opencv避障想法-python

c++ - map 中c++ map 的简写语法

c - 在 C 中更新字符串

c - 在C编程中将元素插入到动态字符数组中

matlab - 使用 MATLAB 进行自动人脸检测

opencv - 有没有一种方法可以评估 OpenCV 中的图像有多少噪声?