How can we calculate length/width of an object in image?

asked 2018-09-01 20:39:13 -0600

abdur-rehman gravatar image

updated 2018-09-02 12:38:13 -0600

Basically , i am trying to calculate length/width of an object in image ,with cartesian graph paper in background.I have found bounding box of object.But how can i read cartesian coordinate (x,y) in background? image description


    import cv2 as cv
import numpy as np
img = cv.imread('E:\PY_CV\graph1.jpg')
img=cv.resize(img,(400,400))
edges = cv.Canny(img,100,200)
####################################
# Convert BGR to HSV https://www.lifewire.com/what-is-hsv-in-design-1078068
hsv_g = cv.cvtColor(img, cv.COLOR_BGR2HSV)
# define range of green color in HSV
lower_green = np.array([30,60,60])
upper_green = np.array([100,255,255])
# Threshold the HSV image to get only yellow colors
mask_g = cv.inRange(hsv_g, lower_green, upper_green)
non_zero_green=cv.countNonZero(mask_g)
points=cv.findNonZero(mask_g)
x,y,w,h=cv.boundingRect(points)
cv.rectangle(img,(x,y),(x+w,y+h),(255,255,255),2)
cropped_green=img[y:y+h,x:x+w]
print(x)
print(y)
print(w)
print(h)
cv.imshow('mask-green',mask_g)
cv.imshow('cropped_green',cropped_green)
cv.imshow('bounding_box',img)
cv.waitKey(0)
cv.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

I cannot see any image in your post

LBerger gravatar imageLBerger ( 2018-09-01 23:36:38 -0600 )edit

Updated image link

abdur-rehman gravatar imageabdur-rehman ( 2018-09-02 02:22:10 -0600 )edit

please put your image here, not on dropbox.

berak gravatar imageberak ( 2018-09-02 02:27:35 -0600 )edit
1

My image is 2.7 MB

abdur-rehman gravatar imageabdur-rehman ( 2018-09-02 02:32:02 -0600 )edit

there is a password. Reduce image size : resize or compress image and insert in your post (limit is 1MB). and post your code

LBerger gravatar imageLBerger ( 2018-09-02 03:12:14 -0600 )edit
1

image uploaded

abdur-rehman gravatar imageabdur-rehman ( 2018-09-02 06:54:12 -0600 )edit

please give the code you tried so far

sturkmen gravatar imagesturkmen ( 2018-09-02 11:03:27 -0600 )edit
1

code added

abdur-rehman gravatar imageabdur-rehman ( 2018-09-02 12:38:25 -0600 )edit

Calibrate your camera and use a camera with a fixed distance and orientation relative to object (leaf)

You should use a scanner it would be easyer

LBerger gravatar imageLBerger ( 2018-09-02 13:12:10 -0600 )edit

If you just want to calculate real world dimensions of leaf. You can you use this approach similar to @LBerger

  1. Capture leaf image such that camera surface is parallel to leaf kept at a known distance. (Ortho image)
  2. Estimate camera intrinsic parameters using monocular camera calibration.
  3. Estimate leaf bounding width and height in pixels. ( you have estimated already in your mentioned code).
  4. Use pin hole camera model to estimate real world dimensions of leaf.
  5. Done ...
ak1 gravatar imageak1 ( 2018-09-05 00:46:09 -0600 )edit