Cov txheej txheem:

Kev Tshawb Nrhiav Xim hauv Python Siv OpenCV: 8 Cov Kauj Ruam
Kev Tshawb Nrhiav Xim hauv Python Siv OpenCV: 8 Cov Kauj Ruam

Video: Kev Tshawb Nrhiav Xim hauv Python Siv OpenCV: 8 Cov Kauj Ruam

Video: Kev Tshawb Nrhiav Xim hauv Python Siv OpenCV: 8 Cov Kauj Ruam
Video: Computer Vision with Python! Resizing Images 2024, Hlis ntuj nqeg
Anonim
Image
Image

Nyob zoo! Cov lus qhia no yog siv los qhia nrog yuav ua li cas rho tawm cov xim tshwj xeeb los ntawm cov duab hauv nab nab siv openCV lub tsev qiv ntawv. Yog tias koj tshiab rau cov txheej txheem no ces tsis txhob txhawj xeeb, thaum kawg ntawm phau ntawv qhia no koj yuav tuaj yeem ua haujlwm rau koj tus kheej qhov kev tshuaj xyuas xim.

Nram qab no yog cov haujlwm lossis peb tuaj yeem hais cov txheej txheem uas koj yuav kawm, 1. Yuav nyeem tus duab li cas

2. Yuav tsim Track Bars li cas

3. Yuav ua li cas kho tus nqi ntawm Hawj txawm, Saturation thiab tus nqi ntawm tus duab siv cov kab tuav

4. Thiab tom qab ntawd yuav muaj koj qhov tso tawm zaum kawg

Koj tuaj yeem saib cov vis dis aus ntawm cov zis uas kuv tau txuas hauv qab no.

Yog li cia peb pib

Khoom siv

  • Nab hab sej 3
  • openCV lub tsev qiv ntawv
  • numpy tsev qiv ntawv

Kauj ruam 1: Ntshuam Cov Tsev Qiv Ntawv

Ntshuam tsev qiv ntawv
Ntshuam tsev qiv ntawv

Cov duab yog daj ferrari raws li qhia thiab peb yuav ua haujlwm kom rho tawm tsuas yog xim daj los ntawm daim duab ntawd

Thawj kauj ruam yuav tau ntshuam peb cov tsev qiv ntawv

1. Nrog rau openCV lub tsev qiv ntawv. Nws tau hu ua cv2 hauv sej

2. Xws li numpy tsev qiv ntawv li np. Qhov "raws" tso cai rau peb rau peb tus lej li np yog li tsis tas yuav sau tus lej ntau thiab ntau dua

Kauj Ruam 2: Tsim Tuav Kab

Tsim Taug Kev Tuav
Tsim Taug Kev Tuav

Taug qab Tuav tau tsim los kho tus nqi ntawm Hawj txawm, Txaus siab thiab tus nqi hauv tus duab.

cv2.namedWindow ("TrackBars") Kab kab ntawv no tau siv los tsim lub qhov rai tso tawm tshiab thiab lub npe ntawm lub qhov rais tau muab ua TrackBars (Koj tuaj yeem muab lub npe koj xav tau)

cv2.resizeWindow ("TrackBars", 600, 250) Lub luag haujlwm no yog siv los hloov lub qhov rai. "TrackBars" yog lub qhov rai twg koj xav hloov pauv txij li kuv xav hloov kho qhov rais TrackBars Kuv tau sau lub npe ntawd. Ua raws li ob tus lej. Cov ob tus lej no yog qhov dav thiab qhov siab. Koj tuaj yeem ua si ib puag ncig nrog ob tus lej txhawm rau hloov qhov loj

Kauj Ruam 3: Tsim TrackBars rau Hawj Hawj, Saturation thiab Tus Nqi

Tsim TrackBars rau Hawj txawm, Saturation thiab tus nqi
Tsim TrackBars rau Hawj txawm, Saturation thiab tus nqi
Tsim TrackBars rau Hawj txawm, Saturation thiab tus nqi
Tsim TrackBars rau Hawj txawm, Saturation thiab tus nqi

Tam sim no peb yuav tsim tag nrho 6 TrackBars rau Hawj txawm, Saturation thiab tus nqi. Txhua tus yuav muaj ob qho xws li 1 rau yam tsawg kawg nkaus thiab 1 rau qhov siab tshaj. Peb yuav siv createTrackbar ua haujlwm ntawm openCV. Ua ntej peb yuav pom cov syntax ntawm txoj haujlwm no.

cv2.createTrackbar ("WINDOWNAME", "MAINWINDOWNAME", "RANGE"). Qhov no tej zaum yuav tsis meej pem tab sis tsis txhob txhawj xeeb peb yuav hla txhua qhov thiab txhua kauj ruam. Khaws ib yam hauv siab tias hauv openCV qhov tseem ceeb ntawm cov hue yog 179, saturation yog 255 thiab tus nqi yog 255

1. Tsim TrackBar rau hue min:

cv2.createTrackbar ("Hue min", "TrackBars", 0, 179, khoob)

Hauv Hue min no yog lub npe hu ua trackbar, TrackBars yog lub qhov rai loj, 0 yog txoj haujlwm uas peb tus swb yuav yog thiab 179 yog qhov ntau yam txhais tau tias silder yuav txav los ntawm 0-179

2. Tsim TrackBar rau hue max:

cv2.createTrackbar ("Hawj max", "TrackBars", 179, 179, khoob)

Hauv Hue max no yog lub npe ntawm lub npe, TrackBars yog lub qhov rai loj, 179 yog txoj haujlwm uas peb tus swb yuav yog thiab 179 yog qhov ntau tshaj plaws txhais tau tias silder yuav txav los ntawm 179-0

3. Zoo sib xws rov ua cov kauj ruam rau zaum min, zaum max, val min thiab val max raws li qhia hauv daim duab

Cov duab nrog cov keeb kwm yav dhau dawb yog cov duab tso tawm. Qhov no yuav ua li cas koj cov kab tuav yuav zoo li

Kauj Ruam 4: Yuav Ua Li Cas Nyeem thiab Hloov Duab

Yuav Ua Li Cas Nyeem thiab Hloov Duab
Yuav Ua Li Cas Nyeem thiab Hloov Duab

cv2.imread () tso cai rau koj nyeem cov duab. Ib qho tseem ceeb xav tias koj yuav tsum nco ntsoov tias qhov chaw ntawm koj daim duab yuav tsum nyob hauv tib daim nplaub tshev uas qhov kev zov me nyuam tau txais kev cawmdim. Peb yuav muab tso rau hauv lub voj vim tias nws yuav tsum khiav mus txog thaum nyeem cov duab lossis peb tuaj yeem hais kom txog thaum qhov xwm txheej muaj tseeb

img = cv2.imread ("ferrari.jpg") Cov.

  • Hauv qhov no kuv tau tsim lub npe sib txawv "img" uas kuv khaws cov duab
  • Sab hauv cv2.imread sau lub npe duab nrog nws txuas ntxiv hauv ob nqe lus

Txhawm rau hloov cov duab peb yuav siv cv2.resize muaj nuj nqi. Qhov no yog nyob ntawm yeem, yog tias koj xav hloov qhov loj ces koj tuaj yeem siv qhov haujlwm no

Sab hauv cv2.resize xub sau cov npe sib txawv uas cov duab khaws tseg thiab tom qab ntawd nws qhov dav thiab qhov siab

Kauj Ruam 5: Nyeem Taug Kev Bar Qhov Muaj Nqis Los Thov Nws Rau Duab

Nyeem Taug Kev Bar Qhov Muaj Nqis Los Thov Nws Rau Duab
Nyeem Taug Kev Bar Qhov Muaj Nqis Los Thov Nws Rau Duab
Nyeem Taug Kev Bar Qhov Muaj Nqis Los Thov Nws Rau Duab
Nyeem Taug Kev Bar Qhov Muaj Nqis Los Thov Nws Rau Duab

Ok tam sim no peb tab tom nyeem cov ntawv trackbar qhov tseem ceeb yog li peb tuaj yeem siv nws rau peb cov duab. Peb yuav tau txais qhov txiaj ntsig siv cv2.getTrackbarPos () muaj nuj nqi.

Wb pib nrog qhov ntawd…

h_min = cv2.getTrackbarPos ("Hue min", "TrackBars")

Hauv cov lus saum toj no kuv tab tom tsim lub npe sib txawv h_min uas kuv yuav khaws tus nqi ntawm Hue min. Yog li sab hauv cv2.getTrackbarPos 1st kev sib cav yuav yog "Hue min" vim kuv xav tau qhov tseem ceeb ntawm cov hue min (Cov ntawv sau yuav tsum zoo ib yam ib yam li nws yog createTrackbar muaj nuj nqi) thiab qhov kev sib cav zaum thib ob yuav yog lub npe ntawm lub qhov rai trackbar uas nws yog.

  • Rov ua cov txheej txheem qub rau h_max thiab so ntawm cov haujlwm raws li qhia hauv daim duab saum toj no thiab tom qab ntawd sau tag nrho cov txiaj ntsig siv luam ntawv ()
  • Cov zis tau pom hauv daim duab thib ob. Nws tau luam tawm qhov tseem ceeb ntawm h_min, h_max, s_min, s_max, v_min, s_max

Kauj Ruam 6: Tso Duab thiab Teeb Upper thiab Lower Limit

Muab duab thiab teeb tsa Upper thiab Lower Limit
Muab duab thiab teeb tsa Upper thiab Lower Limit

Tam sim no peb muaj tus min thiab tus nqi siab tshaj ntawm cov hue, saturation thiab tus nqi peb yuav siv tus nqi no los lim tawm cov duab kom peb tuaj yeem ua cov xim tshwj xeeb ntawm cov duab.

Peb yuav tsim daim npog ntsej muag rau qhov no siv cv2.inRange ua haujlwm. Thiab ua ntej ntawd peb yuav teeb tsa sab saud thiab qis dua ntawm cov hue, saturation thiab tus nqi

Yog li tsim lub npe sib txawv "qis dua" thiab siv cov lej numpy array teeb tsa qhov ntau ntawm min rau txhua 3 raws li hauv qab no

lower = np.array ([h_min, s_min, v_min])

Rov ua tib cov kauj ruam rau sab saud

upper = np.array ([h_max, s_max, v_max])

Tam sim no peb yuav tsim daim npog ntsej muag raws li hauv qab no

daim npog = cv2.inRange (hloov pauv, qis dua, sab saud) Sab hauv cv2.inRang thawj qhov kev sib cav yuav yog qhov sib txawv uas kuv cov duab kawg tau khaws cia, qhov kev sib cav zaum thib ob yuav qis dua thiab qhov kev sib cav zaum thib 3 yuav yog qhov txwv siab tshaj.

Tam sim no peb tab tom yuav tso saib cov duab tseem ceeb thiab daim npog qhov ncauj. Txhawm rau tso saib peb yuav siv cv2.imshow () muaj nuj nqi

cv2.imshow ("img", resize) Qhov no yog los tso saib cov duab tseem ceeb. 1st kev sib cav yog lub npe ntawm lub qhov rais koj tuaj yeem muab lub npe koj xav tau thiab 2nd kev sib cav yog qhov sib txawv uas kuv cov duab tseem ceeb tau khaws cia uas koj xav tso tawm.

Zoo sib xws ua cov kauj ruam rau daim npog ntsej muag

cv2.imshow ("Output", daim npog)

Kauj Ruam 7: Tam sim no Kawg Kawg

Tam sim no Kawg Kawg
Tam sim no Kawg Kawg

Hauv theem kawg no peb yuav rho xim ntawm lub tsheb thiab tso saib.

Kuv tau tsim qhov hloov pauv lub npe tshwm sim. Ib zaug ntxiv koj tuaj yeem muab lub npe koj xav tau. Yog li peb yuav siv cv2.bitwise_and () ua haujlwm uas peb yuav ntxiv rau cov duab ua ke thiab tsim cov duab tshiab. Thiab nyob qhov twg cov pixels hauv ob daim duab tam sim no nws yuav siv qhov ntawd yog lossis "1".

result = cv2.bitwise_and (hloov pauv, hloov pauv, daim npog = daim npog)

  • Hauv qhov kev sib cav zaum 1 yuav yog peb daim duab
  • Kev sib cav zaum thib 2 tseem yuav yog peb daim duab qub tab sis ua raws daim npog ntsej muag uas peb tsim ua ntej
  • Thiab thaum kawg tsuas yog tso saib qhov tshwm sim siv imshow muaj nuj nqi

Tsuas yog luam tawm cov kauj ruam kawg no nws tsuas yog ncua sijhawm thiab koj tuaj yeem tawm ntawm lub qhov rais tawm los ntawm nias "a" ntawm cov keyboard

Kauj Ruam 8: Cov Lus Kawg

Pom zoo: