Skip to content

Instantly share code, notes, and snippets.

@ferryzhou
Last active April 9, 2017 20:43
Show Gist options
  • Save ferryzhou/5547842 to your computer and use it in GitHub Desktop.
Save ferryzhou/5547842 to your computer and use it in GitHub Desktop.
opencv extract and copy patch from a Mat or image
// nY(dst_r_from + (0:rows-1), dst_c_from + (0:cols-1), :) = YY(src_r_from + (0:rows-1), src_c_from + (0:cols-1), :);
void copy_patch(Mat& nY, const Mat& YY, int dst_r_from, int dst_c_from, int src_r_from, int src_c_from, int rows, int cols) {
int row_bytes = cols * YY.channels() * YY.elemSize1();
const uchar* pYY = (const uchar*)YY.data + YY.step[0] * src_r_from + YY.step[1] * src_c_from;
uchar* pnY = (uchar*)nY.data + nY.step[0] * dst_r_from + nY.step[1] * dst_c_from;
for (int i = 0; i < rows; i++, pnY += nY.step[0], pYY += YY.step[0]) {
memcpy(pnY, pYY, row_bytes);
}
}
// YY = Y(r_from:r_to-1, c_from:c_to-1, :);
void extract_patch(Mat& YY, const Mat& Y, int r_from, int r_to, int c_from, int c_to) {
int row_bytes = (c_to - c_from) * Y.channels() * Y.elemSize1();
YY.create(r_to - r_from, c_to - c_from, Y.type());
const uchar* pY = (const uchar*)Y.data + Y.step[0]*r_from + Y.step[1]*c_from;
uchar* pYY = (uchar*)YY.data;
for (int ri = r_from; ri < r_to; ri++, pYY += YY.step[0], pY += Y.step[0]) { // copy row by row
memcpy(pYY, pY, row_bytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment