commit 568ebfec974d38783b676b4522bbccac376a8ee2 Author: Arne van Iterson Date: Wed Sep 6 13:35:15 2023 +0200 Testing exercises working diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..306f58e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": true + } + ] +} \ No newline at end of file diff --git a/example/ex1 - showImg.py b/example/ex1 - showImg.py new file mode 100644 index 0000000..50786ff --- /dev/null +++ b/example/ex1 - showImg.py @@ -0,0 +1,9 @@ +import numpy as np +import cv2 +import os + +# Load an color image in grayscale +img = cv2.imread(os.path.join(os.getcwd(), './res/balls.png'),0) +cv2.imshow('image',img) +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/example/ex2 - showChannels.py b/example/ex2 - showChannels.py new file mode 100644 index 0000000..beebce8 --- /dev/null +++ b/example/ex2 - showChannels.py @@ -0,0 +1,45 @@ +import numpy as np +import cv2 +import copy + +image = cv2.imread('./res/balls.png') + +if image is None: + print("\nERROR: Could not open image\n") + exit() + +# Bepaal afmetingen en aantal kleurkanalen +height = image.shape[0] +width = image.shape[1] +colors = image.shape[2] +print ("%d pixels breed" % width) +print ("%d pixels hoog" % height) +print ("%d kleur kanalen" % colors) +image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) +image_HSV = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) +image_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + +#RGB - Blue +cv2.imshow('B-RGB',image_RGB[:, :, 0]) + +# RGB - Green +cv2.imshow('G-RGB',image_RGB[:, :, 1]) + +# RGB Red +cv2.imshow('R-RGB',image_RGB[:, :, 2]) + +# HSV - H +cv2.imshow('H-HSV',image_HSV[:, :, 0]) + +# HSV - S +cv2.imshow('S-HSV',image_HSV[:, :, 1]) + +# HSV - V +cv2.imshow('V-HSV',image_HSV[:, :, 2]) + +cv2.imshow("HSV", image_HSV) +cv2.imshow("RGB", image_RGB) +cv2.imshow("Input image", image) + +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/example/ex3 - showCamera.py b/example/ex3 - showCamera.py new file mode 100644 index 0000000..dcd1903 --- /dev/null +++ b/example/ex3 - showCamera.py @@ -0,0 +1,21 @@ +import numpy as np +import cv2 + +cap = cv2.VideoCapture(0) + +while(True): + # Capture frame-by-frame + ret, frame = cap.read() + + # Our operations on the frame come here + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + + # Display the resulting frame + cv2.imshow('frame',gray) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +# When everything done, release the capture +cap.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/example/ex4 - deepCopy.py b/example/ex4 - deepCopy.py new file mode 100644 index 0000000..e9ccfac --- /dev/null +++ b/example/ex4 - deepCopy.py @@ -0,0 +1,32 @@ +import numpy as np +import cv2 +import copy + +image = cv2.imread('./res/balls.png') +if image is None: + print("\nERROR: Could not open image\n") + exit() + +# Bepaal afmetingen en aantal kleurkanalen +height = image.shape[0] +width = image.shape[1] +colors = image.shape[2] +print ("%d pixels breed" % width) +print ("%d pixels hoog" % height) +print ("%d kleur kanalen" % colors) + +#snij een selectie uit het plaatje +# dit maakt een copy van het adres van het plaatje: +selection1 = image[10:150, 10:200] ## Dit is een reference, python doet dit om geheugen te besparen +# dit maakt een echte copy van een deel van het plaatje: +selection2 = copy.deepcopy(image[10:150, 10:200]) ## Dit is een daadwerkelijke kopie op een nieuwe locatie +# de kleuren kun je zetten door een zogenaamd tupplet van kleur (50,250,50) +image[10:150, 10:200] = (50,250,50) ## Hier zet je de waarde van de afbeelding op groen dus ook die van de reference + +# Toon het beeld in een venster +cv2.imshow("Cut-out1", selection1) +cv2.imshow("Cut-out2", selection2) +cv2.imshow("Input image", image) + +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/example/ex5 - blue.py b/example/ex5 - blue.py new file mode 100644 index 0000000..95d4f0e --- /dev/null +++ b/example/ex5 - blue.py @@ -0,0 +1,44 @@ +import cv2 +import numpy as np + +cap = cv2.VideoCapture(0) + +while(1): + # Take each frame + ret, frame = cap.read() + + # Convert BGR to HSV + hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + + # define range of blue color in HSV + lower_blue = np.array([110,50,50]) + upper_blue = np.array([130,255,255]) + + # Threshold the HSV image to get only blue colors + mask = cv2.inRange(hsv, lower_blue, upper_blue) + + blue_amnt = mask.mean() + blue_str = "" + if blue_amnt < 10: + blue_str = "not very blue" + elif blue_amnt >= 10 and blue_amnt < 30: + blue_str = "a bit blue" + elif blue_amnt >= 30 and blue_amnt < 100: + blue_str = "very blue" + elif blue_amnt >= 100: + blue_str = "all blue" + + # Bitwise-AND mask and original image + res = cv2.bitwise_and(frame,frame, mask= mask) + + cv2.putText(res, blue_str, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + + cv2.imshow('frame',frame) + cv2.imshow('mask',mask) + cv2.imshow('res',res) + + k = cv2.waitKey(5) & 0xFF + if k == 27: + break + +cv2.destroyAllWindows() \ No newline at end of file diff --git a/example/ex6 - histogram.py b/example/ex6 - histogram.py new file mode 100644 index 0000000..5e82679 --- /dev/null +++ b/example/ex6 - histogram.py @@ -0,0 +1,16 @@ +import numpy as np +import cv2 +import os +from matplotlib import pyplot as plt + +# Load an color image in grayscale +img = cv2.imread(os.path.join(os.getcwd(), "./res/bwg.png"), 0) + +hist = cv2.calcHist([img], [0], None, [256], [0,256]) + +# show the plotting graph of an image +plt.plot(hist) +plt.show() + +cv2.waitKey(0) +cv2.destroyAllWindows() diff --git a/example/ex7 - histogram eq.py b/example/ex7 - histogram eq.py new file mode 100644 index 0000000..11392ee --- /dev/null +++ b/example/ex7 - histogram eq.py @@ -0,0 +1,23 @@ +import numpy as np +import cv2 +import os +from matplotlib import pyplot as plt + +# Load an color image in grayscale +img = cv2.imread(os.path.join(os.getcwd(), "./res/bwg.png"), 0) +imgEq = cv2.equalizeHist(img) + +cv2.imshow('in',img) +cv2.imshow('out',imgEq) + +hist = cv2.calcHist([img], [0], None, [256], [0,256]) +histEq = cv2.calcHist([imgEq], [0], None, [256], [0,256]) + + +# show the plotting graph of an image +plt.plot(hist) +plt.plot(histEq) +plt.show() + +cv2.waitKey(0) +cv2.destroyAllWindows() diff --git a/res/balls.png b/res/balls.png new file mode 100644 index 0000000..ca11605 Binary files /dev/null and b/res/balls.png differ diff --git a/res/bw.png b/res/bw.png new file mode 100644 index 0000000..b180488 Binary files /dev/null and b/res/bw.png differ diff --git a/res/bwg.png b/res/bwg.png new file mode 100644 index 0000000..b22f5f2 Binary files /dev/null and b/res/bwg.png differ diff --git a/res/drake.png b/res/drake.png new file mode 100644 index 0000000..8782d87 Binary files /dev/null and b/res/drake.png differ diff --git a/res/grad.png b/res/grad.png new file mode 100644 index 0000000..581a32c Binary files /dev/null and b/res/grad.png differ diff --git a/res/ohno.png b/res/ohno.png new file mode 100644 index 0000000..67750df Binary files /dev/null and b/res/ohno.png differ diff --git a/src/nuts.py b/src/nuts.py new file mode 100644 index 0000000..e69de29