├── README.md ├── example_01.gif ├── example_02.gif ├── images ├── example_01.png ├── example_02.png └── example_03.png ├── object_size.py └── object_size_mine.py /README.md: -------------------------------------------------------------------------------- 1 | # Measuring Size of Objects with OpenCV 2 | ### Calculates the size of objects based on a given reference object 3 | 4 | Cool object size estimator with just OpenCV and python 5 | 6 | All thanks to Adrian Rosebrock (from [pyimagesearch](https://www.pyimagesearch.com/)) for making 7 | great tutorials. This project is inspired from his blog: [Measuring size of objects in an image with OpenCV](https://www.pyimagesearch.com/2016/03/28/measuring-size-of-objects-in-an-image-with-opencv/). I have included the author's code and the one i wrote my self as well. 8 | 9 | ## **Key Points** 10 | 1. Steps involved: 11 | 1. Find contours in the image. 12 | 2. Get the minimum area rectangle for the contours. 13 | 3. Draw the mid points and the lines joining mid points of the bounding rectangle of the contours. 14 | 4. Grab the reference object from the contours and calculate **Pixel Per Metric** ratio. 15 | 5. Calculate and print the bounding rectangle's dimensions based on the reference object's dimensions. 16 | 2. Assumptions: 17 | 1. There is a reference object in the image which is easy to find and it's width/height is know to us. 18 | 3. Uses "Pixel Per Metric" ratio to calculate the size based on the given reference object. 19 | 4. Reference object properties: 20 | 1. We should know the dimensions of this object (in terms of width or height). 21 | 2. We should be able to easily find this reference object in the image, either based on the placement of the object (like being placed in top-left corner, etc.) or via appearances (like distinctive color and/or shape). 22 | 5. Used the United States quarter as the reference object. 23 | 6. Used the OpenCV's find contours method to find the objects in the image and calculated their dimensions. 24 | 25 | ## **Requirements: (with versions i tested on)** 26 | 1. python (3.7.3) 27 | 2. opencv (4.1.0) 28 | 3. numpy (1.61.4) 29 | 4. imutils (0.5.2) 30 | 31 | ## **Commands to run the detection:** 32 | ``` 33 | python object_size.py --image images/example_01.png --width 0.955 34 | ``` 35 | 36 | ## **Results:** 37 | The results are pretty decent even though not perfect. This is due the limitations of the image itself as its not perfect top-down view of the objects and some calibrations could have also been done in the camera before clicking the picture. 38 | 39 | ![Gif 1 of object dimensions](example_01.gif) 40 | ![Gif 2 of object dimensions](example_02.gif) 41 | 42 | 43 | ## **The limitations** 44 | 1. This technique requires the image to be near perfect top-down view of the objects to calculate the accurate results. Otherwise the dimensions of the objects in the image may be distorted. 45 | 2. The photos are prone to radial and tangential lens distortion which would lead to uneven object dimensions. 46 | -------------------------------------------------------------------------------- /example_01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Practical-CV/Measuring-Size-of-Objects-with-OpenCV/6404a3b3fa4219395b035073632b0129ed648fb6/example_01.gif -------------------------------------------------------------------------------- /example_02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Practical-CV/Measuring-Size-of-Objects-with-OpenCV/6404a3b3fa4219395b035073632b0129ed648fb6/example_02.gif -------------------------------------------------------------------------------- /images/example_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Practical-CV/Measuring-Size-of-Objects-with-OpenCV/6404a3b3fa4219395b035073632b0129ed648fb6/images/example_01.png -------------------------------------------------------------------------------- /images/example_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Practical-CV/Measuring-Size-of-Objects-with-OpenCV/6404a3b3fa4219395b035073632b0129ed648fb6/images/example_02.png -------------------------------------------------------------------------------- /images/example_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Practical-CV/Measuring-Size-of-Objects-with-OpenCV/6404a3b3fa4219395b035073632b0129ed648fb6/images/example_03.png -------------------------------------------------------------------------------- /object_size.py: -------------------------------------------------------------------------------- 1 | # USAGE 2 | # python object_size.py --image images/example_01.png --width 0.955 3 | # python object_size.py --image images/example_02.png --width 0.955 4 | # python object_size.py --image images/example_03.png --width 3.5 5 | 6 | # import the necessary packages 7 | from scipy.spatial import distance as dist 8 | from imutils import perspective 9 | from imutils import contours 10 | import numpy as np 11 | import argparse 12 | import imutils 13 | import cv2 14 | 15 | def midpoint(ptA, ptB): 16 | return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5) 17 | 18 | # construct the argument parse and parse the arguments 19 | ap = argparse.ArgumentParser() 20 | ap.add_argument("-i", "--image", required=True, 21 | help="path to the input image") 22 | ap.add_argument("-w", "--width", type=float, required=True, 23 | help="width of the left-most object in the image (in inches)") 24 | args = vars(ap.parse_args()) 25 | 26 | # load the image, convert it to grayscale, and blur it slightly 27 | image = cv2.imread(args["image"]) 28 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 29 | gray = cv2.GaussianBlur(gray, (7, 7), 0) 30 | 31 | # perform edge detection, then perform a dilation + erosion to 32 | # close gaps in between object edges 33 | edged = cv2.Canny(gray, 50, 100) 34 | edged = cv2.dilate(edged, None, iterations=1) 35 | edged = cv2.erode(edged, None, iterations=1) 36 | 37 | # find contours in the edge map 38 | cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, 39 | cv2.CHAIN_APPROX_SIMPLE) 40 | cnts = imutils.grab_contours(cnts) 41 | 42 | # sort the contours from left-to-right and initialize the 43 | # 'pixels per metric' calibration variable 44 | (cnts, _) = contours.sort_contours(cnts) 45 | pixelsPerMetric = None 46 | 47 | # loop over the contours individually 48 | for c in cnts: 49 | # if the contour is not sufficiently large, ignore it 50 | if cv2.contourArea(c) < 100: 51 | continue 52 | 53 | # compute the rotated bounding box of the contour 54 | orig = image.copy() 55 | box = cv2.minAreaRect(c) 56 | box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) 57 | box = np.array(box, dtype="int") 58 | 59 | # order the points in the contour such that they appear 60 | # in top-left, top-right, bottom-right, and bottom-left 61 | # order, then draw the outline of the rotated bounding 62 | # box 63 | box = perspective.order_points(box) 64 | cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) 65 | 66 | # loop over the original points and draw them 67 | for (x, y) in box: 68 | cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) 69 | 70 | # unpack the ordered bounding box, then compute the midpoint 71 | # between the top-left and top-right coordinates, followed by 72 | # the midpoint between bottom-left and bottom-right coordinates 73 | (tl, tr, br, bl) = box 74 | (tltrX, tltrY) = midpoint(tl, tr) 75 | (blbrX, blbrY) = midpoint(bl, br) 76 | 77 | # compute the midpoint between the top-left and top-right points, 78 | # followed by the midpoint between the top-righ and bottom-right 79 | (tlblX, tlblY) = midpoint(tl, bl) 80 | (trbrX, trbrY) = midpoint(tr, br) 81 | 82 | # draw the midpoints on the image 83 | cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) 84 | cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) 85 | cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) 86 | cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) 87 | 88 | # draw lines between the midpoints 89 | cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), 90 | (255, 0, 255), 2) 91 | cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), 92 | (255, 0, 255), 2) 93 | 94 | # compute the Euclidean distance between the midpoints 95 | dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) 96 | dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) 97 | 98 | # if the pixels per metric has not been initialized, then 99 | # compute it as the ratio of pixels to supplied metric 100 | # (in this case, inches) 101 | if pixelsPerMetric is None: 102 | pixelsPerMetric = dB / args["width"] 103 | 104 | # compute the size of the object 105 | dimA = dA / pixelsPerMetric 106 | dimB = dB / pixelsPerMetric 107 | 108 | # draw the object sizes on the image 109 | cv2.putText(orig, "{:.1f}in".format(dimA), 110 | (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 111 | 0.65, (255, 255, 255), 2) 112 | cv2.putText(orig, "{:.1f}in".format(dimB), 113 | (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 114 | 0.65, (255, 255, 255), 2) 115 | 116 | # show the output image 117 | cv2.imshow("Image", orig) 118 | cv2.waitKey(0) -------------------------------------------------------------------------------- /object_size_mine.py: -------------------------------------------------------------------------------- 1 | from scipy.spatial import distance as dist 2 | from imutils import perspective 3 | from imutils import contours 4 | import argparse 5 | import numpy as np 6 | import imutils 7 | import cv2 8 | 9 | 10 | def midpoint(ptA, ptB): 11 | return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5) 12 | 13 | def show_image(title, image, destroy_all=True): 14 | cv2.imshow(title, image) 15 | cv2.waitKey(0) 16 | if destroy_all: 17 | cv2.destroyAllWindows() 18 | 19 | 20 | 21 | ap = argparse.ArgumentParser() 22 | ap.add_argument("-i", "--image", required=True, help="path to the input image") 23 | ap.add_argument("-w", "--width", type=float, required=True, help="width of the left-most object in the image (in inches)") 24 | args = vars(ap.parse_args()) 25 | 26 | image = cv2.imread(args["image"]) 27 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 28 | gray = cv2.GaussianBlur(gray, (7, 7), 0) 29 | 30 | edged = cv2.Canny(gray, 50, 100) 31 | show_image("Edged", edged, False) 32 | edged = cv2.dilate(edged, None, iterations=1) 33 | edged = cv2.erode(edged, None, iterations=1) 34 | show_image("erode and dilate", edged, True) 35 | 36 | cnts = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 37 | cnts = imutils.grab_contours(cnts) 38 | print("Total number of contours are: ", len(cnts)) 39 | 40 | (cnts, _) = contours.sort_contours(cnts) 41 | pixelPerMetric = None 42 | 43 | 44 | count = 0 45 | for c in cnts: 46 | if cv2.contourArea(c) < 100: 47 | continue 48 | count += 1 49 | 50 | orig = image.copy() 51 | box = cv2.minAreaRect(c) 52 | box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) 53 | box = np.array(box, dtype="int") 54 | 55 | box = perspective.order_points(box) 56 | cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) 57 | 58 | for (x, y) in box: 59 | cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) 60 | 61 | 62 | (tl, tr, br, bl) = box 63 | (tltrX, tltrY) = midpoint(tl, tr) 64 | (blbrX, blbrY) = midpoint(bl, br) 65 | (tlblX, tlblY) = midpoint(tl, bl) 66 | (trbrX, trbrY) = midpoint(tr, br) 67 | 68 | cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) 69 | cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) 70 | cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) 71 | cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) 72 | 73 | cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) 74 | cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) 75 | 76 | dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) 77 | dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) 78 | 79 | if pixelPerMetric is None: 80 | pixelPerMetric = dB / args["width"] 81 | 82 | dimA = dA / pixelPerMetric 83 | dimB = dB / pixelPerMetric 84 | 85 | cv2.putText(orig, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) 86 | cv2.putText(orig, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) 87 | 88 | cv2.imshow("Image", orig) 89 | cv2.waitKey(0) 90 | 91 | print("Total contours processed: ", count) 92 | --------------------------------------------------------------------------------