첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170
import cv2
import numpy as np

videoCapture = cv2.VideoCapture(0)
featureParameterDictionary = dict(maxCorners = 1000, qualityLevel = 0.3, minDistance = 7, blockSize = 5, useHarrisDetector = 1, k = 0.04)
lkParameterDictionary = dict(winSize = (15, 15), maxLevel = 2)
colorNDArray = np.random.randint(0, 255, (1000, 3))
result, previousFrameNDArray = videoCapture.read()
previousGrayscaleFrameNDArray = cv2.cvtColor(previousFrameNDArray, cv2.COLOR_BGR2GRAY)
previousFeatureNDArray = cv2.goodFeaturesToTrack(previousGrayscaleFrameNDArray, mask = None, **featureParameterDictionary)
maskNDArray = np.zeros_like(previousFrameNDArray)
count = 0
while(videoCapture.isOpened()):
    result, frameNDArray = videoCapture.read()
    grayscaleFrameNDArray = cv2.cvtColor(frameNDArray, cv2.COLOR_BGR2GRAY)
    featureNDArray, statusNDArray, errorNDArray = cv2.calcOpticalFlowPyrLK(previousGrayscaleFrameNDArray, grayscaleFrameNDArray, previousFeatureNDArray, None, **lkParameterDictionary)
    pointNDArray         = featureNDArray[statusNDArray == 1]
    previousPointNDArray = previousFeatureNDArray[statusNDArray == 1]
    for i, (point, previousPoint) in enumerate(zip(pointNDArray, previousPointNDArray)):
        x, y = point.ravel()
        previousX, previousY = previousPoint.ravel()
        #cv2.line(maskNDArray, (x, y), (previousX, previousY), colorNDArray[i].tolist(), 2)
        cv2.circle(frameNDArray, (x, y), 5, colorNDArray[i].tolist(), -1)
    imageNDArray = cv2.add(frameNDArray, maskNDArray)
    cv2.imshow("video", imageNDArray)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
    previousGrayscaleFrameNDArray = grayscaleFrameNDArray.copy()
    count = count + 1
    if count % 100 == 0:
        previousFeatureNDArray = cv2.goodFeaturesToTrack(previousGrayscaleFrameNDArray, mask = None, **featureParameterDictionary)
    else:
        previousFeatureNDArray = pointNDArray.reshape(-1, 1, 2)
videoCapture.release()
cv2.destroyAllWindows()
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요