How to Compute Hu-Moments of an Image in OpenCV Python?
If you’re interested in computer vision and image processing, you may have heard of the Hu-Moments. They are a set of seven mathematical features that are used to describe the shape of an object in an image. These features are invariant to translation, scale, and rotation, which makes them very useful for pattern recognition and image matching. In this article, we will show you how to calculate Hu-Moments in OpenCV Python.
First, let’s briefly explain what Hu-Moments are. In the field of computer vision, moments are used to describe the overall shape and distribution of an object in an image. Moments are calculated by performing a mathematical operation on the intensities of the pixels in the image. Hu-Moments are a set of seven normalized and scale-invariant moments that are calculated from the raw moments of an image. These moments can be used to describe the shape of an object in a way that is invariant to translation, scale, and rotation.
To compute Hu-Moments in OpenCV Python, you first need to import the necessary libraries:
import cv2
import numpy as np
Next, you need to load the image you want to compute Hu-Moments on.
You can load this image using the following code:
img = cv2.imread("path/to/image.png")
Once you have loaded the image, you need to convert it to grayscale. This is because Hu-Moments are only computed on grayscale images. You can convert the image to grayscale using the following code:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Now that you have your grayscale image, you can compute the Hu-Moments. To do this, you first need to calculate the raw moments of the image using the cv2.moments()
function. This function takes in a grayscale image and returns a dictionary of moments.
moments = cv2.moments(gray)
The raw moments that are returned by the cv2.moments()
function are used to compute the seven Hu-Moments. You can calculate the Hu-Moments using the cv2.HuMoments()
function. This function takes in the dictionary of raw moments and returns an array of seven Hu-Moments.
huMoments = cv2.HuMoments(moments)
Congratulations! You have now computed the Hu-Moments of your image. The huMoments
array contains the seven Hu-Moments, which you can print using the following code:
for i in range(0,7):
print("Hu-Moment ", i+1, ": ", huMoments[i])
The output of this code should look like this:
Hu-Moment 1 : [ 1.27323205e-03]
Hu-Moment 2 : [ 1.54559681e-07]
Hu-Moment 3 : [ 2.54646152e-10]
Hu-Moment 4 : [ 3.80616348e-14]
Hu-Moment 5 : [-2.53255859e-26]
Hu-Moment 6 : [ 2.57121871e-19]
Hu-Moment 7 : [-1.12114686e-33]
These values represent the seven Hu-Moments of the image. It’s important to note that Hu-Moments are scale-invariant, which means that they are not affected by the size of the object in the image. This makes them very useful for pattern recognition and image matching.
Here is the complete code to compute Hu-Moments of an image in OpenCV Python:
import cv2
import numpy as np
img = cv2.imread("path/to/image.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
moments = cv2.moments(gray)
huMoments = cv2.HuMoments(moments)
for i in range(0,7):
print("Hu-Moment ", i+1, ": ", huMoments[i])
Conclusion
Hu-Moments are a powerful tool in computer vision and image processing. They allow us to describe the overall shape of an object in a way that is invariant to translation, scale, and rotation. With the help of OpenCV Python, we can easily calculate the Hu-Moments of an image and use them for pattern recognition and image matchingtasks. The process of computing Hu-Moments in OpenCV Python involves converting the image to grayscale, calculating the raw moments, and then using the cv2.HuMoments()
function to compute the seven Hu-Moments. From there, you can print out the values of the Hu-Moments to analyze and compare them.
It’s important to note that while Hu-Moments are a powerful tool, they are just one part of the larger field of computer vision and image processing. There are many other techniques and algorithms that can be used to extract features from images and perform various tasks.
In conclusion, computing Hu-Moments in OpenCV Python is an essential process for anyone studying or working in the field of computer vision and image processing. They are a valuable tool that can be used for a wide range of applications, from object recognition to image matching. By using the techniques outlined in this article, you can easily compute the seven Hu-Moments of any grayscale image.