OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sat, 26 Jan 2013 16:14:40 -0600copy selected row of a matrix to anotherhttp://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/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?Thu, 24 Jan 2013 15:21:41 -0600http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/Answer by AlexanderShishkov for <p>Hi,</p>
<p>Thanks for answering my previous question. I have one more related issue. </p>
<p>I am still a little confused with the at operator for unknown type matrix. </p>
<pre><code> // 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);
}
}
</code></pre>
<p>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.</p>
<pre><code> Error: assertion failed, <dims <=2 && data && <unsigned> i0 << unsigned >size.p[0]
</code></pre>
<p><strong>Update:</strong></p>
<p>The error has disappeared. However, the program still crashes in the middle. And the debug shows error in memcpy.asm</p>
<p>UnwindUp2: mov eax,[esi+ecx*4-8] ;U(entry)/V(not) - get dword from source ;V(entry) - spare</p>
<p>What was the underlying reason for this issue?</p>
http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?answer=6503#post-id-6503My 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. Fri, 25 Jan 2013 16:21:27 -0600http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?answer=6503#post-id-6503Comment by southpark for <p>My version of the function:</p>
<pre><code>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></pre>
<p>Code sample:</p>
<pre><code> 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);
</code></pre>
<p>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. </p>
http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?comment=6531#post-id-6531Thanks 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. Sat, 26 Jan 2013 16:14:40 -0600http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?comment=6531#post-id-6531Answer by Daniil Osokin for <p>Hi,</p>
<p>Thanks for answering my previous question. I have one more related issue. </p>
<p>I am still a little confused with the at operator for unknown type matrix. </p>
<pre><code> // 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);
}
}
</code></pre>
<p>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.</p>
<pre><code> Error: assertion failed, <dims <=2 && data && <unsigned> i0 << unsigned >size.p[0]
</code></pre>
<p><strong>Update:</strong></p>
<p>The error has disappeared. However, the program still crashes in the middle. And the debug shows error in memcpy.asm</p>
<p>UnwindUp2: mov eax,[esi+ecx*4-8] ;U(entry)/V(not) - get dword from source ;V(entry) - spare</p>
<p>What was the underlying reason for this issue?</p>
http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?answer=6467#post-id-6467Hi!
You should use zero-based index for column numeration, so, as soon as `indices` has `nx1` dimensionality, use: <pre><code>indices.at< unsigned int >(i,0)</code></pre> About appropriate `Mat` usage see [docs](http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=mat#mat).Thu, 24 Jan 2013 22:47:46 -0600http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?answer=6467#post-id-6467Comment by southpark for <p>Hi!
You should use zero-based index for column numeration, so, as soon as <code>indices</code> has <code>nx1</code> dimensionality, use: </p><pre><code>indices.at< unsigned int >(i,0)</code></pre> About appropriate <code>Mat</code> usage see <a href="http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=mat#mat">docs</a>.<p></p>
http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?comment=6493#post-id-6493Thanks, 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?Fri, 25 Jan 2013 09:38:20 -0600http://answers.opencv.org/question/6460/copy-selected-row-of-a-matrix-to-another/?comment=6493#post-id-6493