1

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

enter image description here

Here's the output I want

enter image description here

I'm just using paint to this output that I want.

nathancy
  • 42,661
  • 14
  • 115
  • 137
TrialAndError
  • 87
  • 1
  • 1
  • 7
  • [How to Detect Contours in Images using OpenCV in Python](https://www.thepythoncode.com/article/contour-detection-opencv-python) – furas Feb 11 '20 at 14:33
  • not the contour I need to get the measure of this object hehe. – TrialAndError Feb 11 '20 at 14:36
  • to measure object you have to first find its contour. OR use Machine Learning or Deep Network to recognize object on image. – furas Feb 11 '20 at 14:37
  • You need to get the contours first, then fit a box around them, measure the dimensions of that box. That'll give you the size in pixels. If you want it in real-world measurements (like centimetres or inches), that's a much more difficult problem as you have to have calibration data for your image to allow you to translate pixels into real-world. – n00dle Feb 11 '20 at 14:38
  • then after detecting the contours? how to get the size? – TrialAndError Feb 11 '20 at 14:38
  • Do you want pixel size or real-world size? – n00dle Feb 11 '20 at 14:39
  • real-world size – TrialAndError Feb 11 '20 at 14:41
  • [Creating Bounding boxes and circles for contours](https://docs.opencv.org/3.4.9/da/d0c/tutorial_bounding_rects_circles.html) - it gives rectangle so you have size in pixels. Now you would need other object on image whihc real size you already know. This way you can calculate scale and use this scale to convert other object pixel's size to real size – furas Feb 11 '20 at 14:45
  • can you help me provide that code? for this image I just need a reference, if you would only. – TrialAndError Feb 11 '20 at 14:49

3 Answers3

2

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.

enter image description here

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()
nathancy
  • 42,661
  • 14
  • 115
  • 137
1

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..

cestpasmoi
  • 505
  • 6
  • 12
0

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

Edwin Cheong
  • 879
  • 2
  • 7
  • 12