Ask Your Question
2

Removing selected rows from a Mat ?

asked 2013-05-24 11:08:30 -0600

yes123 gravatar image

updated 2020-11-15 02:08:07 -0600

For some reasons I need to filter out rows of a Mat. (I need to filter out some descriptors of ORB)

Which way do you suggest me? I haven't find any method to remove a single row from a Mat. So I was thinking I could iteratively inserting the good rows in a new Mat.

C++ pseudocode:

Mat desc;
ORB(myImage,cv::noArray,kp,desc);
matcher->match( myPreviousDesc, desc, matches );

for(auto i=0;i<matches.size();i++) {
   if (some conditions) {
      Remove the ith row of desc:
      erase( desc.row( matches[i].queryIdx ) );
   }
}

? How would you erase a single row from a Mat after checking for some conditions (or adding only the selected row in a new Mat) ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-05-24 11:28:11 -0600

berak gravatar image

updated 2013-05-24 11:29:22 -0600

  1. desc.row() is a copy. no cigar erasing that
  2. erase messes with the size(),so break out after that, or face doom.

you can't do it "inplace", so copy anything but the offending row(s) to a new Mat

edit flag offensive delete link more

Comments

how would you add iteratively the good rows?

yes123 gravatar imageyes123 ( 2013-05-24 12:17:52 -0600 )edit

mat.push_back(orig.row(i));

later:

mat.reshape(0,orig.cols);

berak gravatar imageberak ( 2013-05-24 12:24:42 -0600 )edit

to istanciate correctly the new Mat (for the first row) do i need to pass some params?

yes123 gravatar imageyes123 ( 2013-05-24 12:27:21 -0600 )edit

no, don't think so ;)

type will get inferred with 1st push_back(), rows/cols will get rearranged with reshape()

berak gravatar imageberak ( 2013-05-24 12:33:15 -0600 )edit

thanks a lot

yes123 gravatar imageyes123 ( 2013-05-24 12:58:33 -0600 )edit

hehe, you removed the suspicious comment, and i know, it sound pretty counterintuitive, but yes, look at reshape

move your ass ( cols ) and the rest will follow ..

berak gravatar imageberak ( 2013-05-24 13:01:03 -0600 )edit

@berak: berak, I am testing my code without .reshape and stuff seems to working properly even without the reshape, maybe its not needed?

yes123 gravatar imageyes123 ( 2013-05-25 03:51:21 -0600 )edit

oh,maybe not, depends on how you instanciated your mat.

berak gravatar imageberak ( 2013-05-25 03:57:47 -0600 )edit

@berak: I instanciated it with a simple: Mat myNewMat;

yes123 gravatar imageyes123 ( 2013-05-25 04:40:53 -0600 )edit

Maybe it's because when you insert the first row with push_back(firstRow) Mat infers the number of cols too?

yes123 gravatar imageyes123 ( 2013-05-25 04:53:06 -0600 )edit

Question Tools

Stats

Asked: 2013-05-24 11:08:30 -0600

Seen: 6,222 times

Last updated: May 24 '13