如何将一阶图像矩m01和m10设置为零,以使用Zernike矩实现平移不变性。

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

How to set first order image moments m01 and m10 to zero to achieve translation invariance with Zernike moments

问题

以下是您提供的代码部分的翻译:

import numpy as np
import cv2

image = np.zeros([9,9])

image[2,0] = 1
image[3,0] = 1
image[4,0] = 1
image[2,1] = 1
image[3,1] = 1
image[4,1] = 1
image[2,2] = 1
image[3,2] = 1
image[4,2] = 1

print(image)

M = cv2.moments(image)
m00 = M['m00']
m10 = M['m10']
m01 = M['m01']
x = m10 / m00
y = m01 / m00
print(M)
print(f"centroid is {x},{y}")

image_2 = np.zeros([9,9])

image_2[3,3] = 1
image_2[4,3] = 1
image_2[5,3] = 1
image_2[3,4] = 1
image_2[4,4] = 1
image_2[5,4] = 1
image_2[3,5] = 1
image_2[4,5] = 1
image_2[5,5] = 1

print(image_2)
M = cv2.moments(image_2)
m00 = M['m00']
m10 = M['m10']
m01 = M['m01']
x = m10 / m00
y = m01 / m00
print(M)
print(f"centroid is {x},{y}")

请注意,这段代码演示了如何计算图像的矩和中心位置。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

I am struggling to understand the methodology in the paper 'Invariant image recognition by Zernike moments' to achieve translational invariance of Zernike moments.

It is stated "Translation invariancy is achieved by transforming the image into a new one whose first order moments, m01 and m10, are both equal to zero. This is done by transforming the original f(x, y) image into another one which is f(x+ X, y + J ), where X and J are the centroid location of the original image computed from" raw moments M10, M01 and M00. "In other words, the origin is moved to the centroid before moment calculation".

I have plotted a 3x3 square of 1's in an array of zeros.

[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]

I calculate the moments: M00 = 9.0, M10 = 9.0, M01 = 27.0. Hence centroid (x,y) = (1.0,3.0)

I transform the image by translating the shape +1 in the x and + 3 in the y using the values from the centroid of the first image.
I calculate the moments of the transformed image: M00 = 9.0, M10 = 36.0, M01 = 36.0. Hence (x,y) = (4.0,4.0)

[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]

The resulting M10 and M01 are equal, but not equal to zero, and the object is now in the centre of the array. From my understanding the only way to achieve M10 or M01 equalling to zero is to have no pixels in the image. As M10 = sum of the x coordinates where f(x,y) = 1, and M01 = sum of the y coordinates where f(x,y) = 1.

If I apply this process to each image in a set, have I normalised correctly despite not equalling zero? If not, what am I misunderstanding here?

I have included my crude code in python to illustrate below.

import numpy as np
import cv2

image = np.zeros([9,9])

image[2,0] = 1
image[3,0] = 1
image[4,0] = 1
image[2,1] = 1
image[3,1] = 1
image[4,1] = 1
image[2,2] = 1
image[3,2] = 1
image[4,2] = 1

print(image)

M = cv2.moments(image)
m00 = M['m00']
m10 = M['m10']
m01 = M['m01']
x = m10 / m00
y = m01 / m00
print(M)
print(f"centroid is {x},{y}")

image_2 = np.zeros([9,9])

image_2[3,3] = 1
image_2[4,3] = 1
image_2[5,3] = 1
image_2[3,4] = 1
image_2[4,4] = 1
image_2[5,4] = 1
image_2[3,5] = 1
image_2[4,5] = 1
image_2[5,5] = 1

print(image_2)
M = cv2.moments(image_2)
m00 = M['m00']
m10 = M['m10']
m01 = M['m01']
x = m10 / m00
y = m01 / m00
print(M)
print(f"centroid is {x},{y}")

答案1

得分: 1

在Cris的回答之后,我意识到A00和A11泽尼克矩并不需要包括,因为根据坐标缩放使质心为0,0的定义,它们自然为零。不需要进行图像处理或对图像进行调整。下面的代码计算从轮廓中的泽尼克矩,直到8阶,将存储矩的列表中的前两个矩A00和A11删除。通过计算两个相同形状之间的矩的集合之间的欧氏距离,这些形状已经通过转换它们的图像矩而相同。

z_Moments = mahotas.features.zernike_moments(thresh, cv2.minEnclosingCircle(cnts)[1], degree=8)
z_Moments = z_Moments[2:]

希望这对您有帮助。

英文:

After answer from Cris, realised that the A00 and A11 zernike moments do not need to be included as they are by definition zero after adjusting the coordinate scale such that the centroid is 0,0. No image processing or adjustments to the image are required. Code below calculates zernike moments from a contour up to order 8, the first 2 moments A00 and A11 are removed from the list storing the moments. By computing the Euclidean distance between sets of moments between two identical shapes which have been translated their image moments are the same.

z_Moments = mahotas.features.zernike_moments(thresh, cv2.minEnclosingCircle(cnts)[1], degree=8)
z_Moments = z_Moments[2:]

答案2

得分: 0

我不确定我是否曾阅读过您所指的论文,但它可能存在不清晰的语言。

这个想法是改变图像的坐标系统,使其原点位于对象的质心。也就是说,您改变坐标系统,使M01和M10变为0。您不改变图像本身,只是在矩的计算中使用的坐标系统。

在这个翻译后的坐标系统中计算更高阶矩时,我们将计算中心矩,这些矩是平移不变的。您可以通过修改矩的计算方式以获得中心矩来实现相同的效果。这个维基百科页面有一个相当清晰的解释,说明如何计算中心矩。

在这个翻译后的坐标系统中计算泽尼克矩时,计算出的矩将独立于图像中对象的位置,即它们是平移不变的。

英文:

I'm not sure if I've ever read the paper you refer to, but it might have unclear language.

The idea is to change the coordinate system of the image to have its origin at the centroid of the object. That is, you change the coordinate system such that M01 and M10 become 0. You don't change the image itself, only the coordinate system used in the calculations of the moments.

When computing higher order moments in this translated coordinate system, we will compute the central moments, which are translation invariant. You can accomplish the same thing by modifying the computation of the moments such that you get central moments. This Wikipedia page has a fairly clear explanation for how to compute central moments.

When computing Zernike moments in this translated coordinate system, the computed moments will be independent of the location of the object in the image, i.e. they are translation invariant.

huangapple
  • 本文由 发表于 2023年3月31日 22:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899519.html
匿名

发表评论

匿名网友

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

确定