[OpenCV] ⚠️ Masters don't enter! Learn the basics in half an hour ⚠️ Object Measurement
summarize
OpenCV is a cross-platform computer vision library with multi-language support and powerful features. Today Xiaobai will take you into the world of OpenCV together.
Object Measurement
Object measurements can help us with matrix calculations: the
- Get the arc length and area
- polygonal fitting
- Calculate the center of the image object
Origin distance.
Center distance.
Image center of gravity coordinates.
polygonal fitting
Steps.
- Read image
- Convert to grayscale
- binarization
- Contour Detection
- Calculate contour perimeter
- polygonal fitting
Format.
(curve, epsilon, closed, approxCurve=None)
Parameters.
- curve: Input curve
- epsilon: approximation curvature, the smaller the epsilon, the better the similarity approximation.
- closed: closed or not
Code.
import cv2 from matplotlib import pyplot as plt # Read the picture image = ("") image_gray = (image, cv2.COLOR_BGR2GRAY) # Binarization ret, thresh = (image_gray, 127, 255, cv2.THRESH_OTSU) # Calculate the profile contours, hierarchy = (thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) # Profile approximation perimeter = (contours[0], True) approx = (contours[0], perimeter * 0.1, True) # Drawing outlines result1 = ((), contours, 0, (0, 0, 255), 2) result2 = ((), [approx], -1, (0, 0, 255), 2) # Pictures to show f, ax = (1, 2, figsize=(12, 8)) # Submap ax[0].imshow((result1, cv2.COLOR_BGR2RGB)) ax[1].imshow((result2, cv2.COLOR_BGR2RGB)) # Title ax[0].set_title("contour") ax[1].set_title("approx") ()
Output.
Computational Object Center (computing)
()
This will help us to get the wheelbase, so that we can further calculate the center of the image object.
Format.
(array, binaryImage=None)
Parameters.
- array: contour
- binaryImage: if or not the non-zero values in the array will be treated as 1, default is None.
Example 1.
import numpy as np import cv2 # Read the picture image = ("") image_gray = (image, cv2.COLOR_BGR2GRAY) # Binarization ret, thresh = (image_gray, 0, 255, cv2.THRESH_OTSU) # Get the outline contours, hierarchy = (thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Traverse each profile for i, contour in enumerate(contours): # Area area = (contour) # External Rectangle x, y, w, h = (contour) # Getting the distance mm = (contour) print(mm, type(mm)) # Debug output (dictionary type) # Access Center cx = mm["m10"] / mm["m00"] cy = mm["m01"] / mm["m00"] # Get (image, ((cx), (cy)), 3, (0, 255, 255), -1) (image, (x, y), (x + w, y + h), (0, 0, 255), 2) # Pictures to show ("result", image) (0) () # Save the picture ("", image)
Output.
{'m00': 8500.5, 'm10': 1027805.8333333333, 'm01': 2991483.6666666665, 'm20': 131713199.41666666, 'm11': 365693040.4583333, 'm02': 1061366842.5833333, 'm30': 17848380183.95, 'm21': 47383693552.933334, 'm12': 131067057115.4, 'm03': 379419590249.80005, 'mu20': 7439941.251379013, 'mu11': 3989097.993609071, 'mu02': 8608236.862088203, 'mu30': 123631672.32175827, 'mu21': 66721478.995661736, 'mu12': -71778847.06811166, 'mu03': -153890589.33666992, 'nu20': 0.10296285178405724, 'nu11': 0.05520593397050295, 'nu02': 0.11913113104071384, 'nu30': 0.01855746134472764, 'nu21': 0.010015081443714638, 'nu12': -0.010774206599494254, 'nu03': -0.023099409797678556} <class 'dict'>
{'m00': 15986.0, 'm10': 6026846.0, 'm01': 5179910.0, 'm20': 2292703160.333333, 'm11': 1952864629.0, 'm02': 1698884573.6666665, 'm30': 879850714149.0, 'm21': 742898718990.0, 'm12': 640491821107.3334, 'm03': 563738081200.0, 'mu20': 20535469.371490955, 'mu11': -1620.4595272541046, 'mu02': 20449217.223528624, 'mu30': -223791.80407714844, 'mu21': 151823.5922050476, 'mu12': 209097.09715557098, 'mu03': -152351.75524902344, 'nu20': 0.08035724088041474, 'nu11': -6.34101194440178e-06, 'nu02': 0.08001972803837157, 'nu30': -6.926194062792776e-06, 'nu21': 4.698830090131295e-06, 'nu12': 6.471403538830498e-06, 'nu03': -4.715176353366703e-06} <class 'dict'>
{'m00': 11396.0, 'm10': 6176598.0, 'm01': 2597707.833333333, 'm20': 3349665027.0, 'm11': 1407949570.5833333, 'm02': 655725464.8333333, 'm30': 1817641012813.0, 'm21': 763562731879.1167, 'm12': 355401284084.75, 'm03': 178062030454.85, 'mu20': 1967338.8985610008, 'mu11': -324.81426215171814, 'mu02': 63580327.29723644, 'mu30': -21712.3154296875, 'mu21': 9988180.769364119, 'mu12': 186586.19526672363, 'mu03': -396148296.0755005, 'nu20': 0.015148662774911266, 'nu11': -2.501095121647356e-06, 'nu02': 0.48957347310563326, 'nu30': -1.5661200090835562e-06, 'nu21': 0.0007204523998327835, 'nu12': 1.3458554191159022e-05, 'nu03': -0.028574371768747265} <class 'dict'>
{'m00': 11560.0, 'm10': 4184863.0, 'm01': 1485772.0, 'm20': 1524366924.3333333, 'm11': 537875136.1666666, 'm02': 203000229.0, 'm30': 558641678337.5, 'm21': 195927630288.0, 'm12': 73490515262.5, 'm03': 29185458885.0, 'mu20': 9394750.564388752, 'mu11': 7292.807151079178, 'mu02': 12038426.579238743, 'mu30': -36898.54187011719, 'mu21': 58255.2828142643, 'mu12': 46557.39966964722, 'mu03': -74896.38109207153, 'nu20': 0.07030230843432154, 'nu11': 5.457315488828541e-05, 'nu02': 0.0900853271874644, 'nu30': -2.568115896721007e-06, 'nu21': 4.0545319755426715e-06, 'nu12': 3.2403664790463073e-06, 'nu03': -5.21274221530133e-06} <class 'dict'>
{'m00': 7136.5, 'm10': 931499.3333333333, 'm01': 837811.3333333333, 'm20': 126603461.91666666, 'm11': 109342970.95833333, 'm02': 104031211.58333333, 'm30': 17834967892.7, 'm21': 14861464047.05, 'm12': 13575875235.816666, 'm03': 13540680151.900002, 'mu20': 5018510.189567342, 'mu11': -13253.86603589356, 'mu02': 5673777.230110094, 'mu30': -177930.16611862183, 'mu21': 1921792.6864708662, 'mu12': 201480.14046394825, 'mu03': -4564410.182851791, 'nu20': 0.09853811951621429, 'nu11': -0.00026023879322029775, 'nu02': 0.11140424502299628, 'nu30': -4.135579833554871e-05, 'nu21': 0.00044667676380089435, 'nu12': 4.682945134828951e-05, 'nu03': -0.0010608927713634498} <class 'dict'>
Example 2.
import numpy as np import cv2 # Read the picture image = ("") image_gray = (image, cv2.COLOR_BGR2GRAY) # Binarization ret, thresh = (image_gray, 0, 255, cv2.THRESH_OTSU) # Get the outline contours, hierarchy = (thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Traverse each profile for i, contour in enumerate(contours): # Area area = (contour) # External Rectangle x, y, w, h = (contour) # Getting the distance mm = (contour) # Access Center cx = mm["m10"] / mm["m00"] cy = mm["m01"] / mm["m00"] # Get (image, ((cx), (cy)), 3, (0, 255, 255), -1) (image, (x, y), (x + w, y + h), (0, 0, 255), 2) # Multi-deformation fitting approxCurve = (contour, 4, True) print() # Circles if [0] > 10: (image, contours, i, (0, 255, 0), 2) # Green # 4-10 sides if 10 >= [0] > 3: (image, contours, i, (240, 32, 160), 2) # Purple # Triangles if [0] == 3: (image, contours, i, (250, 206, 135), 2) # Blue # Pictures to show ("result", image) (0) () # Save the picture ("", image)
Output.
(3, 1, 2)
(6, 1, 2)
(7, 1, 2)
(16, 1, 2)
(10, 1, 2)
To this article on OpenCV half an hour to master the basic operation of the object measurement of the article is introduced to this, more related to OpenCV object measurement content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!