Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to measure an object?

I'm trying to make a tool that does object measurement when it is pointed at the webcam, but it ends up measuring anything.

code:

import cv2 import numpy as np

cap = cv2.VideoCapture(0)

while True: _, frame = cap.read()

frame = frame[0:350, 15:540]

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 3)

_, threshold = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY|cv2.THRESH_TRIANGLE)

kernel = np.ones((5,5),np.uint8)

dilation = cv2.erode(threshold,kernel,iterations = 1)

contours,_ = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)


for (i,c) in enumerate(contours):
    (x,y,w,h) = cv2.boundingRect(c)
    area = cv2.contourArea(c)

    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2, cv2.LINE_AA)
    cmY = (h*15)/120
    cmX = (w*10)/71
    cv2.putText(frame, str(int(cmY)), (x, y+h+15), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255),1)
    cv2.putText(frame, str(int(cmX)), (x+w+15, y+h+15), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255),1)


cv2.imshow("Frame", frame)


key = cv2.waitKey(1)
if key == 27:
    break

cap.release() cv2.destroyAllWindows()

how to measure an object?

I'm trying to make a tool that does object measurement when it is pointed at the webcam, but it ends up measuring anything.

code:

import cv2
import numpy as np

np cap = cv2.VideoCapture(0)

cv2.VideoCapture(0) while True: _, frame = cap.read()

cap.read()
frame = frame[0:350, 15:540]
 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 gray = cv2.GaussianBlur(gray, (7, 7), 3)
 _, threshold = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY|cv2.THRESH_TRIANGLE)
 kernel = np.ones((5,5),np.uint8)
 dilation = cv2.erode(threshold,kernel,iterations = 1)
 contours,_ = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 for (i,c) in enumerate(contours):
 (x,y,w,h) = cv2.boundingRect(c)
 area = cv2.contourArea(c)
  cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2, cv2.LINE_AA)
 cmY = (h*15)/120
 cmX = (w*10)/71
  cv2.putText(frame, str(int(cmY)), (x, y+h+15), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255),1)
 cv2.putText(frame, str(int(cmX)), (x+w+15, y+h+15), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 255),1)
 cv2.imshow("Frame", frame)
 key = cv2.waitKey(1)
 if key == 27:
 break

cap.release()
cv2.destroyAllWindows()

cap.release() cv2.destroyAllWindows()