Βλέπετε μια παλαιότερη έκδοση της σελίδας!
Μια μικρή εισαγωγή στην OpenCV ξεκινάει πάντα από ένα νέο λήμμα σε γουίκι… τον σύγχρονο τρόπο να μοιράζεσαι γνώση.
Όσοι οι πιστοί προσέλθετε
Η OpenCV(Open-source Computer Vision) είναι μία βιβλιοθήκη που περιέχει συναρτήσεις, οι οποίες στοχεύουν στη δημιουργία εφαρμογών όρασης υπολογιστών σε πραγματικό χρόνο. Αρχικά αναπτύχθηκε από την Intel, αλλά στη συνέχεια υποστηρήχθηκε από την Willow Garage. Η βιβλιοθήκη μπορεί να χρησιμοποιηθεί σε όλα τα λειτουργικά συστήματα (Windows, Linux, Mac) και μπορεί να χρησιμοποιηθεί δωρεάν, έχοντας άδεια τύπου BSD.
Επίσης η OpenCV υποστηρίζει συστήματα Deep Learning, όπως είναι το TensorFlow, Torch/Pytorch και Caffe.
Όπως και με κάθε άλλη γλώσσα προγραμματισμού, έτσι και με τις βιβλιοθήκες, η διαδικασία εκμάθησης πρέπει να ακολουθεί κάποια στάδια. Το κλασικό Hello World που μαθαίνει κανείς σε μία γλώσσα προγραμματισμού, στην OpenCV θεωρείται η διαδικασία Ανοίγματος Εκόνας - Μικρή Επεξεργασία - Εξαγωγή Εικόνας. Όλα τα παραδείγματα της ενότητας αυτής, θα ακολουθήσουν αυτή τη δομή. Η επεξεργασία μπορεί να είναι από την αλλαγή φωτεινότητας της εικόνας, μέχρι την επεξεργασία της εικόνας χρησιμοποιώντας κάποιο φίλτρο.
Ας ξεκινήσουμε λοιπόν.
import os
import cv2 as cv
import numpy as np
import datetime as dt
imgFileFormats = (".jpg", ".jpeg", ".png", ".tif", ".tiff")
def checkImgFileType(src: str):
typeIMG = None
for f in imgFileFormats:
if f in src:
typeIMG = f
return typeIMG
def brightnessContrastChange(src: str, contrastVal=1.0, brightnessVal=0.0, exportFolderPath=None, exportImageName=None):
print("\n\n")
print(str(dt.datetime.now()) + " : Read image file at %s" % src)
img = cv.imread(src) # Read the image file at path src
# Check if image was opened correctly
if img is None:
print(str(dt.datetime.now()) + " : Error: Cannot read image at path %s" % src)
return False, None
newImg = np.zeros(img.shape, img.dtype) # Create a new image to store the new values
# Do the operation newImg(i,j) = contrastVal * img(i,j) + brightnessVal
# Instead of these 'for' loops we could have used simply:
# newImg = cv.convertScaleAbs(image, alpha=contrastVal, beta=brightnessVal)
# 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, 0, 255)
# newImg = cv.convertScaleAbs(image, alpha=contrastVal, beta=brightnessVal)
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] # Take the name of original image
exportImageName = exportImageName + "_brightnessContrastChange" # Set new name
imgTypeFormat = os.path.splitext(os.path.basename(src))[1] # Take original image format
else:
imgTypeFormat = checkImgFileType(exportImageName) # Find the input format first
exportImageName = os.path.splitext(os.path.basename(exportImageName))[0] # Take new name
if imgTypeFormat is None: # If user hasn't specify a type then export image as JPG
imgTypeFormat = ".jpg"
exportAt = exportFolderPath+exportImageName+imgTypeFormat
print(str(dt.datetime.now()) + " : Write image file at %s" % exportAt)
cv.imwrite(exportAt, newImg) # Export image
return True, newImg # Return True for success and the newImg