How to Compute Image Moments in OpenCV Python?


Image Moments are very important to compute the features like center of mass of an object, area of an object, etc., in a given image. Image moments are computed for an object using the contour of the object. So first, we detect the contour of the object, then apply cv2.moments(cnt) function to compute the moments.

Syntax

This is the syntax used for the function −

cv2.moments(cnt)

Where, "cnt" is a numpy array of the contour points of an object in the image.

Steps

You can use the following steps to compute the moments in an image −

Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.

import cv2

Read the input image using cv2.imread() and convert it to grayscale.

img = cv2.imread('shape.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Apply thresholding on the grayscale image to create a binary image.

ret,thresh = cv2.threshold(gray,150,255,0)

Find the contours in the image using cv2.findContours() function.

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

Find the moments of the contours using cv2.moments(cnt) function.

cnt = contours[0]
M = cv2.moments(cnt)

Draw the contours on the input image.

cv2.drawContours(img, [cnt], -1, (0,255,255), 3)

Print the Moments for the contours and display the image with the drawn contours.

print("Moments of first contour:", M)
cv2.imshow("Contour", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Let's have a look at some examples for more clear understanding.

We will use the following image as the Input File in the examples below.

Example 1

In the Python 3 program below, we detect the contours in the image and find the moments for the first contour. We also draw the first contour on the image.

# import the required libraries import cv2 # Read the input image img = cv2.imread('shape.png') # Convert the input image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding on gray image ret,thresh = cv2.threshold(gray,150,255,0) # Find the contours in the image contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) print("Number of Contours detected:",len(contours)) # Find the moments of first contour cnt = contours[0] M = cv2.moments(cnt) # Draw the contour cv2.drawContours(img, [cnt], -1, (0,255,255), 3) x1, y1 = cnt[0,0] cv2.putText(img, 'Contour:1', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) # print the moments of the first contour print("Moments of first contour:", M) cv2.imshow("Contour", img) cv2.waitKey(0) cv2.destroyAllWindows()

Output

On execution, the program will produce the following output −

Number of Contours detected: 3
Moments of first contour: {'m00': 14947.0, 'm10': 5239300.5, 'm01': 4905869.833333333, 'm20': 1855054841.3333333, 
'm11': 1719626880.3333333, 'm02': 1627236449.0, 'm30': 663246399000.25, 'm21': 608855981883.0333, 
'm12': 570384732497.0, 'm03': 545278150361.85004, 'mu20': 18547868.074468374, 'mu11': -4234.319657325745, 
'mu02': 17043178.00180459, 'mu30': -103085.66333007812, 'mu21': -2867085.318628311, 'mu12': 93785.43371105194, 
'mu03': 2614635.8901367188, 'nu20': 0.08302061174329703, 'nu11': -1.8952895656603758e-05, 
'nu02': 0.07628559024028292, 'nu30': - 3.7740980910254148e-06, 'nu21': -0.00010496766357504605, 
'nu12': 3.4336047797598577e-06, 'nu03': 9.572516684589537e-05}

And we get the following window, showing the output −

Example 2

In the Python 3 program below we detect the contours in the image and find the moments for the all contours. We also draw all the contours on the image.

import cv2 img = cv2.imread('shape.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,170,255,0) contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of Contours detected:",len(contours)) for i, cnt in enumerate(contours): M = cv2.moments(cnt) print(f"Moments of Contour {i+1}:\n", M) x1, y1 = cnt[0,0] img1 = cv2.drawContours(img, [cnt], -1, (0,255,255), 3) cv2.putText(img1, f'Contour:{i+1}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) cv2.imshow("Contours", img) cv2.waitKey(0) cv2.destroyAllWindows()

Output

On execution, it will produce the following output −

Number of Contours detected: 3 Moments of Contour 1: {'m00': 15402.0, 'm10': 5460392.0, 'm01': 5121775.5, 'm20': 1955553262.0, 'm11': 1815792476.0, 'm02': 1721276078.8333333, 'm30': 707266472160.0, 'm21': 650295900662.75, 'm12': 610232408910.5834, 'm03': 584418978310.75, 'mu20': 19708514.97597742, 'mu11': -4301.4312427043915, 'mu02': 18082709.63451171, 'mu30': -105064.52392578125, 'mu21': -22675.465063095093, 'mu12': 96956.12330245972, 'mu03': 19236.347534179688, 'nu20': 0.0830806075148932, 'nu11': -1.8132544296869317e-05, 'nu02': 0.07622707767590893, 'nu30': -3.5687267756372534e-06, 'nu21': -7.702175415353248e-07, 'nu12': 3.2933087246071784e-06, 'nu03': 6.534010334371792e-07}
 Moments of Contour 2: {'m00': 13560.0, 'm10': 6726168.0, 'm01': 2332734.0, 'm20': 3353389910.0, 'm11': 1157102529.0, 'm02': 415106150.0, 'm30': 1680255479196.0, 'm21': 576881671183.0, 'm12': 205903881453.0, 'm03': 76160706300.0, 'mu20': 17008201.723893642, 'mu11': -3723.4566371440887, 'mu02': 13804681.36017698, 'mu30': 25766.02392578125, 'mu21': -81702.26369953156, 'mu12': 22229.887162208557, 'mu03': 121576.5337524414, 'nu20': 0.09249942201541517, 'nu11': -2.0250088306010698e-05, 'nu02': 0.07507701682121294, 'nu30': 1.203367233494467e-06, 'nu21': -3.815793516358757e-06, 'nu12': 1.0382167575539102e-06, 'nu03': 5.678067268001503e-06} 
 Moments of Contour 3: {'m00': 15787.0, 'm10': 2612018.0, 'm01': 2825720.5, 'm20': 468078997.1666666, 'm11': 481368351.5833333, 'm02': 527125908.3333333, 'm30': 90297435295.8, 'm21': 88735810974.15, 'm12': 91882427379.68333, 'm03': 101549171192.45001, 'mu20': 35910882.11478847, 'mu11': 13842361.276815295, 'mu02': 21349234.860206723, 'mu30': 968841707.3605652, 'mu21': 373651504.4356222, 'mu12': -287817054.9542117, 'mu03': -443888063.0274048, 'nu20': 0.14408768219763246, 'nu11': 0.055540650495388824, 'nu02': 0.08566099150299868, 'nu30': 0.03093882414212907, 'nu21': 0.011932122758907373, 'nu12': -0.009191100239267325, 'nu03': -0.014175044918544318}

And we get the following window, showing the output

Updated on: 28-Sep-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements