比较两个OpenCV图像/2D Numpy数组 [英] Comparing two OpenCV images/2D Numpy arrays

查看:150
本文介绍了比较两个OpenCV图像/2D Numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用OpenCV,Python和Numpy的新手,但是已经有一段时间成为Java,C ++,C程序员了.

I am new to using OpenCV, Python and Numpy, but have been a Java, C++, C programmer for a while now.

我正在实现一个sigma-delta背景检测器,该检测器执行以下操作:

I am implementing a sigma-delta background detector, which does the following:

让i1为第一张图片,让i2为第二张图片

let i1 be first image, let i2 be second image

    for each pixel:
    if i1(x,y) > i2(x,y), then i2(x,y) = i2(x,y) + 1
    if i1(x,y) < i2(x,y), then i2(x,y) = i2(x,y) - 1

我基本上是在尝试遍历2D图像数组并将像素值与其他图像进行比较,但是我正在努力使用for循环将其与numpy数组一起使用.我曾尝试使用嵌套的for循环,但收到一条错误消息,说我无法访问该数组的元素.

I'm basically trying to iterate through the 2D image array and compare the pixel value with the other image, but I'm struggling to get that working with numpy arrays using for loops. I've tried using a nested for loop, but I get an error saying that I can't access the elements of that array.

for x in range (width):
    for y in range (height):
        if Mt [x,y] > It[x,y]:
            Mt [x,y] = Mt[x,y]+1
        elif Mt [x,y] < It[x,y]:
            Mt [x,y] = Mt[x,y]-1

这是可行的,但看起来并不优雅或高效.我希望有一个更快的解决方案...

This is working but doesn't seem very elegant or efficient. I am hoping for a faster solution...

任何建议都将受到欢迎

推荐答案

这是对代码进行矢量化的好地方,以进行解释和演示.

This is a great place to vectorize your code, for an explanation and demonstration.

#Generate two random arrays, shape must be the same
>>> Mt = np.random.rand(2,2)
>>> It = np.random.rand(2,2)
>>> Mt
array([[ 0.47961753,  0.74107574],
       [ 0.94540074,  0.05287875]])
>>> It
array([[ 0.86232671,  0.45408798],
       [ 0.99468912,  0.87005204]])

#Create a mask based on some condition
>>> mask = Mt > It
>>> mask
array([[False,  True],
       [False, False]], dtype=bool)

#Update in place
>>> Mt[mask]+=1
>>> Mt[~mask]-=1  #Numpy logical not
>>> Mt
array([[-0.52038247,  1.74107574],
       [-0.05459926, -0.94712125]])

您可能需要创建第二个遮罩,因为当前减法遮罩是Mt <= It而不是Mt < It,但是这是演示逻辑不的好地方.

You will probably need to create a second mask as currently the subtraction mask is Mt <= It not Mt < It, however it was a good place to demonstrate logical not.

要完全复制代码,请改用此代码:

To reproduce your code exactly use this instead:

Mt[Mt > It]+=1
Mt[Mt < It]-=1  


因为我对这些东西感兴趣:


Because I am interested in these things:

 def looper(Mt,It):
     for x in range (Mt.shape[0]):
         for y in range (Mt.shape[1]):
             if Mt [x,y] > It[x,y]:
                Mt [x,y] +=1
             elif Mt [x,y] < It[x,y]:
                Mt [x,y] -=1

nlooper = autojit(looper)

Mt = np.random.rand(500,500)
It = np.random.rand(500,500)

%timeit looper(Mt,It)
1 loops, best of 3: 531 ms per loop

%timeit Mt[Mt > It]+=1;Mt[Mt < It]-=1
100 loops, best of 3: 2.27 ms per loop

%timeit nlooper(Mt,It)
1 loops, best of 3: 308 µs per loop

autojit是来自 numba 模块的python/numpy的JIT编译器.

autojit is a JIT compiler for python/numpy from the numba module.

这篇关于比较两个OpenCV图像/2D Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