An improvement on Jav_Rock will answer here, how I would do it.
Mat image = ...; vector<byte> v_char(image.rows * image.cols); for (int i = 0; i < image.rows; i++) memcpy(&v_char[i * image.cols], image.data + i * image.step, image.cols);
EDIT: Initialization by the constructor will allocate enough space to avoid additional redistribution, but it will also set all the elements in the default vector (0). The following code avoids this extra initialization.
Mat image = ...; vector<byte> v_char; v_char.reserve(image.rows * image.cols); for (int i = 0; i < image.rows; i++) { int segment_start = image.data + i * image.step; v_char.insert(v_char.end(), segment_start, segment_start + image.cols); }
source share