From b781b4150cf86b78226b616bb68cbad99f133ad2 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Tue, 22 Nov 2022 08:37:34 +0100 Subject: Initial commit --- .gitignore | 226 +++++++++++++++++++++++++++++++++++ 02_HelperFunctions.py | 83 +++++++++++++ images/hummingbird_from_pixabay.png | Bin 0 -> 449215 bytes images/hummingbird_from_pixabay2.png | Bin 0 -> 354063 bytes images/hummingbird_from_pixabay3.png | Bin 0 -> 153971 bytes openCVTools.py | 45 +++++++ pyvenv.cfg | 3 + 7 files changed, 357 insertions(+) create mode 100644 .gitignore create mode 100644 02_HelperFunctions.py create mode 100644 images/hummingbird_from_pixabay.png create mode 100644 images/hummingbird_from_pixabay2.png create mode 100644 images/hummingbird_from_pixabay3.png create mode 100644 openCVTools.py create mode 100644 pyvenv.cfg diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..402f5fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,226 @@ +# Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,linux,windows +# Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode,linux,windows + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,linux,windows diff --git a/02_HelperFunctions.py b/02_HelperFunctions.py new file mode 100644 index 0000000..1ef0944 --- /dev/null +++ b/02_HelperFunctions.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + + +import cv2 as cv +import openCVTools as cvt +import numpy as np +import matplotlib.pyplot as plt + +def REPLACE_THIS(input): return input + + +# load image +imgOriginal = cvt.safeLoad('images/hummingbird_from_pixabay.png') +cvt.showImage("Original image", imgOriginal) + + +# TODO Aufgabe 1 +''' +Konvertieren Sie das Bild in ein 1-Kanal Schwarzweiß(Grauton)-Bild. +Passen Sie die Funktionen `imageStats(..)` und `showImage(..)` so an, dass sie sowohl für Grau- als auch Farbbilder korrekt funktionieren. +''' +imgGray = REPLACE_THIS(imgOriginal) +cvt.showImage("Grayscale image", imgGray) + + +''' +Konvertieren Sie das originale Bild von seinem Stndarddatentyp (`uint8` pro Farbkanal) in `float` und zeigen Sie es an. +Voraussichtlich wird das Bild nahezu weiß sein. Versuchen Sie herauszufinden woran das liegt und passen Sie das Bild entsprechend an. +''' +img = REPLACE_THIS(imgOriginal) +print("Floating point image:") +cvt.showImage("Floating point image", img) + + +# TODO Aufgabe 2 +''' +Zeichnen Sie ein rotes Rechteck (110 x 220 Pixel) genau in die Mitte des Bildes. +Versuchen Sie dazu den Slicing Operator aus Python zu nutzen. +''' +# +# ??? +# + +cvt.showImage("Red box", img) + + +# TODO Aufgabe 3 +''' +Hängen Sie eine Callback-Funktion `mouse_move(event)` an das Bild im Fenster "Red box", die bei jeder Mausbewegung aufgerufen wird. Sie soll die Bild(!)-Koordinaten des Mauszeigers (x, y) und evtl. gedrückte Tasten ausgeben. +Sie benötigen dazu u.U. die Referenz auf das Zeichenfeld des Fensters (`plt.figure(title)`) aus der Funktion `showImage(..)`. +''' +# +# ??? +# + +''' +Optional: Entfernen Sie die Callback-Funktion, sobald auf das Bild geklickt wird. +''' +# +# ??? +# + + +# TODO Aufgabe 4 +''' +Schreiben Sie eine Funktion `showImageList(...)`, die Bilder auch als Liste annimmt und diese horizontal nebeneinander anzeigt. +''' +img2 = cvt.safeLoad('images/hummingbird_from_pixabay2.png') +img3 = cvt.safeLoad('images/hummingbird_from_pixabay3.png') + +# cvt.showImageList("Image list", (imgOriginal, img, img2, img3)) + + +# TODO Aufgabe 5 +''' +Erweitern Sie die Funktionen `showImage(...)` und `showImageList(...)` um einen weiteren Parameter `normalize` vom Typ Boolean. Wenn dieser auf `False` gesetzt ist, dann soll die Funktion wie bisher funktionieren, wenn dieser auf `True` gesetzt wird, dann soll jedes Eingabebild kopiert und der Kontrast der kopierten Bilder vor der Anzeige mittels Contrast Stretching verbessert werden, damit der gesamte Wertebereich ausgenutzt wird. +''' +img3[:,:,:] = img3[:,:,:] / 4 # darken the image to show contrast stretching + +# +# ??? +# diff --git a/images/hummingbird_from_pixabay.png b/images/hummingbird_from_pixabay.png new file mode 100644 index 0000000..60e845a Binary files /dev/null and b/images/hummingbird_from_pixabay.png differ diff --git a/images/hummingbird_from_pixabay2.png b/images/hummingbird_from_pixabay2.png new file mode 100644 index 0000000..39c756f Binary files /dev/null and b/images/hummingbird_from_pixabay2.png differ diff --git a/images/hummingbird_from_pixabay3.png b/images/hummingbird_from_pixabay3.png new file mode 100644 index 0000000..63ade5a Binary files /dev/null and b/images/hummingbird_from_pixabay3.png differ diff --git a/openCVTools.py b/openCVTools.py new file mode 100644 index 0000000..484c3fa --- /dev/null +++ b/openCVTools.py @@ -0,0 +1,45 @@ +import cv2 as cv +import sys +import numpy as np +import matplotlib.pyplot as plt + + +def safeLoad(pathToFile): + ''' + OpenCV does no validation checks due to performance reasons. + Therefore, this function checks if the image could be loaded + ''' + img = cv.imread(pathToFile) + if img is None: + sys.exit("Image could not be loaded.") + return img + + +# TODO Aufgabe 1 +''' +Passen Sie die Funktion `imageStats(..)` so an, dass sie sowohl Grau- als auch Farbbilder korrekt anzeigt. +Erweitern Sie die Funktion zusätzlich so dass der Datentyp mit ausgegeben wird. +''' +def imageStats(img): + ''' + Returns a few image statistics + ''' + s = img.shape + return f'Width: {s[1]}, Height: {s[0]}, Channels: {s[2]}' + + + +# TODO Aufgabe 1 +''' +Passen Sie die Funktion `showImage(..)` so an, dass sie sowohl Grau- als auch Farbbilder korrekt anzeigt. +''' +def showImage(title, originalImg): + print(imageStats(originalImg)) + + img = originalImg.copy() + img = img[:,:,::-1] + plt.figure(title) + plt.imshow(img) + plt.show() + + diff --git a/pyvenv.cfg b/pyvenv.cfg new file mode 100644 index 0000000..8810f2d --- /dev/null +++ b/pyvenv.cfg @@ -0,0 +1,3 @@ +home = /usr/bin +include-system-site-packages = false +version = 3.10.8 -- cgit v1.2.1