Ask Your Question
1

copy selected row of a matrix to another

asked 2013-01-24 15:21:41 -0600

southpark gravatar image

updated 2013-01-25 11:10:49 -0600

Hi,

Thanks for answering my previous question. I have one more related issue.

I am still a little confused with the at operator for unknown type matrix.

 // copy select rows into a new matrix
 Mat indices; // n*1 matrix storing the row index, up to index 165600
 Mat dst;
 Mat src; // n*2 matrix

  void copy_matrix(Mat &src, Mat &dst, Mat &indices) {
  for (int i=0; i<indices.rows; i++) {
    // now copy the row vector
        Mat M1;
    src.row(indices.at<unsigned int>(i,1)).copyTo(M1);
    dst.push_back(M1);
   }
  }

The above code can pass the debug procedure. However, when I run the code, it gives the following error. The matrix indices has the data type int ( 0 to 4294967295). Therefore, the return type should be correct. I don't know which part goes wrong. I also tried at<int>, at<uchar> or other data types. But the error remains. Much appreciated for your help.

  Error: assertion failed, <dims <=2 && data && <unsigned> i0 << unsigned >size.p[0]

Update:

The error has disappeared. However, the program still crashes in the middle. And the debug shows error in memcpy.asm

UnwindUp2: mov eax,[esi+ecx*4-8] ;U(entry)/V(not) - get dword from source ;V(entry) - spare

What was the underlying reason for this issue?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-01-25 16:21:27 -0600

AlexanderShishkov gravatar image

updated 2013-01-26 04:55:06 -0600

My version of the function:

void copy_matrix(const Mat &src, Mat &dst, const Mat &indices) {
    for (int i=0; i<indices.rows; i++) {
        dst.push_back(src.row(indices.at<int>(i)));
    }
}

Code sample:

    Mat indices(3,1,CV_32SC1); // n*1 matrix storing the row index, up to index 165600
    indices.at<int>(0,0) = 3;
    indices.at<int>(1,0) = 4;
    indices.at<int>(2,0) = 9;


    Mat dst;
    Mat src(100,2,CV_32SC1); // n*2 matrix
    for (int i = 0; i < src.rows; i++)
    {
        src.at<int>(i,0) = i;
        src.at<int>(i,1) = i+1;
    }
    copy_matrix(src, dst, indices);

As Daniil said, you should use 0-based numeration. Also if your matrix has only one row or only one column you can use one index in "at" operator (see my variant of function). Also I can't understand why do you use "unsigned int" type for "at" operator. OpenCV doesn't support this type. In my opinion it is impossible to create a matrix with this type, because we don't have CV_32U depth variant. If you don't know type of your matrix "indices" you should use depth() or type() methods to detect it.

edit flag offensive delete link more

Comments

Thanks for your help. Your function runs pretty well. Later, I figured out the program was due to the generation of src matrix in the previous part of code. This part of code accidentally violate the address access. That causes the memory problem. Now I replace this part and everything works well.

I agree that there is not type as CV_32U.

southpark gravatar imagesouthpark ( 2013-01-26 16:14:40 -0600 )edit
2

answered 2013-01-24 22:47:46 -0600

Daniil Osokin gravatar image

Hi! You should use zero-based index for column numeration, so, as soon as indices has nx1 dimensionality, use:

indices.at< unsigned int >(i,0)
About appropriate Mat usage see docs.

edit flag offensive delete link more

Comments

Thanks, Dniil. That was a silly mistake I made. The error has disappeared. However, the program still crashes in the middle. And the debug shows error in memcpy.asm

UnwindUp2: mov eax,[esi+ecx*4-8] ;U(entry)/V(not) - get dword from source ;V(entry) - spare

What was the underlying reason for this issue?

southpark gravatar imagesouthpark ( 2013-01-25 09:38:20 -0600 )edit

Question Tools

Stats

Asked: 2013-01-24 15:21:41 -0600

Seen: 7,471 times

Last updated: Jan 26 '13