Note that, sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) is the same as saying sqrt( (a - b).x * (a - b).x + (a - b).y * (a - b).y ), so you can call cv::norm(a - b).

However, according to this (albeit old) link, performance is poor for single point pairs.

I just ran the test code on my machine. It generates 15,000 points and calculates the distance from each to the remaining points.

a : [0, 0] - b : [2.14748e+09, 2.14748e+09]

euclideanDist : 3.037e+09
distanceBtwPoints : 3.037e+09
cv::norm : 3.037e+09

max_distance euclideanDist : 3.02456e+09 time passed :0.165179
max_distance distanceBtwPoints : 3.02456e+09 time passed :0.259471
max_distance cv::norm : 3.02456e+09 time passed :0.26728

Surprising. The fastest code is

float euclideanDist(cv::Point2f& a, cv::Point2f& b)
{
    cv::Point2f diff = a - b;
    return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
}

Using cv::norm(a - b) and the code below are nearly equal:

static double distanceBtwPoints(const cv::Point2f &a, const cv::Point2f &b)
{
    double xDiff = a.x - b.x;
    double yDiff = a.y - b.y;

    return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
}

But that's evidently because of the casts to double. If left as float, distanceBtwPoints is as fast as eucledianDist.

Answer from darda on Stack Overflow
🌐
OpenCV
docs.opencv.org › 4.13.0 › d2 › de8 › group__core__array.html
OpenCV: Operations on arrays
This version of cv::norm calculates the absolute difference norm or the relative difference norm of arrays src1 and src2.
Top answer
1 of 3
2

Note that, sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) is the same as saying sqrt( (a - b).x * (a - b).x + (a - b).y * (a - b).y ), so you can call cv::norm(a - b).

However, according to this (albeit old) link, performance is poor for single point pairs.

I just ran the test code on my machine. It generates 15,000 points and calculates the distance from each to the remaining points.

a : [0, 0] - b : [2.14748e+09, 2.14748e+09]

euclideanDist : 3.037e+09
distanceBtwPoints : 3.037e+09
cv::norm : 3.037e+09

max_distance euclideanDist : 3.02456e+09 time passed :0.165179
max_distance distanceBtwPoints : 3.02456e+09 time passed :0.259471
max_distance cv::norm : 3.02456e+09 time passed :0.26728

Surprising. The fastest code is

float euclideanDist(cv::Point2f& a, cv::Point2f& b)
{
    cv::Point2f diff = a - b;
    return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
}

Using cv::norm(a - b) and the code below are nearly equal:

static double distanceBtwPoints(const cv::Point2f &a, const cv::Point2f &b)
{
    double xDiff = a.x - b.x;
    double yDiff = a.y - b.y;

    return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
}

But that's evidently because of the casts to double. If left as float, distanceBtwPoints is as fast as eucledianDist.

2 of 3
1

As you can read in the doc, the norm() set of functions all operate on the generic InputArray datatype. So, from here, you can see that this can be either a cv::Mat or std::vector but certainly not cv::Point2f.

After checking in the sources (of 3.0, but I doubt it was different in 2.4 series), I see that that function has 9 overloads, but the only ones with cv::Point<> only have one argument.

So you will need to to convert your points to cv::Mat. This can be done easily, as there is a constructor for cv::Mat that takes a point as argument. So just do:

std::cout << cv::norm( cv::Mat(a), cv::Mat(b) ) << std::endl;

But as a side comment, I am not sure that this is the best solution: it implies additional memory allocation, and it is probably faster to compute the distance directly (i.e.: sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ). It seems harsh, but you can rely on the compiler to optimize that.

Discussions

