Skip to content

Slow speed using cv2.norm(img1,img2) with opencv > 4.2.0.34 #25928

@Scachi

Description

@Scachi

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 version newer than 4.2.0.34.
open-cv version 4.2.0.34 is the last one working fast.

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)

Even cv2.matchTemplate(img1, img2) is faster then cv2.norm(img, img2) on opencv-python v > 4.2.0.34

Any idea or fix to get the old speed back ?

Steps to reproduce

This is a small sample to show the speed difference.
My results:
using versions:
opencv-contrib-python 4.7.0.72
opencv-python 4.7.0.72
opencv-python-headless 4.7.0.72
"""
norm1c2 : cv2.norm( , ) : count = 1000
Real time: 1.45 seconds
CPU time: 0.08 seconds
Res: [2804.09022679371, 2804.09022679371, 0.0, 0.0]

norm1m2 : cv2.norm( - ) : count = 1000
Real time: 0.01 seconds
CPU time: 0.00 seconds
Res: [8761.552944541281, 7376.9984411005535, 0.0, 0.0]
"""

using versions
opencv-contrib-python 4.2.0.34
opencv-python 4.2.0.34
opencv-python-headless 4.2.0.34
"""
norm1c2 : cv2.norm( , ) : count = 1000
Real time: 0.00 seconds
CPU time: 0.00 seconds
Res: [2804.09022679371, 2804.09022679371, 0.0, 0.0]

norm1m2 : cv2.norm( - ) : count = 1000
Real time: 0.01 seconds
CPU time: 0.02 seconds
Res: [8761.552944541281, 7376.9984411005535, 0.0, 0.0]
"""

The sample program:

import cv2
import time

img1 = cv2.imread('./img1.png')
img2 = cv2.imread('./img2.png')
res = [0, 0, 0, 0]
cnt = 1000


def norm1c2():
    t1 = time.perf_counter(), time.process_time()
    for i in range(0, cnt):
        res[0] = cv2.norm(img1, img2)
        res[1] = cv2.norm(img2, img1)
        res[2] = cv2.norm(img1, img1)
        res[3] = cv2.norm(img2, img2)
    t2 = time.perf_counter(), time.process_time()

    print(f"\nnorm1c2 : cv2.norm( , ) : count = {cnt}")
    print(f" Real time: {t2[0] - t1[0]:.2f} seconds")
    print(f" CPU time: {t2[1] - t1[1]:.2f} seconds")
    print(f" Res: {res}")


def norm1m2():
    t1 = time.perf_counter(), time.process_time()

    for i in range(0, cnt):
        res[0] = cv2.norm(img1 - img2)
        res[1] = cv2.norm(img2 - img1)
        res[2] = cv2.norm(img1 - img1)
        res[3] = cv2.norm(img2 - img2)

    t2 = time.perf_counter(), time.process_time()
    print(f"\nnorm1m2 : cv2.norm( - ) : count = {cnt}")
    print(f" Real time: {t2[0] - t1[0]:.2f} seconds")
    print(f" CPU time: {t2[1] - t1[1]:.2f} seconds")
    print(f" Res: {res}")


norm1c2()
norm1m2()

img files attached
img1
img2

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions