Detecting an approaching object

I read this blog post where he uses a laser and a webcam to estimate the distance from cardboard from a webcam.

I had a different idea about this. I do not want to calculate the distance from the webcam.

I want to check if an object is approaching a webcam. The algorithm, in my opinion, would be something like this:

  • Object detection in the webcam channel.
  • If an object approaches a webcam, it will become more and more in the video stream.
  • Use this data for further calculations.

Since I want to detect random objects, I use the method findContours()to find outlines in the video stream. Using this, I will have at least the outlines of the objects in the video stream. Source:

import numpy as np
import cv2

vid=cv2.VideoCapture(0)
ans, instant=vid.read()
average=np.float32(instant)
cv2.accumulateWeighted(instant, average, 0.01)
background=cv2.convertScaleAbs(average)

while(1):
    _,f=vid.read()
    imgray=cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
    ret, thresh=cv2.threshold(imgray,127,255,0)
    diff=cv2.absdiff(f, background)
    cv2.imshow("input", f)
    cv2.imshow("Difference", diff)
    if cv2.waitKey(5)==27:
        break
cv2.destroyAllWindows()

Conclusion:

enter image description here

. , . ? ?

+4
2

. , "", . . - .

, , . , . - () . , , . .

, , . , . . .

, ( - ):

My hand moving towards the camera

My hand moving away from the camera

, .

:

import cv2
import numpy as np

AVERAGE_ALPHA      = 0.2         # 0-1 where 0 never adapts, and 1 instantly adapts
MOVEMENT_THRESHOLD = 30          # Lower values pick up more movement
REDUCED_SIZE       = (400, 600)
MORPH_KERNEL       = np.ones((10, 10), np.uint8)

def reduce_image(input_image):
    """Make the image easier to deal with."""
    reduced = cv2.resize(input_image, REDUCED_SIZE) 
    reduced = cv2.cvtColor(reduced, cv2.COLOR_BGR2GRAY)
    return reduced

# Initialise
vid = cv2.VideoCapture(0)
average = None

old_sizes = np.zeros(20)
size_update_index = 0

while (True):
    got_frame, frame = vid.read()

    if got_frame:
        # Reduce image
        reduced = reduce_image(frame)
        if average is None: average = np.float32(reduced)

        # Get background
        cv2.accumulateWeighted(reduced, average, AVERAGE_ALPHA) 
        background = cv2.convertScaleAbs(average)

        # Get thresholded difference image
        movement     = cv2.absdiff(reduced, background)
        _, threshold = cv2.threshold(movement, MOVEMENT_THRESHOLD, 255, cv2.THRESH_BINARY)

        # Apply morphology to help find object
        dilated = cv2.dilate(threshold, MORPH_KERNEL, iterations=10)
        closed  = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, MORPH_KERNEL)

        # Get contours
        contours, _ = cv2.findContours(closed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(closed, contours, -1, (150, 150, 150), -1)

        # Find biggest bounding rectangle
        areas = [cv2.contourArea(c) for c in contours]
        if (areas != list()):
            max_index = np.argmax(areas)
            max_cont  = contours[max_index]

            x, y, w, h = cv2.boundingRect(max_cont)
            cv2.rectangle(closed, (x, y), (x+w, y+h), (255, 255, 255), 5)

            # Guess movement direction
            size = w*h
            if size > old_sizes.mean():
                print "Towards"
            else:
                print "Away"

            # Update object size 
            old_sizes[size_update_index] = size
            size_update_index += 1
            if (size_update_index) >= len(old_sizes): size_update_index = 0

        # Display image
        cv2.imshow('RaptorVision', closed)

, , .. ( , - ). (, , ). , .

:

, :

wallflower

+3
Detect the object in the webcam feed.
If the object is approaching the webcam it'll grow larger and larger in the video feed.
Use this data for further calculations.

. , :

  • I1, I2,... In
  • . C1, C2,..., Cn (Contour - OpenCV)
  • + 1: S_i\leq C_i, i\in 1... n
  • + 1. .
  • , ( ;)
  • , .

, , . , , "" .

+1

Source: https://habr.com/ru/post/1545790/


All Articles