【问题标题】:OpenCV python - How to constantly update the time output and distance output?OpenCV python - 如何不断更新时间输出和距离输出?
【发布时间】:2016-01-20 19:37:38
【问题描述】:

我正在使用运行 Raspbian Wheezy 并带有 USB 网络摄像头的 Raspberry Pi B+。我的目标是实时测量物体与相机之间的距离。

关注a guide on how to do so with still images

这是我目前正在运行的代码:

# import the necessary packages
import numpy as np
import cv2
import datetime
import time

def find_marker(frame):
    # convert the image to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)

    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    c = max(cnts, key = cv2.contourArea)

    # compute the bounding box of the of the paper region and return it
    return cv2.minAreaRect(c)

def distance_to_camera(knownWidth, focalLength, perWidth):
    # compute and return the distance from the maker to the camera
    return (knownWidth * focalLength) / perWidth

# initialize the known distance from the camera to the object, which
# in this case is 24 inches
KNOWN_DISTANCE = 11.811

# initialize the known object width, which in this case, the piece of
# paper is 11 inches wide
KNOWN_WIDTH = 2.3622

# initialize the list of images that we'll be using
#IMAGE_PATHS = ["images/2ft.png", "images/3ft.png", "images/4ft.png"]

# load the furst image that contains an object that is KNOWN TO BE 2 feet
# from our camera, then find the paper marker in the image, and initialize
# the focal length
#image = cv2.imread(IMAGE_PATHS[0])
#marker = find_marker(image)
#focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH


cap = cv2.VideoCapture(0)
timestamp = datetime.datetime.now()

while(1):

    (grabbed, frame) = cap.read()
    marker = find_marker(frame)
#   for () LOOP THIS TO GET DISTANCE CALCULATION FULLY WORKING!
    focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
    inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])

        # draw a bounding box around the image and display it
        box = np.int0(cv2.cv.BoxPoints(marker))
        cv2.drawContours(frame, [box], -1, (0, 255, 0), 2)
        ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
    cv2.putText(frame, "%.2fft" % (inches / 12),
        (frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
        2.0, (0, 255, 0), 3)
    cv2.putText(frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
        0.35, (0, 0, 255), 1)   

    #Write to textfile here and send
#   for () LOOP End

    cv2.imshow("Frame",frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

# loop over the images
#for imagePath in IMAGE_PATHS:
    # load the image, find the marker in the image, then compute the
    # distance to the marker from the camera
#   image = cv2.imread(imagePath)
#   marker = find_marker(image)
#   inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])

    # draw a bounding box around the image and display it
#   box = np.int0(cv2.cv.BoxPoints(marker))
#   cv2.drawContours(image, [box], -1, (0, 255, 0), 2)
#   cv2.putText(image, "%.2fft" % (inches / 12),
#       (image.shape[1] - 200, image.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
#       2.0, (0, 255, 0), 3)
#   cv2.imshow("image", image)
#   cv2.waitKey(0)

这是我的输出:

但是,时间(左下角的红色文本)和检测到的距离不会随着程序的运行而改变。有没有办法让这两个值在程序结束之前更新?

【问题讨论】:

    标签: python opencv camera raspberry-pi


    【解决方案1】:

    这就是为什么两个值都没有更新的原因:

    时间戳

    timestamp 不在while 循环中

    timestamp = datetime.datetime.now()
    while(1):
    

    应该是:

    while(1):
        timestamp = datetime.datetime.now()
    

    距离

    distance_to_camera,使用这个参数,会产生一个恒定的输出:

    # assuming:
    # a = KNOWN_WIDTH
    # b = focalLength
    # c = marker[1][0]
    # d = KNOWN_DISTANCE
    
    def distance_to_camera(x,y,z):
        return (x*y)/z
    
    b = c*d/a
    inches = distance_to_camera(a,b,c) # => a*b/c
    # inches = a*b/c, b = c*d/a
    # inches = a*c*d/a*c
    # inches = d << constant output
    

    等于KNOWN_DISTANCE。如果你算一下:KNOWN_DISTANCE / 12 = 0.98425 是你得到的距离


    编辑:

    我刚刚阅读了教程,看来您应该只计算一次 focalLenght,而不是 while

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多