Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Check Mat doc for details

BTW, you have to be sure that there is enough space in destination than... for a single row you can use assignment like this

if(dstMat.cols==srcMat.cols)
    dstMat.row(5) = srcMat.row(1); //copy src 2nd row to dst 6th

In general you can use ROI like here

Rect srcRect(0,1,srcMat.cols,1); //select  2nd row
Rect dstRect(0,5,srcMat.cols,1); //select  6th row
dstRect = dstRect & Rect(0,0,dstMat.size() ) ; //intersection to avoid out of range
srcMat(srcRect).copyTo(dstMat(dstRect));

as alternative you can use Range

srcMat.rowRange(1,2).copyTo(dstMat.rowRange(5, 6) ); //copy src 2nd row to dst 6th row

or more general range

//copy src 2nd row to dst 6th row
srcMat( Range(1, 2),Range::all() ).copyTo( dstMat(Range(5, 6), Range::all() ) )

Check Mat doc for details

BTW, you have to be sure that there is enough space space(size and data type) in destination than... destination.

Check for data type

if (srcMat.type() != dstMat.type())
    return;

for a single row you can use assignment like this

if(dstMat.cols==srcMat.cols)
    below (this dstMat.row(5) = srcMat.row(1); //copy src srcMat.row(1) will not work)

if (srcMat.cols == dstMat.cols)
    srcMat.row(1).copyTo(dstMat.row(5)); //copy the 2nd row to dst 6th
the 6th row

In general you can use ROI like here

Rect srcRect(0,1,srcMat.cols,1); srcRect(Point(0, 1), Size(srcMat.cols, 1)); //select  the 2nd row
Rect dstRect(0,5,srcMat.cols,1); //select  6th row
dstRect(Point(3, 5), srcRect.size() ); //destination in (3,5), size same as srcRect

dstRect = dstRect & Rect(0,0,dstMat.size() ) ; Rect(Point(0, 0), dstMat.size()); //intersection to avoid out of range
srcMat(srcRect).copyTo(dstMat(dstRect));

srcRect = Rect(srcRect.tl(), dstRect.size()); //adjust source size same as safe destination srcMat(srcRect).copyTo(dstMat(dstRect)); //copy from (0,1) to (3,5) max allowed cols

As alternative you can use Range

srcMat.rowRange(1,2).copyTo(dstMat.rowRange(5, 6) ); //copy src 2nd row to dst 6th row

or rowRange

if (srcMat.cols == dstMat.cols)
    srcMat.rowRange(1, 2).copyTo(dstMat.rowRange(5, 6)); //copy row: 2nd->6th

Or more general range

//copy src Range colRange = Range(0, min(srcMat.cols, dstMat.cols)); //select maximum allowed cols
//copy the 2nd row to dst the 6th row
srcMat( Range(1, 2),Range::all() ).copyTo( dstMat(Range(5, row  max allowed cols
srcMat(Range(1, 2), colRange).copyTo(dstMat(Range(5, 6), Range::all() ) )
colRange));