Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between L2_NORM and NORM_MINMAX implementation in cv::Normalise()?

Tags:

c++

opencv

I was trying to replicate the Dense optical flow as discussed here : https://nanonets.com/blog/optical-flow/ .This is the snippet , I have used to process each frame for optical flow. The intution is that H->Angle of gradient,S->255,V->Norm value of Magnitude of gradient.The gradient is from output of calcOpticalFlowFarenback.

       cv::calcOpticalFlowFarneback(prevgray,gray,flow,0.5,3,15,3,5,1.2,0);
       cv::split(flow,channels);
       cv::cartToPolar(channels[0],channels[1],mag,angle);




       std::vector<cv::Mat> channels_m;
       cv::Mat c1 (Mask.size(),CV_8UC1);
       cv::Mat c0 (Mask.size(),CV_8UC1);
       cv::Mat c2 (Mask.size(),CV_8UC1);
       c1 = cv::Scalar::all(255);
       c0= angle*((180/3.14)/2);
       c0.convertTo(c0, CV_8U);
       cv::normalize(mag,c2,0,255,cv::NORM_MINMAX); <----- Line of interest
       c2.convertTo(c2, CV_8U);
       channels_m.push_back(c0);
       channels_m.push_back(c1);
       channels_m.push_back(c2);

       cv::merge(channels_m,Mask);
       cv::cvtColor(Mask,cflow,cv::COLOR_HSV2BGR_FULL);

when norm type cv::NORM_L2 is used , my entire V array of HSV is returning nothing but zero . However when changed to cv::NORM_MINMAX, It returns non zero values .I thought the actual function of cv::normalise is to normalise an array within the range alpha and beta . The Norm is just type of normalisation formula used to achieve the ranging objective. So why do we see a difference in output ?

Thanks in Advance !

like image 217
siva_uchiha Avatar asked Oct 20 '25 09:10

siva_uchiha


1 Answers

cv::NORM_L2 normalises your data such that if you took the entire image and converted it into one long vector, the magnitude of this vector is such that it becomes alpha. beta is ignored in the normalisation. Therefore, once you normalise by cv::NORM_L2, after you normalise if you were to consider this normalised input as one long vector, the L2 norm of this vector thus becomes alpha. Because you specified alpha = 0, it's not surprising that the output image is entirely 0 because you are specifying that the norm needs to be 0 after normalisation.

cv::NORM_MINMAX uses both alpha and beta such that the smallest value in the input array gets mapped to alpha and the largest value gets mapped to beta with all values in between scaled proportionally.

If you're wondering how I know this, the documentation for the function makes this very clear: https://docs.opencv.org/4.3.0/d2/de8/group__core__array.html#ga87eef7ee3970f86906d69a92cbf064bd

like image 50
rayryeng Avatar answered Oct 21 '25 22:10

rayryeng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!