docente a.a. 2019/2020 domenico daniele bloisi opencv (python)
TRANSCRIPT
![Page 1: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/1.jpg)
OpenCV(Python)
Ap
rile
20
20
Corso di Visione e PercezioneA.A. 2019/2020
UNIVERSITÀ DEGLI STUDI
DELLA BASILICATA
DocenteDomenico Daniele Bloisi
![Page 2: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/2.jpg)
• Home page del corsohttp://web.unibas.it/bloisi/corsi/visione-e-percezione.html
• Docente: Domenico Daniele Bloisi
• Periodo: II semestre marzo 2020 – giugno 2020
Martedì 17:00-19:00 (Aula GUGLIELMINI)Mercoledì 8:30-10:30 (Aula GUGLIELMINI)
Il corso
![Page 3: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/3.jpg)
• OpenCV (Open Source Computer Vision Library) è una libreria software open source per la computer vision e il machine learning
• Distribuita con licensa BSD (è possibile utilizzarla per fini commerciali)
• Più di 2500 algoritmi disponibili• Più di 47000 utenti nella community• Più di 14 milioni di download
OpenCV
![Page 4: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/4.jpg)
• Può essere utilizzata con C++, Python, Java e MATLAB
• Può essere installata su Windows, Linux, Android e Mac OS
• Dispone di interface per CUDA e OpenCL
• Viene usata da Google, Yahoo, Microsoft, Intel, IBM, Sony, Honda, Toyota
OpenCV
![Page 5: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/5.jpg)
• OpenCV was started at Intel in 1999 by Gary Bradsky, and the first release came out in 2000. Vadim Pisarevsky joined Gary Bradsky to manage Intel's Russian software OpenCV team.
• In 2005, OpenCV was used on Stanley, the vehicle that won the 2005 DARPA Grand Challenge.
• Later, its active development continued under the support of Willow Garage with Gary Bradsky and Vadim Pisarevskyleading the project.
OpenCV - storia
![Page 6: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/6.jpg)
• Home: https://opencv.org/• Documentatation: https://docs.opencv.org/• Q&A forum: http://answers.opencv.org• GitHub: https://github.com/opencv/
OpenCV - links
![Page 7: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/7.jpg)
OpenCV - moduli
OpenCV ha una struttura modulare
I principali moduli sono:• core• imgproc• video• calib3d• features2d• objdetect• highgui
![Page 8: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/8.jpg)
OpenCV – core e imgproc
Core functionality (core)A compact module defining basic data structures, including the dense multi-dimensional array Mat and basic functions used by all other modules.
Image Processing (imgproc)An image processing module that includes linear and non-linear image filtering, geometrical image transformations (resize, affine and perspective warping, generic table-based remapping), color space conversion, histograms, and so on.
![Page 9: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/9.jpg)
OpenCV – video e calib3d
Video Analysis (video)A video analysis module that includes motion estimation, background subtraction, and object tracking algorithms.
Camera Calibration and 3D Reconstruction (calib3d)Basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence algorithms, and elements of 3D reconstruction.
![Page 10: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/10.jpg)
OpenCV – features2d e objdetect
2D Features Framework (features2d)Salient feature detectors, descriptors, and descriptor matchers.
Object Detection (objdetect)Detection of objects and instances of the predefined classes (for example, faces, eyes, mugs, people, cars, and so on).
![Page 11: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/11.jpg)
OpenCV – highgui e videoio
High-level GUI (highgui) an easy-to-use interface to simple UI capabilities.
Video I/O (videoio)An easy-to-use interface to video capturing and video codecs.
![Page 12: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/12.jpg)
OpenCV – Python• Python is slower compared to C++ or C. Python is built for its simplicity,
portability and moreover, creativity where users need to worry only about their algorithm, not programming troubles.
• Python-OpenCV is just a wrapper around the original C/C++ code. It is normally used for combining best features of both the languages. Performance of C/C++ & Simplicity of Python.
• So when you call a function in OpenCV from Python, what actually run is underlying C/C++ source.
• Performance penalty is < 4%Source: Mašinska vizija
![Page 13: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/13.jpg)
OpenCV Timeline
https://github.com/opencv/opencv/wiki/images/OpenCV3_0_CVPR_2014.pptx
Version Released Reason Lifetime
pre 1.0 2000 (first alpha)
- 6 years
1.0 2006 (ChangeLog)
maturity 3 years
2.0 2009(ChangeLog)
C++ API >3 years
3.0 2014 several (next level maturity, ...)
4.0 Nov. 2018 better DNN support
![Page 14: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/14.jpg)
OpenCV in Colab
La versione di OpenCVattualmente disponibile in Google Colab è la 4.1.2
![Page 16: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/16.jpg)
OpenCV-Python Tutorials
OpenCV fornisce una serie di tutorial specifici per Pythonche possono essere utilizzati per imparare ad utilizzare la libreria attraverso esempi pratici
![Page 17: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/17.jpg)
Load an image in Colab
![Page 18: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/18.jpg)
Load an image in Colabhttp://portale.unibas.it/contents/instance1/images/logo-unibas.png
![Page 19: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/19.jpg)
Load an image in Colab
![Page 20: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/20.jpg)
Read an image with OpenCV
warningColor image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV.
Source image
![Page 21: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/21.jpg)
Images are NumPy arrays
Images in OpenCV-Python are NumPy arrays
![Page 22: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/22.jpg)
RGB visualization in Matplotlib
![Page 23: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/23.jpg)
Accessing and Modifying pixel values
warningNumpy is a optimized library for fast array calculations. So simply accessing each and every pixel values and modifying it will be very slow and it is discouraged.
![Page 24: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/24.jpg)
item e itemset
I metodi Numpyarray.item()array.itemset()sono considerati migliori per accedere agli elementi di una immagine.Tuttavia, se si vuole accedere a tutti e tre i canali B,G,R è necessario ripetere la chiamata tre volte separatamente.
![Page 25: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/25.jpg)
Accessing Image Properties
![Page 26: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/26.jpg)
Esercizio
Ricolorare la figura in rosso nella immaginehttps://web.unibas.it/bloisi/corsi/images/forme.pngcon il colore verde
![Page 27: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/27.jpg)
Esercizio - soluzione
redgreen
blue
![Page 28: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/28.jpg)
inRange
https://docs.opencv.org/4.1.2/da/d97/tutorial_threshold_inRange.html
mask
![Page 29: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/29.jpg)
Image ROI
![Page 30: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/30.jpg)
Changing Color-space
Ci sono tantissimi metodi per cambiare il color-space disponibili in OpenCV.
![Page 31: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/31.jpg)
BGR2GRAY
![Page 32: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/32.jpg)
Grayscale conversion
tasto destro del mouse
![Page 33: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/33.jpg)
Read an image from URL with OpenCV
![Page 34: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/34.jpg)
BGR2RGB
![Page 35: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/35.jpg)
HSV color spaceThe HSV color space has the following three components1. H – Hue (Dominant Wavelength)2. S – Saturation (Purity/shades of the color)3. V – Value (Intensity)
Observations• The H component is very similar in both
the images which indicates the color information is intact even under illumination changes
• The S component is also very similar in both images
• The V component captures the amount of light falling on it thus it changes due to illumination changes
https://www.learnopencv.com/color-spaces-in-opencv-cpp-python/
![Page 36: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/36.jpg)
HSV color-space
HSV is a projection of the RGB space
RGB cube HSV top view HSV cone
Source: Donald House
![Page 37: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/37.jpg)
Hue
Source: Donald House
Hue, an angular measure (0 … 360)
Hue range is [0,179] in OpenCV
![Page 38: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/38.jpg)
Saturation
Source: Donald HouseSaturation range is [0,255] in OpenCV
Saturation, a fractional measure (0.0 … 1.0)
![Page 39: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/39.jpg)
Value
Source: Donald HouseValue range is [0,255] in OpenCV
Value, a fractional measure (0.0 … 1.0)
![Page 40: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/40.jpg)
HSV conversion
![Page 41: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/41.jpg)
H channel
![Page 42: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/42.jpg)
S channel
![Page 43: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/43.jpg)
V channel
![Page 44: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/44.jpg)
Merge
![Page 45: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/45.jpg)
Esercizio 7
Applicare all’immagine https://web.unibas.it/bloisi/corsi/images/forme.pngle operazioni di• erosion• dilation• aperture• closing
![Page 46: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/46.jpg)
cv2_imshow
![Page 47: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/47.jpg)
IMREAD_GRAYSCALE
![Page 48: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/48.jpg)
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
Esercizio 7 – soluzione
![Page 49: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/49.jpg)
Esercizio 7 – soluzione 2
https://docs.opencv.org/master/d0/d86/tutorial_py_image_arithmetics.html
![Page 50: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/50.jpg)
Esercizio 8
Applicare all’immagine https://web.unibas.it/bloisi/corsi/images/forme.pngil metodo di thresholding di Otsu
![Page 51: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/51.jpg)
Esercizio 8 – soluzione
Otsu's threshold: 103.0
![Page 52: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/52.jpg)
Esercizio 9
Estrarre i contorni dall’immaginehttps://web.unibas.it/bloisi/corsi/images/forme.png
![Page 53: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/53.jpg)
Esercizio 9 – soluzione
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html
![Page 54: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/54.jpg)
Harris corner detection OpenCV
https://docs.opencv.org/4.1.2/d4/d7d/tutorial_harris_detector.html
![Page 55: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/55.jpg)
Harris corner detection OpenCV
https://docs.opencv.org/4.1.2/d4/d7d/tutorial_harris_detector.html
![Page 56: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/56.jpg)
Harris corner detection OpenCV
https://docs.opencv.org/4.1.2/d4/d7d/tutorial_harris_detector.html
![Page 57: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/57.jpg)
Harris corner detection OpenCV
![Page 58: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/58.jpg)
Esercizio 10
Usare la funzione OpenCV goodFeaturesToTrack()per trovare i corner nell’immaginehttps://web.unibas.it/bloisi/corsi/images/forme.png
Suggerimento: si veda il tutorial a questo indirizzohttps://docs.opencv.org/4.1.2/d4/d8c/tutorial_py_shi_tomasi.html
![Page 59: Docente A.A. 2019/2020 Domenico Daniele Bloisi OpenCV (Python)](https://reader036.vdocuments.pub/reader036/viewer/2022071003/62c874d7871960343554ee56/html5/thumbnails/59.jpg)
OpenCV(Python)
Ap
rile
20
20
Corso di Visione e PercezioneA.A. 2019/2020
UNIVERSITÀ DEGLI STUDI
DELLA BASILICATA
DocenteDomenico Daniele Bloisi