commit 6404a3b3fa4219395b035073632b0129ed648fb6 Author: pawan Date: Sun Jul 21 22:37:25 2019 +0530 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..b4a2f26 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# Measuring Size of Objects with OpenCV +### Calculates the size of objects based on a given reference object + +Cool object size estimator with just OpenCV and python + +All thanks to Adrian Rosebrock (from [pyimagesearch](https://www.pyimagesearch.com/)) for making +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. + +## **Key Points** +1. Steps involved: + 1. Find contours in the image. + 2. Get the minimum area rectangle for the contours. + 3. Draw the mid points and the lines joining mid points of the bounding rectangle of the contours. + 4. Grab the reference object from the contours and calculate **Pixel Per Metric** ratio. + 5. Calculate and print the bounding rectangle's dimensions based on the reference object's dimensions. +2. Assumptions: + 1. There is a reference object in the image which is easy to find and it's width/height is know to us. +3. Uses "Pixel Per Metric" ratio to calculate the size based on the given reference object. +4. Reference object properties: + 1. We should know the dimensions of this object (in terms of width or height). + 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). +5. Used the United States quarter as the reference object. +6. Used the OpenCV's find contours method to find the objects in the image and calculated their dimensions. + + ## **Requirements: (with versions i tested on)** + 1. python (3.7.3) + 2. opencv (4.1.0) + 3. numpy (1.61.4) + 4. imutils (0.5.2) + + ## **Commands to run the detection:** + ``` +python object_size.py --image images/example_01.png --width 0.955 +``` + +## **Results:** +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. + +![Gif 1 of object dimensions](example_01.gif) +![Gif 2 of object dimensions](example_02.gif) + + +## **The limitations** +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. +2. The photos are prone to radial and tangential lens distortion which would lead to uneven object dimensions. diff --git a/example_01.gif b/example_01.gif new file mode 100644 index 0000000..1e79a6f Binary files /dev/null and b/example_01.gif differ diff --git a/example_02.gif b/example_02.gif new file mode 100644 index 0000000..c34fbe7 Binary files /dev/null and b/example_02.gif differ diff --git a/images/example_01.png b/images/example_01.png new file mode 100644 index 0000000..454fee0 Binary files /dev/null and b/images/example_01.png differ diff --git a/images/example_02.png b/images/example_02.png new file mode 100644 index 0000000..3eb3fb5 Binary files /dev/null and b/images/example_02.png differ diff --git a/images/example_03.png b/images/example_03.png new file mode 100644 index 0000000..3f02ff4 Binary files /dev/null and b/images/example_03.png differ diff --git a/object_size.py b/object_size.py new file mode 100644 index 0000000..21b99c3 --- /dev/null +++ b/object_size.py @@ -0,0 +1,118 @@ +# USAGE +# python object_size.py --image images/example_01.png --width 0.955 +# python object_size.py --image images/example_02.png --width 0.955 +# python object_size.py --image images/example_03.png --width 3.5 + +# import the necessary packages +from scipy.spatial import distance as dist +from imutils import perspective +from imutils import contours +import numpy as np +import argparse +import imutils +import cv2 + +def midpoint(ptA, ptB): + return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5) + +# construct the argument parse and parse the arguments +ap = argparse.ArgumentParser() +ap.add_argument("-i", "--image", required=True, + help="path to the input image") +ap.add_argument("-w", "--width", type=float, required=True, + help="width of the left-most object in the image (in inches)") +args = vars(ap.parse_args()) + +# load the image, convert it to grayscale, and blur it slightly +image = cv2.imread(args["image"]) +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +gray = cv2.GaussianBlur(gray, (7, 7), 0) + +# perform edge detection, then perform a dilation + erosion to +# close gaps in between object edges +edged = cv2.Canny(gray, 50, 100) +edged = cv2.dilate(edged, None, iterations=1) +edged = cv2.erode(edged, None, iterations=1) + +# find contours in the edge map +cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, + cv2.CHAIN_APPROX_SIMPLE) +cnts = imutils.grab_contours(cnts) + +# sort the contours from left-to-right and initialize the +# 'pixels per metric' calibration variable +(cnts, _) = contours.sort_contours(cnts) +pixelsPerMetric = None + +# loop over the contours individually +for c in cnts: + # if the contour is not sufficiently large, ignore it + if cv2.contourArea(c) < 100: + continue + + # compute the rotated bounding box of the contour + orig = image.copy() + box = cv2.minAreaRect(c) + box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) + box = np.array(box, dtype="int") + + # order the points in the contour such that they appear + # in top-left, top-right, bottom-right, and bottom-left + # order, then draw the outline of the rotated bounding + # box + box = perspective.order_points(box) + cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) + + # loop over the original points and draw them + for (x, y) in box: + cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) + + # unpack the ordered bounding box, then compute the midpoint + # between the top-left and top-right coordinates, followed by + # the midpoint between bottom-left and bottom-right coordinates + (tl, tr, br, bl) = box + (tltrX, tltrY) = midpoint(tl, tr) + (blbrX, blbrY) = midpoint(bl, br) + + # compute the midpoint between the top-left and top-right points, + # followed by the midpoint between the top-righ and bottom-right + (tlblX, tlblY) = midpoint(tl, bl) + (trbrX, trbrY) = midpoint(tr, br) + + # draw the midpoints on the image + cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) + cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) + cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) + cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) + + # draw lines between the midpoints + cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), + (255, 0, 255), 2) + cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), + (255, 0, 255), 2) + + # compute the Euclidean distance between the midpoints + dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) + dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) + + # if the pixels per metric has not been initialized, then + # compute it as the ratio of pixels to supplied metric + # (in this case, inches) + if pixelsPerMetric is None: + pixelsPerMetric = dB / args["width"] + + # compute the size of the object + dimA = dA / pixelsPerMetric + dimB = dB / pixelsPerMetric + + # draw the object sizes on the image + cv2.putText(orig, "{:.1f}in".format(dimA), + (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, + 0.65, (255, 255, 255), 2) + cv2.putText(orig, "{:.1f}in".format(dimB), + (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, + 0.65, (255, 255, 255), 2) + + # show the output image + cv2.imshow("Image", orig) + cv2.waitKey(0) \ No newline at end of file diff --git a/object_size_mine.py b/object_size_mine.py new file mode 100644 index 0000000..b694b84 --- /dev/null +++ b/object_size_mine.py @@ -0,0 +1,91 @@ +from scipy.spatial import distance as dist +from imutils import perspective +from imutils import contours +import argparse +import numpy as np +import imutils +import cv2 + + +def midpoint(ptA, ptB): + return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5) + +def show_image(title, image, destroy_all=True): + cv2.imshow(title, image) + cv2.waitKey(0) + if destroy_all: + cv2.destroyAllWindows() + + + +ap = argparse.ArgumentParser() +ap.add_argument("-i", "--image", required=True, help="path to the input image") +ap.add_argument("-w", "--width", type=float, required=True, help="width of the left-most object in the image (in inches)") +args = vars(ap.parse_args()) + +image = cv2.imread(args["image"]) +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +gray = cv2.GaussianBlur(gray, (7, 7), 0) + +edged = cv2.Canny(gray, 50, 100) +show_image("Edged", edged, False) +edged = cv2.dilate(edged, None, iterations=1) +edged = cv2.erode(edged, None, iterations=1) +show_image("erode and dilate", edged, True) + +cnts = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) +cnts = imutils.grab_contours(cnts) +print("Total number of contours are: ", len(cnts)) + +(cnts, _) = contours.sort_contours(cnts) +pixelPerMetric = None + + +count = 0 +for c in cnts: + if cv2.contourArea(c) < 100: + continue + count += 1 + + orig = image.copy() + box = cv2.minAreaRect(c) + box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) + box = np.array(box, dtype="int") + + box = perspective.order_points(box) + cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) + + for (x, y) in box: + cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) + + + (tl, tr, br, bl) = box + (tltrX, tltrY) = midpoint(tl, tr) + (blbrX, blbrY) = midpoint(bl, br) + (tlblX, tlblY) = midpoint(tl, bl) + (trbrX, trbrY) = midpoint(tr, br) + + cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) + cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) + cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) + cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) + + cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) + cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) + + dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) + dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) + + if pixelPerMetric is None: + pixelPerMetric = dB / args["width"] + + dimA = dA / pixelPerMetric + dimB = dB / pixelPerMetric + + cv2.putText(orig, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) + cv2.putText(orig, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) + + cv2.imshow("Image", orig) + cv2.waitKey(0) + +print("Total contours processed: ", count)