Εδώ βλέπετε τις διαφορές μεταξύ της επιλεγμένης έκδοσης και της τρέχουσας έκδοσης της σελίδας.
opencv [2020/02/23 23:56] John_Crabs |
opencv [2020/11/21 09:52] |
||
---|---|---|---|
Γραμμή 1: | Γραμμή 1: | ||
- | Μια μικρή εισαγωγή στην OpenCV ξεκινάει πάντα από ένα νέο λήμμα σε γουίκι... τον σύγχρονο τρόπο να μοιράζεσαι γνώση. | ||
- | |||
- | Όσοι οι πιστοί προσέλθετε :-) | ||
- | |||
- | ====== Εισαγωγή στην 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 | ||