如何找到两幅图像之间的相关性。

huangapple go评论81阅读模式
英文:

How to find correlation between two images

问题

I need to find correlation between two images, using numpy, but basic math only. I have the problem: "IndexError: index 5434 is out of bounds for axis 0 with size 5434". And i have a code. Tell me what to do, pls.

img = PIL.Image.open("SR1.png").convert("L")
im = numpy.array(img)
img2 = PIL.Image.open("SR61.png").convert("L")
im2 = numpy.array(img2)
np.array(im,dtype=float)
np.array(im2,dtype=float)
import math
import cmath
def correlationCoefficient(X, Y, n) : 
    sum_X = 0
    sum_Y = 0
    sum_XY = 0
    squareSum_X = 0
    squareSum_Y = 0


    i = 0
    for i in range(n) : 
        sum_X = sum_X + X[i]
        sum_Y = sum_Y + Y[i] 
        sum_XY = sum_XY + X[i] * Y[i] 
        squareSum_X = squareSum_X + X[i] * X[i] 
        squareSum_Y = squareSum_Y + Y[i] * Y[i] 

        i = i + 1

    corr = (n * sum_XY - sum_X * sum_Y)/(cmath.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y))) 
    return corr


X = im.flatten()
Y = im2.flatten()


n = len(X) 


print ('{0:.6f}'.format(correlationCoefficient(X, Y, n)))

Please note that I've provided the code without the translation parts as requested. If you have any specific questions or need further assistance, please let me know.

英文:

I need to find correlation between two images, using numpy, but basic math only. I have the problem:"***
IndexError: index 5434 is out of bounds for axis 0 with size 5434***". And i have a code. Tell me what to do, pls.

img = PIL.Image.open("SR1.png").convert("L")
im = numpy.array(img)
img2 = PIL.Image.open("SR61.png").convert("L")
im2 = numpy.array(img2)
np.array(im,dtype=float)
np.array(im2,dtype=float)
import math
import cmath
def correlationCoefficient(X, Y, n) : 
    sum_X = 0
    sum_Y = 0
    sum_XY = 0
    squareSum_X = 0
    squareSum_Y = 0
  
  
    i = 0
    for i in range(n) : 
        sum_X = sum_X + X[i]
        sum_Y = sum_Y + Y[i] 
        sum_XY = sum_XY + X[i] * Y[i] 
        squareSum_X = squareSum_X + X[i] * X[i] 
        squareSum_Y = squareSum_Y + Y[i] * Y[i] 
      
        i = i + 1

    corr = (n * sum_XY - sum_X * sum_Y)/(cmath.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y))) 
    return corr
 
  
X = im.flatten()
Y = im2.flatten()
   

n = len(X) 


print ('{0:.6f}'.format(correlationCoefficient(X, Y, n))) 

如何找到两幅图像之间的相关性。

答案1

得分: 2

你可以使用numpy中的corrcoef函数来计算皮尔逊相关系数。首先需要将两个图像数组都使用flatten函数展平:

np.corrcoef(im1.flatten(), im2.flatten())
英文:

You can use the function corrcoef in numpy to find Peason correlation. First you need to flatten both image arrays:

np.corrcoef(im1.flatten(), im2.flatten())

答案2

得分: 2

以下是您功能的矢量化版本:

import numpy as np

def correlationCoefficient(X, Y):
    n = X.size
    sum_X = X.sum()
    sum_Y = Y.sum()
    sum_XY = (X*Y).sum()
    squareSum_X = (X*X).sum()
    squareSum_Y = (Y*Y).sum()
    corr = (n * sum_XY - sum_X * sum_Y)/(np.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y))) 
    return corr

还重要的是要对图像数组进行归一化以避免溢出:

from PIL import Image

img1 = Image.open("1.jpg").convert("L")
im1 = np.array(img1)/255

img2 = Image.open("2.jpg").convert("L")
im2 = np.array(img2)/255

print ('{0:.6f}'.format(correlationCoefficient(im1, im2)))
英文:

Here's a vectorized version of your function:

import numpy as np

def correlationCoefficient(X, Y):
    n = X.size
    sum_X = X.sum()
    sum_Y = Y.sum()
    sum_XY = (X*Y).sum()
    squareSum_X = (X*X).sum()
    squareSum_Y = (Y*Y).sum()
    corr = (n * sum_XY - sum_X * sum_Y)/(np.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y))) 
    return corr

It is also important to normalize your image arrays to avoid overflow:

from PIL import Image

img1 = Image.open("1.jpg").convert("L")
im1 = np.array(img1)/255

img2 = Image.open("2.jpg").convert("L")
im2 = np.array(img2)/255

print ('{0:.6f}'.format(correlationCoefficient(im1, im2))) 

huangapple
  • 本文由 发表于 2020年1月6日 15:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定