Reputation: 87
I have a question regarding opencv python , I have an example of image here I want to know how to get the size of this object using opencv python.
Here's the sample image
Here's the output I want
I'm just using paint to this output that I want.
Upvotes: 1
Views: 27523
Reputation: 655
In your case, using just an RGB picture, you actually can't.
Nevertheless, a simple and practical way would be the following. You probably need a reference object with known size in real world in a measurable unit such as millimeters. This should be placed beforehand at the same distance from the camera with the object of interest. Having detected both objects within the image (reference object and object of interest), you would be able to calculate the "Pixel Per Metric ratio" in order to compute it's actual size. For further detail, you check these links : tutorial, similarly in github.
Another way would be by using depth cameras, or just simply retrieve the distance from the object of interest using alternative techniques as this answer may suggest.
(edit) On the other hand, since your question doesn't exactly clarify whether you mean real-world metrics (i.e. centimeters) or just a measurement in pixels, forgive me if I mislead you..
Upvotes: 1
Reputation: 46600
A simple approach is to obtain a binary image then find the bounding box on that image. Here's the result with the width (in pixels) and the height of the box drawn onto the image. To determine real-world measurements, you would need calibration information to scale pixels into concrete values (such as centimeters). Without calibration information to translate pixels into quantifiable lengths, it would be difficult to convert this to a real-life size.
Code
import cv2
# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find bounding box
x,y,w,h = cv2.boundingRect(thresh)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.putText(image, "w={},h={}".format(w,h), (x,y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)
cv2.imshow("thresh", thresh)
cv2.imshow("image", image)
cv2.waitKey()
Upvotes: 2
Reputation: 979
Here's a quick recommendation why not use an object detector like YOLO, there are lot of pretrained weights you can download online and banana is fortunately within the coco classes.
You can take a look at this github repo: https://github.com/divikshrivastava/drfoodie
Edit: Take a look here for a sample https://drive.google.com/file/d/1uJE0hy9mv75Ya3dqBIyw-kn1h87WLxy4/view?usp=drivesdk
Upvotes: 0