Εδώ βλέπετε τις διαφορές μεταξύ της επιλεγμένης έκδοσης και της τρέχουσας έκδοσης της σελίδας.
| Προηγούμενος έλεγχος και από τις δύο πλευρές Προηγούμενη αναθεώρηση Επόμενη αναθεώρηση | Προηγούμενη αναθεώρηση | ||
|
opencv [2020/02/19 10:01] chiossif |
opencv [2020/11/21 09:52] (τρέχουσα) |
||
|---|---|---|---|
| Γραμμή 2: | Γραμμή 2: | ||
| Όσοι οι πιστοί προσέλθετε :-) | Όσοι οι πιστοί προσέλθετε :-) | ||
| + | |||
| + | ====== Εισαγωγή στην OpenCV ====== | ||
| + | |||
| + | |||
| + | =====Τι είναι η OpenCV===== | ||
| + | |||
| + | Η OpenCV(Open-source Computer Vision) είναι μία βιβλιοθήκη που περιέχει συναρτήσεις, | ||
| + | |||
| + | Επίσης η OpenCV υποστηρίζει συστήματα Deep Learning, όπως είναι το TensorFlow, Torch/ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | =====Βασικές εφαρμογές της OpenCV===== | ||
| + | |||
| + | Όπως και με κάθε άλλη γλώσσα προγραμματισμού, | ||
| + | |||
| + | Ας ξεκινήσουμε λοιπόν. | ||
| + | |||
| + | ====Αλλαγή Αντίθεσης - Φωτεινότητας Εικόνας==== | ||
| + | |||
| + | Ο υπολογιστής, | ||
| + | |||
| + | Νεα_Εικόνα = Τιμή_Αντίθεσης * Αρχική_Εικόνα + Τιμή_Φωτεινότητας | ||
| + | |||
| + | Συνεπώς, | ||
| + | |||
| + | import os | ||
| + | import cv2 as cv | ||
| + | import numpy as np | ||
| + | import datetime as dt | ||
| + | | ||
| + | imgFileFormats = (" | ||
| + | | ||
| + | def checkImgFileType(src: | ||
| + | typeIMG = None | ||
| + | for f in imgFileFormats: | ||
| + | if f in src: | ||
| + | typeIMG = f | ||
| + | return typeIMG | ||
| + | | ||
| + | def brightnessContrastChange(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read image file at %s" % src) | ||
| + | img = cv.imread(src) | ||
| + | | ||
| + | # Check if image was opened correctly | ||
| + | if img is None: | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False, None | ||
| + | | ||
| + | newImg = np.zeros(img.shape, | ||
| + | | ||
| + | # Do the operation newImg(i,j) = contrastVal * img(i,j) + brightnessVal | ||
| + | # Instead of these ' | ||
| + | # newImg = cv.convertScaleAbs(image, | ||
| + | # I prefer this method, cause it shows how to access the points of the image directly. This method can be used | ||
| + | # if someone wants to write his own image manipulate functions | ||
| + | for y in range(img.shape[0]): | ||
| + | for x in range(img.shape[1]): | ||
| + | for c in range(img.shape[2]): | ||
| + | newImg[y, x, c] = np.clip(contrastVal * img[y, x, c] + brightnessVal, | ||
| + | | ||
| + | # newImg = cv.convertScaleAbs(img, | ||
| + | | ||
| + | if exportFolderPath is not None: # Check if an export path has need set | ||
| + | if exportImageName is None: # Check if user has not specify a name | ||
| + | exportImageName = os.path.splitext(os.path.basename(src))[0] | ||
| + | exportImageName = exportImageName + " | ||
| + | imgTypeFormat = checkImgFileType(src) # Take original image format | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | else: | ||
| + | imgTypeFormat = checkImgFileType(exportImageName) | ||
| + | exportImageName = os.path.splitext(os.path.basename(exportImageName))[0] | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | exportAt = exportFolderPath+exportImageName+imgTypeFormat | ||
| + | print(str(dt.datetime.now()) + " : Write image file at %s" % exportAt) | ||
| + | cv.imwrite(exportAt, | ||
| + | | ||
| + | return True, newImg | ||
| + | |||
| + | Επειδή είναι το πρώτο παράδειγμα, | ||
| + | |||
| + | Ας εξηγηθεί όμως, τι γίνεται παραπάνω: | ||
| + | |||
| + | - Αρχικά εισάγονται οι απαραίτητες βιβλιοθήκες. Η βιβλιοθήκη **os** εισάγεται για τη διαχείρηση των path των εικόνων, | ||
| + | - Η συνάρτηση checkImgFileType() δημιουργήθηκε για να μπορεί το πρόγραμμα να γίνει πιο αυτοματοποιημένο. Η συνάρτηση αυτή χρησιμοποιείται για να αναγνωρίσει συγκεκριμένους τύπους αρχείων εικόνων, | ||
| + | - Η βασική συνάρτηση είναι η brightnessContrastChange, | ||
| + | - Το άνοιγμα της εικόνας πραγματοποιείται με τη συνάρτηση **cv.imread(src, | ||
| + | - Με ίδια λογική για την εξαγωγή εικόνας χρησιμοποιείται η συνάρτηση **cv.imwrite(filename, | ||
| + | - Για την επεξεργασία της εικόνας στο παραπάνω παράδειγμα χρησιμοποιήθηκε ένα nested for loop. Στη θέση των for loop, θα μπορούσε να χρησιμοποιηθεί απευθείας η συνάρτηση **cv.convertScaleAbs(image, | ||
| + | |||
| + | |||
| + | ====Φιλτράρισμα Εικόνας==== | ||
| + | |||
| + | Για τη χρήση φίλτρων στην OpenCV χρησιμοποιείται η συνάρτηση cv.filter2D(src, | ||
| + | |||
| + | Παρακάτω παρατίθεται κώδικας για τη χρήση μερικών βασικών φίλτρων. | ||
| + | |||
| + | ===Φίλτρο Δέλτα=== | ||
| + | |||
| + | def deltaFiltering(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read image file at %s" % src) | ||
| + | img = cv.imread(src) | ||
| + | | ||
| + | # Check if image was opened correctly | ||
| + | if img is None: | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False, None | ||
| + | | ||
| + | # Set the filter | ||
| + | kernel = np.array(([0, | ||
| + | [0, 1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | | ||
| + | print(str(dt.datetime.now()) + " : Image filtering: | ||
| + | print(kernel) | ||
| + | newImg = cv.filter2D(src=img, | ||
| + | | ||
| + | if exportFolderPath is not None: # Check if an export path has need set | ||
| + | if exportImageName is None: # Check if user has not specify a name | ||
| + | exportImageName = os.path.splitext(os.path.basename(src))[0] | ||
| + | exportImageName = exportImageName + " | ||
| + | imgTypeFormat = checkImgFileType(src) | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | else: | ||
| + | imgTypeFormat = checkImgFileType(exportImageName) | ||
| + | exportImageName = os.path.splitext(os.path.basename(exportImageName))[0] | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | exportAt = exportFolderPath + exportImageName + imgTypeFormat | ||
| + | print(str(dt.datetime.now()) + " : Write image file at %s" % exportAt) | ||
| + | cv.imwrite(exportAt, | ||
| + | | ||
| + | return True, newImg | ||
| + | | ||
| + | ===Φίλτρο Παραγώγου=== | ||
| + | | ||
| + | def shiftAndSubtractFilter(src: | ||
| + | | ||
| + | | ||
| + | | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read image file at %s" % src) | ||
| + | img = cv.imread(src) | ||
| + | | ||
| + | # Check if image was opened correctly | ||
| + | if img is None: | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False, None | ||
| + | | ||
| + | # Set the filter | ||
| + | kernel = np.array(([0, | ||
| + | [0, 0, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | if topLeft: | ||
| + | kernel = kernel + np.array(([-1, | ||
| + | [0, 1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | if topMiddle: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, 1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | if topRight: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, 1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | if centerLeft: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [-1, 1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | if centerMiddle: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, 1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | if centerRight: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, 1, -1], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | if bottomLeft: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, 1, 0], | ||
| + | [-1, 0, 0]), np.float32) | ||
| + | if bottomMiddle: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, 1, 0], | ||
| + | [0, -1, 0]), np.float32) | ||
| + | if bottomRight: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, 1, 0], | ||
| + | [0, 0, -1]), np.float32) | ||
| + | | ||
| + | print(str(dt.datetime.now()) + " : Image filtering: | ||
| + | print(kernel) | ||
| + | newImg = cv.filter2D(src=img, | ||
| + | | ||
| + | if exportFolderPath is not None: # Check if an export path has need set | ||
| + | if exportImageName is None: # Check if user has not specify a name | ||
| + | exportImageName = os.path.splitext(os.path.basename(src))[0] | ||
| + | exportImageName = exportImageName + " | ||
| + | imgTypeFormat = checkImgFileType(src) | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | else: | ||
| + | imgTypeFormat = checkImgFileType(exportImageName) | ||
| + | exportImageName = os.path.splitext(os.path.basename(exportImageName))[0] | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | exportAt = exportFolderPath + exportImageName + imgTypeFormat | ||
| + | print(str(dt.datetime.now()) + " : Write image file at %s" % exportAt) | ||
| + | cv.imwrite(exportAt, | ||
| + | | ||
| + | return True, newImg | ||
| + | | ||
| + | ===Ανίχνευση Ακμών=== | ||
| + | | ||
| + | def edgeDetection(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read image file at %s" % src) | ||
| + | img = cv.imread(src) | ||
| + | | ||
| + | # Check if image was opened correctly | ||
| + | if img is None: | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False, None | ||
| + | | ||
| + | # Set the filter | ||
| + | kernel = np.array(([1 / 8, 1 / 8, 1 / 8], | ||
| + | [1 / 8, 1, 1 / 8], | ||
| + | [1 / 8, 1 / 8, 1 / 8]), np.float32) | ||
| + | | ||
| + | if centerMiddle: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, -1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | | ||
| + | print(str(dt.datetime.now()) + " : Image filtering: | ||
| + | print(kernel) | ||
| + | newImg = cv.filter2D(src=img, | ||
| + | | ||
| + | if exportFolderPath is not None: # Check if an export path has need set | ||
| + | if exportImageName is None: # Check if user has not specify a name | ||
| + | exportImageName = os.path.splitext(os.path.basename(src))[0] | ||
| + | exportImageName = exportImageName + " | ||
| + | imgTypeFormat = checkImgFileType(src) | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | else: | ||
| + | imgTypeFormat = checkImgFileType(exportImageName) | ||
| + | exportImageName = os.path.splitext(os.path.basename(exportImageName))[0] | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | exportAt = exportFolderPath + exportImageName + imgTypeFormat | ||
| + | print(str(dt.datetime.now()) + " : Write image file at %s" % exportAt) | ||
| + | cv.imwrite(exportAt, | ||
| + | | ||
| + | return True, newImg | ||
| + | | ||
| + | ===Ενίσχυση Ακμών=== | ||
| + | |||
| + | def edgeEnhancement(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read image file at %s" % src) | ||
| + | img = cv.imread(src) | ||
| + | | ||
| + | # Check if image was opened correctly | ||
| + | if img is None: | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False, None | ||
| + | | ||
| + | # Set the filter | ||
| + | if kappa > 8: | ||
| + | kappa = 8 | ||
| + | elif kappa < -8: | ||
| + | kappa = -8 | ||
| + | | ||
| + | kernel = np.array(([-kappa / 8, -kappa / 8, -kappa / 8], | ||
| + | | ||
| + | | ||
| + | | ||
| + | if centerMiddle: | ||
| + | kernel = kernel + np.array(([0, | ||
| + | [0, -1, 0], | ||
| + | [0, 0, 0]), np.float32) | ||
| + | | ||
| + | print(str(dt.datetime.now()) + " : Image filtering: | ||
| + | print(kernel) | ||
| + | newImg = cv.filter2D(src=img, | ||
| + | | ||
| + | if exportFolderPath is not None: # Check if an export path has need set | ||
| + | if exportImageName is None: # Check if user has not specify a name | ||
| + | exportImageName = os.path.splitext(os.path.basename(src))[0] | ||
| + | exportImageName = exportImageName + " | ||
| + | imgTypeFormat = checkImgFileType(src) | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | else: | ||
| + | imgTypeFormat = checkImgFileType(exportImageName) | ||
| + | exportImageName = os.path.splitext(os.path.basename(exportImageName))[0] | ||
| + | if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG | ||
| + | imgTypeFormat = " | ||
| + | exportAt = exportFolderPath + exportImageName + imgTypeFormat | ||
| + | print(str(dt.datetime.now()) + " : Write image file at %s" % exportAt) | ||
| + | cv.imwrite(exportAt, | ||
| + | | ||
| + | return True, newImg | ||
| + | |||
| + | |||
| + | ====Χρήση της OpenCV για προβολή εικόνας σε GUI==== | ||
| + | |||
| + | Η OpenCV παρέχει τις δικές της εντολές για τη δημιουργία γραφικού περιβάλλοντος. Σε αυτή την ενότητα θα γίνει σύντομη περιγραφή του περιβάλλοντος αυτού. | ||
| + | |||
| + | ===Άνοιγμα Παραθύρου για Προβολή Εικόνας=== | ||
| + | |||
| + | Για τη δημιουργία παραθύρου χρησιμοποιείται η συνάρτηση **cv.namesWindow(winname, | ||
| + | |||
| + | Στη συνέχεια για την προβολή της εικόνας στο παράθυρο χρησιμοποιούνται η συνάρτηση **cv.imshow(winname, | ||
| + | |||
| + | Αφού τερματιστεί με κάποιο τρόπο η προβολή της εικόνας, | ||
| + | |||
| + | def openImageInWindow(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read image file at %s" % src) | ||
| + | img = cv.imread(src) | ||
| + | | ||
| + | # Check if image was opened correctly | ||
| + | if img is None: | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False | ||
| + | | ||
| + | windowName = os.path.basename(src) | ||
| + | cv.namedWindow(winname=windowName, | ||
| + | cv.imshow(winname=windowName, | ||
| + | cv.waitKey(0) | ||
| + | cv.destroyAllWindows() | ||
| + | | ||
| + | return True | ||
| + | |||
| + | ===Χρήση Πυραμίδων για Προβολή Μεγάλων Εικόνων=== | ||
| + | |||
| + | Όπως αναφέρθηκε παραπάνω όταν μία εικόνα είναι πολύ μεγάλη, | ||
| + | |||
| + | **Προσοχή: | ||
| + | |||
| + | Η για τη δημιουργία πυραμίδα εικόνας χρησιμοποιείται η συνάρτηση cv.pyrDown(src, | ||
| + | | ||
| + | def openImageInWindowPyramidVersion(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read image file at %s" % src) | ||
| + | img = cv.imread(src) | ||
| + | | ||
| + | # Check if image was opened correctly | ||
| + | if img is None: | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False | ||
| + | | ||
| + | h, w, _channels = map(int, img.shape) | ||
| + | while w > 1024 or h > 720: # We want the image to fit the screen (this values ensures that) | ||
| + | img = cv.pyrDown(src=img, | ||
| + | # (there is also cv.pyrUp(src=img, | ||
| + | h, w, _channels = map(int, img.shape) | ||
| + | print(" | ||
| + | | ||
| + | windowName = os.path.basename(src) | ||
| + | cv.namedWindow(winname=windowName, | ||
| + | cv.imshow(winname=windowName, | ||
| + | cv.waitKey(0) | ||
| + | cv.destroyAllWindows() | ||
| + | | ||
| + | return True | ||
| + | |||
| + | ===Προβολή Βίντεο=== | ||
| + | |||
| + | Με αντίστοιχο γίνεται και η προβολή ενός βίντεο. Το βίντεο αποτελείται από μία σειρά εικόνων που η μία διαδέχεται την άλλη με κάποια συγκεκριμένη ταχύτητα που μετριέται σε καρέ/ | ||
| + | |||
| + | Για το άνοιγμα ενός βίντεο η OpenCV έχει δημιουργήσει τη συνάρτηση **cv.VideoCapture(src)**, | ||
| + | Αυτό στην πράξη μεταφράζεται ως **success, frame = videoFile.read()**. Η συνάρτηση **read()** επιστρέφει σαν πρώτη παράμετρο την επιτυχία ή αποτυχία ανάγνωσης του καρέ (για αυτό και θα πρέπει να γίνεται πάντα έλεγχος εάν διαβάστηκε το καρέ ή όχι) και σαν δέυτερη παράμετρο επιστροφής είναι το καρέ. Εάν δεν γίνει έλεγχος, | ||
| + | |||
| + | ==Απλή Προσπέλαση-Προβολή Βίντεο== | ||
| + | |||
| + | def openVideoInWindow(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read video file at %s" % src) | ||
| + | videoFile = cv.VideoCapture(src) | ||
| + | | ||
| + | # Check if video was opened correctly | ||
| + | if not videoFile.isOpened(): | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False | ||
| + | | ||
| + | windowName = os.path.basename(src) | ||
| + | cv.namedWindow(winname=windowName, | ||
| + | | ||
| + | videoPlay = True | ||
| + | while videoPlay: | ||
| + | success, frame = np.array(videoFile.read()) | ||
| + | if not success: | ||
| + | break | ||
| + | cv.imshow(winname=windowName, | ||
| + | press_ESC = cv.waitKey(33) | ||
| + | if press_ESC is 27: | ||
| + | break | ||
| + | cv.destroyAllWindows() | ||
| + | |||
| + | |||
| + | ==Προβολή Βίντεο Με Χρήση Πυραμίδων== | ||
| + | |||
| + | Εάν η ανάλυση του βίντεο είναι πολύ μεγάλη, | ||
| + | λόγο συνιστάται η χρήση πυραμίδων για μείωση της ανάλυσης στα επιθυμητά όρια. | ||
| + | |||
| + | def openVideoInWindowPyramidVersion(src: | ||
| + | print(" | ||
| + | print(str(dt.datetime.now()) + " : Read video file at %s" % src) | ||
| + | videoFile = cv.VideoCapture(src) | ||
| + | | ||
| + | # Check if video was opened correctly | ||
| + | if not videoFile.isOpened(): | ||
| + | print(str(dt.datetime.now()) + " | ||
| + | return False | ||
| + | | ||
| + | windowName = os.path.basename(src) | ||
| + | cv.namedWindow(winname=windowName, | ||
| + | | ||
| + | videoPlay = True | ||
| + | while videoPlay: | ||
| + | success, frame = videoFile.read() | ||
| + | if not success: | ||
| + | break | ||
| + | | ||
| + | h, w, _channels = map(int, frame.shape) | ||
| + | while w > 1024 or h > 720: # We want the image to fit the screen (this values ensures that) | ||
| + | frame = cv.pyrDown(src=frame, | ||
| + | # (there is also cv.pyrUp(src=img, | ||
| + | h, w, _channels = map(int, frame.shape) | ||
| + | | ||
| + | cv.imshow(winname=windowName, | ||
| + | press_ESC = cv.waitKey(33) | ||
| + | if press_ESC is 27: | ||
| + | break | ||
| + | cv.destroyAllWindows() | ||