Ask Your Question
1

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
1

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

Simone gravatar image

Hi,

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 !!!
     src.at<float>(0, i) /= src.at<float>(2, i);
     src.at<float>(1, i) /= src.at<float>(2, i);
}

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

Hope it helps.

S.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 1,146 times

Last updated: Nov 08 '12