Ask Your Question
1

copy small part of mat to another

asked 2015-12-18 13:13:21 -0600

ggeo gravatar image

updated 2015-12-18 13:57:04 -0600

pklab gravatar image

I can't figure how to copy a part of mat to another mat. ( or to itself)

I want for example to copy (or to keep) only the first row of srcMat.

I am using :

    for ( int x = 0; x < height; x++ )
    {
        for ( int y = 0; y < width; y++ )
        {
            destMat = scrMat.at<float>(0,y);
           // or destMat.at<float>(x,y) = scrMat.at<float>(0,y);
        }
    }

I am not sure about using:

            srcMat.copyTo( destMat( Rect( y, 0, srcMat.cols, srcMat.rows ) ) );
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-12-18 13:52:22 -0600

pklab gravatar image

updated 2015-12-19 04:01:06 -0600

Check Mat doc for details

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

Check for data type

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

for a single row you can use assignment like below (this dstMat.row(5) = srcMat.row(1) will not work)

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

In general you can use ROI like here

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

dstRect = dstRect & Rect(Point(0, 0), dstMat.size()); //intersection to avoid out of range
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 rowRange

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

Or more general range

Range colRange = Range(0, min(srcMat.cols, dstMat.cols)); //select maximum allowed cols
//copy the 2nd row to the 6th row  max allowed cols
srcMat(Range(1, 2), colRange).copyTo(dstMat(Range(5, 6), colRange));
edit flag offensive delete link more

Comments

Thanks for the help.I am trying the last methid you described but I am receiving assertion failed...m.dims >= 2 in function Mat. My src mat has the same size as dst.But , keep in mind that I want to copy(keep) only a row and all columns from src to the dest.So, what about the size of dest?Just be the same as src?

ggeo gravatar imageggeo ( 2015-12-18 14:39:15 -0600 )edit

The truth is that I have vector<Mat> src and vector<Mat> dst;Both , with .resize(N).I tried copy dst[0]= src[0];but it gives me floating point exception. ( src is a filled vector and dst empty).Thanks!

ggeo gravatar imageggeo ( 2015-12-18 14:55:28 -0600 )edit
1

@pklab careful, first assigment with row() will not work

LorenaGdL gravatar imageLorenaGdL ( 2015-12-18 15:19:05 -0600 )edit

Thanks to @LorenaGdL copyTo should be used with row assignment ! I edited my answer with more details.

@ggeo using dst.resize(N) you are allocating N Mat headers for Mat(0,0,CV_8UC1). Than dst[0] is a valid Mat header. If src[0] is a valid Mat header too your assignment should work fine even if both Mats are empty. There is no reason to have floating point exception here, use debug to locate where is the exception.

Remember that dst[0]= src[0] means that dst[0] points to src[0]... if you change dst[0] also src[0] will change and reverse.

pklab gravatar imagepklab ( 2015-12-19 04:27:20 -0600 )edit

@pklab:Ok,thanks for the help!

ggeo gravatar imageggeo ( 2015-12-19 04:39:26 -0600 )edit

@pklab this is not working in ios

govi2010 gravatar imagegovi2010 ( 2019-07-22 05:27:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-18 13:13:21 -0600

Seen: 29,385 times

Last updated: Dec 19 '15