First time here? Check out the FAQ!

Ask Your Question
2

Removing selected rows from a Mat ?

asked May 24 '13

yes123 gravatar image

updated Nov 15 '0

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) ?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
3

answered May 24 '13

berak gravatar image

updated May 24 '13

  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

Preview: (hide)

Comments

how would you add iteratively the good rows?

yes123 gravatar imageyes123 (May 24 '13)edit

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

later:

mat.reshape(0,orig.cols);

berak gravatar imageberak (May 24 '13)edit

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

yes123 gravatar imageyes123 (May 24 '13)edit

no, don't think so ;)

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

berak gravatar imageberak (May 24 '13)edit

thanks a lot

yes123 gravatar imageyes123 (May 24 '13)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 (May 24 '13)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 (May 25 '13)edit

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

berak gravatar imageberak (May 25 '13)edit

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

yes123 gravatar imageyes123 (May 25 '13)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 (May 25 '13)edit

Question Tools

Stats

Asked: May 24 '13

Seen: 6,248 times

Last updated: May 24 '13