0

This is a followup question to to the question I posted here, although it different enough to warrant posting a new question.

I have a OpenCV (cv::Mat) matrix variable (which is just an unsigned char * of with a known size). I used the answer in the referenced question to copy a std::string to my unsigned char* array using the code:

int initial_offset = 10;
unsigned char *r_record = new unsigned char[1024]();
std::copy(title.begin(), title.end(), r_record + initial_offset);

This works as expected. But now I would like to loop through each row of the cv::Mat variable and insert those unsigned char values into the same r_record array.

I have the code:

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
}

But I'm not sure what goes in place of the ??? to reference the i_row data. Can this be done?

Also, apparently using new unsigned char[1024]() is a bad idea, but I'm not sure how to replace it with something not bad.

Update:

Looks like the response from @juanchopanza below isn't quite working. Here is what I have:

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);

But no matter what, r_record only contains: "Title 1". Does anyone have any ideas?

Community
  • 1
  • 1
Brett
  • 10,971
  • 27
  • 114
  • 198

1 Answers1

2

Something like this:

std::copy(img.begin<unsigned char>(), img.end<unsigned char>(), r_record + data_offset);
juanchopanza
  • 210,243
  • 27
  • 363
  • 452
  • With this, is there any reason to loop through each row? It doesn't look like it (which makes this solution better). – Brett Jan 26 '14 at 21:32
  • @Brett This would copy the elements as required. No need for loops over rows. – juanchopanza Jan 26 '14 at 21:33
  • I'm not getting the expected results. I updated the post with some more of my code. Any ideas? – Brett Jan 26 '14 at 22:07
  • How do you check the contents of `r_record`? – juanchopanza Jan 26 '14 at 22:12
  • I'm using Xcode as my IDE, and I just do a `print r_record` in the debugger (after setting a breakpoint). Not sure it's ideal, but it does output `"Title 1"` and nothing else. – Brett Jan 26 '14 at 22:14
  • @Brett no idea. Check the contents of the `cv::Mat` object. And loop over all `1024` entries of `r_record`, printing out each character. – juanchopanza Jan 26 '14 at 22:17
  • Contents are correct in `cv::Mat`. I'm finding an oddity when adding `33` to the index. When I set it to a value less than the length of "Title 1" (ie., less than 7), it works. Over 7 and it doesn't. Would `std::copy()` resize `r_record` after the title insertion? – Brett Jan 26 '14 at 22:22
  • @Brett No, copy doesn't resize anything. Just loop over the contents of `r_record` as I said. The problem could be that you have a `0` after `Title 1`, and whatever you are using to print interprets that as the termination of the string. – juanchopanza Jan 26 '14 at 22:23
  • Just ran a loop over the 1024 elements. They are all there are expected, so the code works. It's odd that I couldn't see this in Xcode though. Thanks for your input. Answer accepted! – Brett Jan 26 '14 at 22:25