Domanda

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?

È stato utile?

Soluzione

Something like this:

std::copy(img.begin<unsigned char>(), img.end<unsigned char>(), r_record + data_offset);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top