Cov txheej txheem:

Yooj Yim Xim-Tshawb Pom Siv OpenCV: 6 Cov Kauj Ruam
Yooj Yim Xim-Tshawb Pom Siv OpenCV: 6 Cov Kauj Ruam

Video: Yooj Yim Xim-Tshawb Pom Siv OpenCV: 6 Cov Kauj Ruam

Video: Yooj Yim Xim-Tshawb Pom Siv OpenCV: 6 Cov Kauj Ruam
Video: Computer Vision with Python! Resizing Images 2024, Hlis ntuj nqeg
Anonim
Yooj Yim Xim-Tshawb Pom Siv OpenCV
Yooj Yim Xim-Tshawb Pom Siv OpenCV

Nyob zoo! Hnub no kuv yuav qhia txoj hauv kev yooj yim txhawm rau txheeb xyuas cov xim los ntawm cov vis dis aus siv OpenCV thiab nab hab sej.

Yeej kuv yuav cia li sim cov xim uas xav tau tam sim no hauv kab tom qab lossis tsis yog thiab siv OpenCV cov qauv kuv yuav npog thaj tsam ntawd thiab ib txhij nthuav tawm cov thav duab.

Kauj Ruam 1: Cov Ntaub Ntawv Header

Cov ntaub ntawv Header
Cov ntaub ntawv Header

Tam sim no ntawm no kuv tau siv ob daim ntawv header uas yog cv2 thiab NumPy. Yeej cv2 yog OpenCV lub tsev qiv ntawv uas thauj tag nrho c ++ cov ntaub ntawv uas tseem ceeb thaum siv cov lus txib hauv cov lej (nws muaj tag nrho cov lus txhais).

Thiab Numpy yog lub tsev qiv ntawv nab nab uas yog qhov tseem ceeb rau khaws cia ntau qhov ntau. Peb yuav siv los khaws peb cov xim sib xyaw ua ke.

Thiab tus lej raws li np ib txwm pab peb cov lej kom luv me ntsis los ntawm kev siv np txhua lub sijhawm tsis txhob siv tus lej.

Kauj ruam 2: Kev ntes cov yees duab

Kev ntes tus yees duab
Kev ntes tus yees duab

Nov yog qhov yooj yim zoo nkauj thaum siv nab hab sej. Ntawm no peb tsuas yog xav tau qhib lub kaw video kom nws tuaj yeem pib kaw cov ntas.

Tam sim no tus nqi sab hauv VideoCapture qhia lub koob yees duab, hauv kuv cov ntaub ntawv lub koob yees duab txuas nrog kuv lub laptop, yog li 0.

Koj tuaj yeem mus zoo ib yam rau 1 rau lub koob yees duab thib ob thiab ntxiv rau. VideoCapture tsim lub hom phiaj rau nws.

Kauj Ruam 3: Txheeb Tus Ncej thiab Txiav Xim

Capturing Frame thiab Txhais Xim
Capturing Frame thiab Txhais Xim

Tam sim no ntawm no peb yuav tsum tau ua ib yam dab tsi kom peb tuaj yeem ntes lub thav duab ntawm cov vis dis aus uas yuav pab peb rho cov duab thiab peb tuaj yeem ua haujlwm raws li qhov xav tau.

"thaum" voj yuav pab peb khiav lub voj rau peb lub sijhawm xav tau. Tam sim no "_, thav duab = cap.read ()" tau siv los tshuaj xyuas qhov siv tau ntawm tus ncej raug thiab khaws nws. "cap.read () yog qhov sib txawv boolean thiab rov qab muaj tseeb yog tias cov kab ntawv tau nyeem raug thiab yog tias koj tsis tau txais cov thav ntawv nws yuav tsis qhia qhov ua yuam kev, koj yuav yooj yim tsis tau.

Tam sim no kab 11 thiab kab 12 pib txhais qhov ntau ntawm cov xim peb xav tau los tshuaj xyuas. Rau qhov no, Kuv tau siv xim xiav.

Koj tuaj yeem txuas ntxiv nrog cov xim twg uas koj xav tau tsuas yog ntaus nqi BGR rau cov xim tshwj xeeb. Nws yog qhov zoo dua los txheeb xyuas ob lub arrays uas siv cov lej numpy raws li kuaj pom cov xim tshwj xeeb hauv lub ntiaj teb tiag yuav tsis ua haujlwm rau peb lub hom phiaj es peb yuav txiav txim siab ntau yam xim xiav kom nws pom nyob hauv thaj tsam.

Txog qhov no, Kuv tau piav qhia ob qhov sib txawv khaws cia qis dua BGR qhov tseem ceeb thiab qhov tseem ceeb BGR siab dua.

Kauj Ruam 4: Ntxuav thiab rho tawm

Npog thiab Extracting
Npog thiab Extracting

Tam sim no ntawm no los txog lub luag haujlwm tseem ceeb ntawm kev npog lub thav duab thiab tshem tawm cov xim ntawm lub thav duab. Kuv siv cov lus txib ua ntej tam sim no hauv lub tsev qiv ntawv hauv OpenCV los ua lub ntsej muag. Yeej qhov ncauj qhov ntswg yog txheej txheem tshem tawm qee feem ntawm tus thav duab, piv txwv li peb yuav tshem tawm cov pixels uas nws cov xim BGR qhov txiaj ntsig uas tsis dag hauv cov xim uas tau hais tseg thiab qhov no yog ua los ntawm cv2.inRange. Tom qab ntawd, peb siv cov xim sib txawv rau cov duab ua ntsej muag nyob ntawm qhov txiaj ntsig pixel thiab rau qhov no, peb yuav siv cv2.bitwise_and, Nws yuav yooj yim muab cov xim rau thaj tsam npog ntsej muag nyob ntawm daim npog ntsej muag thiab xim ntau yam.

Link cv 2 bitwise_and:

Kauj Ruam 5: Thaum kawg Tso Tawm

Thaum kawg Zaub!
Thaum kawg Zaub!

Ntawm no kuv tau siv qhov yooj yim cv2.imshow () rau tso rau txhua tus ncej ua duab. Txij li thaum kuv muaj cov ntaub ntawv thav duab khaws cia hauv qhov sib txawv Kuv tuaj yeem khaws lawv hauv imshow (). Ntawm no kuv tau nthuav tawm tag nrho peb lub thav duab, thawj, npog ntsej muag, thiab xim.

Tam sim no peb yuav tsum tawm ntawm lub voj thaum. Txog qhov no, peb tuaj yeem yooj yim siv cv2.wait. Key (). Yeej nws qhia lub sijhawm tos ua ntej teb. Yog li yog tias koj dhau 0 nws yuav tos tsis kawg thiab 0xFF qhia tus qauv tsim yog 64 ntsis. "ord ()" qhia meej txog tus cwj pwm uas thaum nias yuav ua tiav cov lus txib tawg hauv yog tias thaiv thiab nws yuav tawm ntawm lub voj.

Tom qab ntawd cap.release () kaw lub kaw video thiab cv2.destroyAllWindows () kaw txhua lub qhov rais qhib.

Yog tias koj muaj teeb meem, thov qhia rau kuv paub.

Txuas mus rau qhov chaws:

Pom zoo: