Ask Your Question

cv::Mat divide by last row.

asked 2012-11-08 11:08:13 -0500

Hyon Lim gravatar image

Hi. How can I write a OpenCV code for following.

if there is a 3xN vector, divide all elements by last row.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-11-08 11:30:48 -0500

Simone gravatar image


I don't know if it is particularly efficient but you can do the following using repeat

Mat src ...;  // your 3XN matrix
Mat lastRow = src.row( 2 );
Mat tmp;
repeat(lastRow, tmp, 3, 1 );  // create a tmp matrix of 3xN whose elements are repeated elements of the last row of src
src = src / tmp;

This is like doing

src = src./repmat(src(3,:), 3, 1)

in matlab. Otherwise another way to do it is to pass element by element and do the division

for(int i =0; i<src.cols; ++i )
     // you should first check that the last element is !=0 !!!<float>(0, i) /=<float>(2, i);<float>(1, i) /=<float>(2, i);

and replace <float> with the correct type of your matrix.

Hope it helps.


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2012-11-08 11:08:13 -0500

Seen: 1,557 times

Last updated: Nov 08 '12