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

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

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.

  1. img = PIL.Image.open("SR1.png").convert("L")
  2. im = numpy.array(img)
  3. img2 = PIL.Image.open("SR61.png").convert("L")
  4. im2 = numpy.array(img2)
  5. np.array(im,dtype=float)
  6. np.array(im2,dtype=float)
  7. import math
  8. import cmath
  9. def correlationCoefficient(X, Y, n) :
  10. sum_X = 0
  11. sum_Y = 0
  12. sum_XY = 0
  13. squareSum_X = 0
  14. squareSum_Y = 0
  15. i = 0
  16. for i in range(n) :
  17. sum_X = sum_X + X[i]
  18. sum_Y = sum_Y + Y[i]
  19. sum_XY = sum_XY + X[i] * Y[i]
  20. squareSum_X = squareSum_X + X[i] * X[i]
  21. squareSum_Y = squareSum_Y + Y[i] * Y[i]
  22. i = i + 1
  23. corr = (n * sum_XY - sum_X * sum_Y)/(cmath.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y)))
  24. return corr
  25. X = im.flatten()
  26. Y = im2.flatten()
  27. n = len(X)
  28. 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.

  1. img = PIL.Image.open("SR1.png").convert("L")
  2. im = numpy.array(img)
  3. img2 = PIL.Image.open("SR61.png").convert("L")
  4. im2 = numpy.array(img2)
  5. np.array(im,dtype=float)
  6. np.array(im2,dtype=float)
  7. import math
  8. import cmath
  9. def correlationCoefficient(X, Y, n) :
  10. sum_X = 0
  11. sum_Y = 0
  12. sum_XY = 0
  13. squareSum_X = 0
  14. squareSum_Y = 0
  15. i = 0
  16. for i in range(n) :
  17. sum_X = sum_X + X[i]
  18. sum_Y = sum_Y + Y[i]
  19. sum_XY = sum_XY + X[i] * Y[i]
  20. squareSum_X = squareSum_X + X[i] * X[i]
  21. squareSum_Y = squareSum_Y + Y[i] * Y[i]
  22. i = i + 1
  23. corr = (n * sum_XY - sum_X * sum_Y)/(cmath.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y)))
  24. return corr
  25. X = im.flatten()
  26. Y = im2.flatten()
  27. n = len(X)
  28. print ('{0:.6f}'.format(correlationCoefficient(X, Y, n)))

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

答案1

得分: 2

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

  1. 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:

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

答案2

得分: 2

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

  1. import numpy as np
  2. def correlationCoefficient(X, Y):
  3. n = X.size
  4. sum_X = X.sum()
  5. sum_Y = Y.sum()
  6. sum_XY = (X*Y).sum()
  7. squareSum_X = (X*X).sum()
  8. squareSum_Y = (Y*Y).sum()
  9. corr = (n * sum_XY - sum_X * sum_Y)/(np.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y)))
  10. return corr

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

  1. from PIL import Image
  2. img1 = Image.open("1.jpg").convert("L")
  3. im1 = np.array(img1)/255
  4. img2 = Image.open("2.jpg").convert("L")
  5. im2 = np.array(img2)/255
  6. print ('{0:.6f}'.format(correlationCoefficient(im1, im2)))
英文:

Here's a vectorized version of your function:

  1. import numpy as np
  2. def correlationCoefficient(X, Y):
  3. n = X.size
  4. sum_X = X.sum()
  5. sum_Y = Y.sum()
  6. sum_XY = (X*Y).sum()
  7. squareSum_X = (X*X).sum()
  8. squareSum_Y = (Y*Y).sum()
  9. corr = (n * sum_XY - sum_X * sum_Y)/(np.sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y)))
  10. return corr

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

  1. from PIL import Image
  2. img1 = Image.open("1.jpg").convert("L")
  3. im1 = np.array(img1)/255
  4. img2 = Image.open("2.jpg").convert("L")
  5. im2 = np.array(img2)/255
  6. 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:

确定