Slow speed using cv2.norm(img1,img2) with opencv > 4.2.0.34
System Information OpencCV python version: 4.7.072 Operating System: Windows 11 pro Python version: 3.8.19 Detailed description I am used to use cv2.norm(img1, img2) to compare images. Using that function got slow with any opencv-python ... More on github.com
🌐 github.com
13
July 17, 2024
cv2.normalize with NORM_MINMAX produces small values below set minimum on float32
OpenCV python version: 4.10.0 Operating System / Platform: macOS 15.1.1 arm64 M2 Python version: 3.9.6 (/usr/bin/python3 provided by macOS) The following code shows unexpected small negative numbers when trying to normalize an image between 0 and 1 while a comparison, which is mathematically ... More on github.com
🌐 github.com
25
December 7, 2024
🌐
Amroamroamro
amroamroamro.github.io › mexopencv › matlab › cv.norm.html
cv.norm - mexopencv
cv.norm - Calculates absolute array norm, absolute difference norm, or relative difference norm
🌐
GitHub
github.com › opencv › opencv › issues › 25928
Slow speed using cv2.norm(img1,img2) with opencv > 4.2.0.34 · Issue #25928 · opencv/opencv
July 17, 2024 - I tested various opencv-python versions (4.10.0.84, 4.8.0.76, 4.7.072 with python 3.8.19 and also tested python 3.10.14 without any improvement. But I need to stay with python 3.8.19 and opencv version >= 4.7 for compatibility reasons with other libs. cv2.norm(img1 - img2) works faster than cv2.norm(img1, img2) on opencv-python v > 4.2.0.34 but is still slower than 4.2.0.34 with cv2.norm(img1, img2)
Author: opencv
🌐
OpenCV
forum.opencv.org › c++
MINMAX normalization on CV_32F image does not seem to work (all outputs are 0) - C++ - OpenCV
May 25, 2023 - Hello, I have a CV_32FC1 image img containing values in the approximate range -1e-04, 1e-03. I want to visualize this image, so I normalize it as follows cv::Mat disp; cv::normalize(img, disp, 0, float_max, cv::NORM_MINMAX, CV_32FC1); where float_max is the maximum value a float can hold.
🌐
Rust
docs.rs › opencv › latest › opencv › core › fn.normalize.html
normalize in opencv::core - Rust
opencv:: core · Source · pub fn normalize( src: &impl ToInputArray, dst: &mut impl ToInputOutputArray, alpha: f64, beta: f64, norm_type: i32, dtype: i32, mask: &impl ToInputArray, ) -> Result<()> Expand description · Normalizes the norm or value range of an array.
🌐
Comfy
comfy.icu › extensions › opencv › nodes › opencv norm_2
OpenCV norm_2 - ComfyUI Cloud
OpenCV norm_2 is a ComfyUI node in the image/OpenCV category from OpenCV. Inputs: src1, src2, normType, mask. Outputs: FLOAT. Run it in the cloud, no install…
Find elsewhere
🌐
OpenCV
docs.opencv.org › 3.4.20 › dc › d84 › group__core__basic.html
OpenCV: Basic structures
Usage flags for allocator · makePtr<T>(...) is equivalent to Ptr<T>(new T(...)). It is shorter than the latter, and it's marginally safer than using a constructor or Ptr::reset, since it ensures that the owned pointer is new and thus not owned by any other Ptr instance.
🌐
Cornor's Blog
wjddyd66.github.io › opencv › OpenCV(3)
OpenCV-기본연산 - Cornor’s Blog
October 23, 2019 - OpenCV_Python은 영상을 numpy.ndarray로 표현하기 때문에 numpy연산을 사용할 수 있다. ... [[255 255 255 ... 255 255 255] [255 255 255 ... 255 255 255] [255 255 255 ... 255 255 255] ... [255 255 255 ... 255 255 255] [255 255 255 ... 255 255 255] [255 255 255 ... 255 255 255]] ... cv2.normalize(src,dst[,alpha[,beta[,norm_type[,dtype[,mask]]]]]): 지정한 범위의 정규화 parameter
🌐
GitHub
github.com › opencv › opencv › issues › 26588
cv2.normalize with NORM_MINMAX produces small values below set minimum on float32 · Issue #26588 · opencv/opencv
December 7, 2024 - OpenCV python version: 4.10.0 Operating System / Platform: macOS 15.1.1 arm64 M2 Python version: 3.9.6 (/usr/bin/python3 provided by macOS) The following code shows unexpected small negative numbers when trying to normalize an image between 0 and 1 while a comparison, which is mathematically ...
Author: opencv
🌐
OpenCV
docs.opencv.org › 4.13.0 › d2 › d75 › namespacecv.html
OpenCV: cv Namespace Reference
Loading · Searching · No Matches · Namespaces | Classes | Typedefs | Enumerations | Functions | Variables · cv Namespace Reference · Core functionality » Utility and system functions and macros | Core functionality » Utility and system functions and macros » Logging facilities
🌐
OpenCV
docs.opencv.org › 3.4 › d4 › d7f › structcv_1_1L1.html
OpenCV: cv::L1< T > Struct Template Reference
Public Types | Public Member Functions | List of all members · cv::L1< T > Struct Template Reference · #include <opencv2/features2d.hpp> · Member Typedef Documentation · ◆ ResultType · template<class T > · ◆ ValueType · Member Enumeration Documentation · ◆ anonymous enum
🌐
Alibaba Cloud
topic.alibabacloud.com › a › opencv-the-block-distance-from-l1-to-l2-is-originally-here-cvnorm-_8_8_32027957.html
Opencv-the block distance from L1 to L2 is originally here! CvNorm ()
# Include "stdafx. h "<br/> # include <cv. h> <br/> # include <pighgui. h> </p> <p> float Array1 [] = {1,-}; <br/> float Array2 [] = {, 2 }; <br/> // uchar MaskArray [] = {1, 1, 0}; </p> <p> int main () <br/>{ <br/> CvMat * Mat1 = cvCreateMat (1, 3, CV_32FC1); <br/> CvMat * Mat2 = cvCreateMat (1, 3, CV_32FC1 ); <br/> cvSetData (Mat1, Array1, Mat1-> step); <br/> cvSetData (Mat2, Array2, Mat2-> step ); </p> <p> printf ("1-norm is: %. 1f/n ", cvNorm (Mat2, Mat1, CV_L1); <br/> // printf (" 1-norm + mask is: %. 1f/n ", cvNorm (Mat2, NULL, CV_L1, Mat1); <br/> printf (" 2-norm is: %. 3e/n ", cvNorm (
🌐
PLOS
journals.plos.org › plosone › article
CMAP-Fusion: A cross-modal feature selection and model pruning framework for laboratory and imaging data | PLOS One
April 24, 2026 - The hardware environment for this experiment uses an Intel Xeon Gold 6248 CPU (2.5GHz, 20 cores, 40 threads), two NVIDIA RTX 3090 GPUs (24GB VRAM), and 128GB of DDR4 memory. The software environment is based on the Ubuntu 20.04 operating system, using the PyTorch 2.0 deep learning framework and the Python 3.9 programming language. Core dependencies include Transformers 4.30, OpenCV 4.8, Scikit-learn 1.2, and thop 0.1.1.
🌐
OpenCV
docs.opencv.org › 4.13.0 › dc › dbb › tutorial_py_calibration.html
OpenCV: Camera Calibration
Given the intrinsic, distortion, rotation and translation matrices, we must first transform the object point to image point using cv.projectPoints(). Then, we can calculate the absolute norm between what we got with our transformation and the corner finding algorithm.
🌐
OpenCV
docs.opencv.org › 3.4.20 › d9 › d0c › group__calib3d.html
OpenCV: Camera Calibration and 3D Reconstruction
Line coefficients are defined up to a scale. They are normalized so that \(a_i^2+b_i^2=1\) .