about summary refs log tree commit diff
path: root/usth/ICT2.12/labwork/1
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2020-07-08 14:11:07 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2020-07-08 14:11:07 +0700
commite09aa90e0e388d4c42e38381961bc3fa1771cff3 (patch)
treebf6bc96a6a21ab41c57e76e5167986879557923e /usth/ICT2.12/labwork/1
parent815cb0173890ada973925a9238d8765c47a1bbfd (diff)
downloadcp-e09aa90e0e388d4c42e38381961bc3fa1771cff3.tar.gz
[usth/ICT2.{5,12}] Process images and base data
Diffstat (limited to 'usth/ICT2.12/labwork/1')
-rwxr-xr-xusth/ICT2.12/labwork/155
1 files changed, 55 insertions, 0 deletions
diff --git a/usth/ICT2.12/labwork/1 b/usth/ICT2.12/labwork/1
new file mode 100755
index 0000000..2590192
--- /dev/null
+++ b/usth/ICT2.12/labwork/1
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+from cv2 import (
+    THRESH_BINARY, ADAPTIVE_THRESH_MEAN_C,
+    adaptiveThreshold as adaptive_threshold, calcHist as calc_hist, blur,
+    equalizeHist as equalize_hist, GaussianBlur as gaussian_blur,
+    imread, imshow, Laplacian as laplacian, medianBlur as median_blur,
+    resize, Sobel as sobel, threshold, waitKey as wait_key)
+from numpy import uint8
+
+FILENAME = 'dino-gang.jpg'
+GREYSCALE_COEFF = 0.2126, 0.7152, 0.0722
+THRESHOLD, WHITE = 128, 255
+BLUR_KSIZE = 6, 9
+
+
+def disp(image, name):
+    """Display the given image."""
+    imshow(name, image.astype(uint8))
+    wait_key()
+
+
+# Exercise 1
+image = imread(FILENAME)
+disp(image, 'original')
+
+# Exercise 2
+disp(resize(image, (512, 512)), 'square')
+
+# Exercise 3
+# I'm going to ignore about cv2.IMREAD_GRAYSCALE
+grey_image = uint8(image.dot(GREYSCALE_COEFF))
+disp(grey_image, 'grey')
+
+# Exercise 4
+a, b = 1/2, 1/3
+disp(image*a+b, 'brightness adjusted')
+
+# Exercise 5
+disp(threshold(grey_image, THRESHOLD, WHITE, THRESH_BINARY)[-1],
+     'binary threshold')
+disp(adaptive_threshold(grey_image, WHITE, ADAPTIVE_THRESH_MEAN_C,
+                        THRESH_BINARY, 25, 12), 'adaptive threshold')
+
+# Exercise 6
+hist = calc_hist([grey_image], [0], None, [256], [0, 256])
+disp(equalize_hist(grey_image), 'equalized')
+
+# Exercise 7
+disp(blur(image, BLUR_KSIZE), 'blur')
+disp(gaussian_blur(image, (0, 0), 5), 'Gaussian blur')
+disp(median_blur(image, 7), 'median blur')
+
+# Exercise 8
+disp(laplacian(image, 2), 'Laplacian')
+disp(sobel(image, 2, 1, 1), 'Sobel')