OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Tue, 22 Mar 2016 15:48:19 -0500Normalize vectorhttp://answers.opencv.org/question/90659/normalize-vector/Hello
I have column vectors stored in a matrix M. How do i normalize each vector ? I have tried
normalize(M.col(i), M.col(i));
reduce(M, sum, 0, CV_REDUCE_SUM, CV_32FC1);
But the results do not give me a 1 for every column. Help thanksTue, 22 Mar 2016 06:49:19 -0500http://answers.opencv.org/question/90659/normalize-vector/Answer by Tetragramm for <p>Hello</p>
<p>I have column vectors stored in a matrix M. How do i normalize each vector ? I have tried </p>
<p>normalize(M.col(i), M.col(i));
reduce(M, sum, 0, CV_REDUCE_SUM, CV_32FC1);</p>
<p>But the results do not give me a 1 for every column. Help thanks</p>
http://answers.opencv.org/question/90659/normalize-vector/?answer=90661#post-id-90661Try multiply(M.col(i), 1.0/(sum(M.col(i))(0)), M.col(i));
This takes the column, finds the sum, and multiplies the column by 1/sum. Then use the reduce to check.
If you want to use the normalize function, you need to change the norm type to NORM_L1, and it will do the same thing. Actually, just do this, that's better. [Here's the documentation](http://docs.opencv.org/3.1.0/d2/de8/group__core__array.html#ga87eef7ee3970f86906d69a92cbf064bd&gsc.tab=0) on the normalize function so you can see why NORM_L2 wasn't working and why NORM_L1 will.Tue, 22 Mar 2016 06:58:32 -0500http://answers.opencv.org/question/90659/normalize-vector/?answer=90661#post-id-90661Comment by Tetragramm for <p>Try multiply(M.col(i), 1.0/(sum(M.col(i))(0)), M.col(i));</p>
<p>This takes the column, finds the sum, and multiplies the column by 1/sum. Then use the reduce to check.</p>
<p>If you want to use the normalize function, you need to change the norm type to NORM_L1, and it will do the same thing. Actually, just do this, that's better. <a href="http://docs.opencv.org/3.1.0/d2/de8/group__core__array.html#ga87eef7ee3970f86906d69a92cbf064bd&gsc.tab=0">Here's the documentation</a> on the normalize function so you can see why NORM_L2 wasn't working and why NORM_L1 will.</p>
http://answers.opencv.org/question/90659/normalize-vector/?comment=90719#post-id-90719The sum() function returns a cv::Scalar, which is four doubles together. The first contains the sum of the first channel, the second, channel 2, ect. Since you have one channel, you want the first double, which is index 0.
But you should use normalize with NORM_L1, that's more clear, and possibly a bit faster.Tue, 22 Mar 2016 15:48:19 -0500http://answers.opencv.org/question/90659/normalize-vector/?comment=90719#post-id-90719Comment by Nbb for <p>Try multiply(M.col(i), 1.0/(sum(M.col(i))(0)), M.col(i));</p>
<p>This takes the column, finds the sum, and multiplies the column by 1/sum. Then use the reduce to check.</p>
<p>If you want to use the normalize function, you need to change the norm type to NORM_L1, and it will do the same thing. Actually, just do this, that's better. <a href="http://docs.opencv.org/3.1.0/d2/de8/group__core__array.html#ga87eef7ee3970f86906d69a92cbf064bd&gsc.tab=0">Here's the documentation</a> on the normalize function so you can see why NORM_L2 wasn't working and why NORM_L1 will.</p>
http://answers.opencv.org/question/90659/normalize-vector/?comment=90665#post-id-90665Hello thanks ! I did not know such a function existed. What is the use of (0) in sum(M.col(i))(0) ? I looked at http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html but it only specifies the input..Tue, 22 Mar 2016 07:34:41 -0500http://answers.opencv.org/question/90659/normalize-vector/?comment=90665#post-id-90665