Iterate matrix and copy out [closed]

asked 2014-01-14 03:06:43 -0600

Deus gravatar image

Hi all, Hi all, I have a problem with some matrix operations. I have one matrix MatOriginal and I want copy out into another one, only the range from (0,0) to (300,300). So I try to iterate the original one (MatOriginal) and copy out, my code is:

a = 0; b = 0;

    for(int x = 0; x < 300; x++)
    {
        for(int y = 0; y < 300; y++)
        {
            MatNew.at<int>(a,b) = MatOriginal.at<int>(x,y);
            a++;
        }

        b++;
    }

But this doesn't works... anyone can help me please?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-26 15:26:24.800420

Comments

2

2 things wrong with your mat.at() there:

  • it's (y,x) not (x,y)
  • the type inside the braces has to match the Mat type, so either Mat.at<uchar> or Mat.at<Vec3b>

in the end, you don't need those loops at all, you can just copy one region to another:

MatOriginal.copyTo( MatNew( Rect(0,0,300,300) );

berak gravatar imageberak ( 2014-01-14 03:19:50 -0600 )edit