Cov txheej txheem:

Opencv Object Tracking: 3 Kauj Ruam
Opencv Object Tracking: 3 Kauj Ruam

Video: Opencv Object Tracking: 3 Kauj Ruam

Video: Opencv Object Tracking: 3 Kauj Ruam
Video: Object tracking using Homography - OpenCV 3.4 with python 3 Tutorial 34 2024, Hlis ntuj nqeg
Anonim
Opencv Object Tracking
Opencv Object Tracking

Kev txheeb xyuas cov khoom txav yog cov txheej txheem siv hauv kev pom lub computer thiab ua cov duab. Ntau kab sib law liag los ntawm cov vis dis aus raug sib piv los ntawm ntau txoj hauv kev los txiav txim siab yog tias pom cov khoom txav mus.

Kev txheeb xyuas cov khoom txav tau siv rau ntau yam kev siv xws li kev saib xyuas video, kev lees paub kev ua haujlwm, kev saib xyuas xwm txheej, kev nyab xeeb hauv tshav dav hlau, saib xyuas kev tiv thaiv raws ntug dej hiav txwv thiab lwm yam.

Kev txheeb xyuas qhov khoom txav yog kom paub txog lub cev txav ntawm ib yam khoom hauv ib qho chaw lossis thaj chaw. [2] Los ntawm kev ua segmentation ntawm cov khoom txav chaw thiab thaj chaw nyob ruaj khov lossis thaj chaw, cov khoom txav mus los tuaj yeem taug qab thiab yog li tuaj yeem tshuaj xyuas tom qab. Txhawm rau ua tiav qhov no, txiav txim siab cov vis dis aus yog tus qauv tsim los ntawm ib tus thav duab, txav cov khoom pom yog nrhiav pom lub hauv ntej txav mus rau lub hom phiaj (ib qho), hauv txhua kab duab video lossis tsuas yog thaum lub hom phiaj txav qhia pom thawj zaug hauv daim vis dis aus.

Kuv yuav siv Opnecv thiab Python ua ke txhawm rau txheeb xyuas thiab taug qab cov khoom raws li xim

Kauj Ruam 1: Teeb duab duab plaub rau ntawm Lub Cim Pom Zoo

yog tias koj lub pc tsis muaj nab hab sej lossis opencv thov ua raws li cov hauv qab no hauv kev tsim kho

nov yog tus nab npawb nab npawb:

import cv2import numpy as np

cap = cv2. VideoCapture (0)

thaum Muaj Tseeb:

_, ncej = cap.read () hsv = cv2.cvtColor (ncej, cv2. COLOR_BGR2HSV)

lower_yellow = np.array ([20, 110, 110])

upper_yellow = np.array ([40, 255, 255])

yellow_mask = cv2.inRange (hsv, qis dua -yellow, Upper_yellow)

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

rau contour nyob rau hauv contours:

cheeb tsam = cv2.contourArea (contour)

yog (thaj tsam> 800):

x, y, w, h = cv2.boundingRect (contour) thav duab = cv2.rectangle (thav duab, (x, y), (x+w, y+h), (0, 0, 255), 10)

cv2.imshow ("taug qab", thav duab)

k = cv2.waitKey (5) & 0XFF

yog tias k == 27: so

cv2.destroyAllWindows ()

cap.release () thov

Kauj Ruam 2: Txheeb Tawm Txoj Hauv Kev Uas Lub Hom Phiaj Tau Mooved

taug txoj kev:

rau kuv hauv ntau (1, len (center_points)): b = random.randint (230, 255) g = random.randint (100, 255) r = random.randint (100, 255) yog lej.sqrt (((center_points [i - 1] [0] - center_points [0]) ** 2) + ((center_points [i - 1] [1] - center_points [1]) ** 2)) <= 50: cv2.line (ncej, center_points [i - 1], center_points , (b, g, r), 4)

Kauj Ruam 3: Ua ke Ob Qhov Cai

kuv yuav mus koom ob txoj cai

ntshuam cv2import numpy li np ntshuam random los ntawm kev sau ntshuam deque

cap = cv2. VideoCapture (1)

# Txhawm rau taug qab txhua kis uas cov khoom mus xyuas center_points = deque ()

thaum Muaj Tseeb:

# Nyeem thiab tig tus ncej _, thav duab = cap.read () thav duab = cv2.flip (thav duab, 1)

# Qhov muag plooj me ntsis

blur_frame = cv2. GaussianBlur (thav duab, (7, 7), 0)

# Hloov los ntawm BGR rau HSV hom xim

hsv = cv2.cvtColor (blur_frame, cv2. COLOR_BGR2HSV)

# Txheeb cais qis thiab sab saud ntawm hsv xim kom pom. Xiav ntawm no

lower_blue = np.array ([100, 50, 50]) upper_blue = np.array ([140, 255, 255]) mask = cv2.inRange (hsv, lower_blue, upper_blue)

# Ua cov elliptical kernel

kernel = cv2.getStructuringElement (cv2. MORPH_ELLIPSE, (15, 15))

# Qhib morph (yaig tom qab nthuav dav)

mask = cv2.morphologyEx (daim npog, cv2. MORPH_OPEN, kernel)

# Nrhiav txhua daim duab

contours, hierarchy = cv2.findContours (mask.copy (), cv2. RETR_LIST, cv2. CHAIN_APPROX_SIMPLE) [-2:]

yog len (contours)> 0:

# Nrhiav qhov loj tshaj contour biggest_contour = max (contours, qhov tseem ceeb = cv2.contourArea)

# Nrhiav qhov chaw nruab nrab ntawm daim duab thiab kos lub voj voog

moments = cv2.moments (biggest_contour) centre_of_contour = (int (moments ['m10'] / moments ['m00']), int (moments ['m01'] / moments ['m00'])) cv2.circle (thav duab, centre_of_contour, 5, (0, 0, 255), -1)

# Ciam lub contour nrog lub voj voog

ellipse = cv2.fitEllipse (biggest_contour) cv2.ellipse (thav duab, ellipse, (0, 255, 255), 2)

# Txuag qhov chaw nruab nrab ntawm daim phiaj kom peb kos kab taug qab nws

center_points.appendleft (centre_of_contour)

# Kos kab los ntawm cov ntsiab lus nruab nrab ntawm daim duab

rau kuv hauv ntau (1, len (center_points)): b = random.randint (230, 255) g = random.randint (100, 255) r = random.randint (100, 255) yog lej.sqrt (((center_points [i - 1] [0] - center_points [0]) ** 2) + ((center_points [i - 1] [1] - center_points [1]) ** 2)) <= 50: cv2.line (ncej, center_points [i - 1], center_points , (b, g, r), 4)

cv2.imshow ('thawj', thav duab)

cv2.imshow ('daim npog', daim npog)

k = cv2.waitKey (5) & 0xFF

yog tias k == 27: so

cv2.destroyAllWindows ()

cap.release () thov

Pom zoo: