├── Dataset ├── test │ ├── akahara_0009.jpg │ ├── akahara_0010.jpg │ ├── akahara_0011.jpg │ ├── madara_0009.jpg │ ├── madara_0010.jpg │ └── madara_0011.jpg └── train │ ├── akahara_0004.jpg │ ├── akahara_0005.jpg │ ├── akahara_0007.jpg │ ├── madara_0001.jpg │ ├── madara_0003.jpg │ └── madara_0006.jpg ├── Question_01_10 ├── Qusetion_01_10.ipynb ├── README.md ├── answers │ ├── answer_1.jpg │ ├── answer_1.py │ ├── answer_10.jpg │ ├── answer_10.py │ ├── answer_2.jpg │ ├── answer_2.py │ ├── answer_3.jpg │ ├── answer_3.py │ ├── answer_4.jpg │ ├── answer_4.py │ ├── answer_5.jpg │ ├── answer_5.py │ ├── answer_6.jpg │ ├── answer_6.py │ ├── answer_7.jpg │ ├── answer_7.py │ ├── answer_8.jpg │ ├── answer_8.py │ ├── answer_9.jpg │ └── answer_9.py ├── answers_cpp │ ├── answer_1.cpp │ ├── answer_10.cpp │ ├── answer_2.cpp │ ├── answer_3.cpp │ ├── answer_4.cpp │ ├── answer_5.cpp │ ├── answer_6.cpp │ ├── answer_7.cpp │ ├── answer_8.cpp │ └── answer_9.cpp ├── imori.jpg └── imori_noise.jpg ├── Question_11_20 ├── Qusetion_11_20.ipynb ├── README.md ├── answers │ ├── answer_11.jpg │ ├── answer_11.py │ ├── answer_12.jpg │ ├── answer_12.py │ ├── answer_13.jpg │ ├── answer_13.py │ ├── answer_14.py │ ├── answer_14_h.jpg │ ├── answer_14_v.jpg │ ├── answer_15.py │ ├── answer_15_h.jpg │ ├── answer_15_v.jpg │ ├── answer_16.py │ ├── answer_16_h.jpg │ ├── answer_16_v.jpg │ ├── answer_17.jpg │ ├── answer_17.py │ ├── answer_18.jpg │ ├── answer_18.py │ ├── answer_19.jpg │ ├── answer_19.py │ ├── answer_20.png │ └── answer_20.py ├── imori.jpg ├── imori_dark.jpg └── imori_noise.jpg ├── Question_21_30 ├── Qusetion_21_30.ipynb ├── README.md ├── answers │ ├── answer_21.py │ ├── answer_21_1.jpg │ ├── answer_21_2.png │ ├── answer_22.py │ ├── answer_22_1.jpg │ ├── answer_22_2.png │ ├── answer_23.py │ ├── answer_23_1.jpg │ ├── answer_23_2.png │ ├── answer_24.jpg │ ├── answer_24.py │ ├── answer_25.jpg │ ├── answer_25.py │ ├── answer_26.jpg │ ├── answer_26.py │ ├── answer_27.jpg │ ├── answer_27.py │ ├── answer_28.jpg │ ├── answer_28.py │ ├── answer_29.py │ ├── answer_29_1.jpg │ ├── answer_29_2.jpg │ ├── answer_30_1.jpg │ ├── answer_30_1.py │ ├── answer_30_2.jpg │ └── answer_30_2.py ├── imori.jpg ├── imori_dark.jpg ├── imori_gamma.jpg ├── out.jpg ├── question_24_1.jpg └── question_24_2.jpg ├── Question_31_40 ├── README.md ├── answers │ ├── answer_31.py │ ├── answer_31_1.jpg │ ├── answer_31_2.jpg │ ├── answer_31_3.jpg │ ├── answer_32.jpg │ ├── answer_32.py │ ├── answer_32_ps.jpg │ ├── answer_33.jpg │ ├── answer_33.py │ ├── answer_34.jpg │ ├── answer_34.py │ ├── answer_35.jpg │ ├── answer_35.py │ ├── answer_36.jpg │ ├── answer_36.py │ ├── answer_37.jpg │ ├── answer_37.py │ ├── answer_38.jpg │ ├── answer_38.py │ ├── answer_39.jpg │ ├── answer_39.py │ ├── answer_40.jpg │ └── answer_40.py ├── imori.jpg └── imori_gray.jpg ├── Question_41_50 ├── README.md ├── answers │ ├── answer_4.jpg │ ├── answer_41.py │ ├── answer_41_1.jpg │ ├── answer_41_2.jpg │ ├── answer_42.jpg │ ├── answer_42.py │ ├── answer_43.jpg │ ├── answer_43.py │ ├── answer_44.jpg │ ├── answer_44.py │ ├── answer_45.jpg │ ├── answer_45.py │ ├── answer_46.jpg │ ├── answer_46.py │ ├── answer_47.jpg │ ├── answer_47.py │ ├── answer_48.jpg │ ├── answer_48.py │ ├── answer_49.jpg │ ├── answer_49.py │ ├── answer_50.jpg │ └── answer_50.py ├── imori.jpg └── thorino.jpg ├── Question_51_60 ├── README.md ├── answers │ ├── answer_4.jpg │ ├── answer_51.jpg │ ├── answer_51.py │ ├── answer_52.jpg │ ├── answer_52.py │ ├── answer_53.jpg │ ├── answer_53.py │ ├── answer_54.jpg │ ├── answer_54.py │ ├── answer_55.jpg │ ├── answer_55.py │ ├── answer_56.jpg │ ├── answer_56.py │ ├── answer_57.jpg │ ├── answer_57.py │ ├── answer_58.png │ ├── answer_58.py │ ├── answer_59.png │ ├── answer_59.py │ ├── answer_60.jpg │ └── answer_60.py ├── imori.jpg ├── imori_part.jpg ├── seg.png └── thorino.jpg ├── Question_61_70 ├── README.md ├── answers │ ├── answer_61.png │ ├── answer_61.py │ ├── answer_62.png │ ├── answer_62.py │ ├── answer_63.png │ ├── answer_63.py │ ├── answer_64.py │ ├── answer_64_a.py │ ├── answer_65.png │ ├── answer_65.py │ ├── answer_66.py │ ├── answer_66_gra.jpg │ ├── answer_66_mag.jpg │ ├── answer_67.png │ ├── answer_67.py │ ├── answer_68.png │ ├── answer_68.py │ ├── answer_69.jpg │ ├── answer_69.py │ ├── answer_70.png │ └── answer_70.py ├── gazo.png ├── imori.jpg ├── jieshi.png └── renketsu.png ├── Question_71_80 ├── README.md ├── answers │ ├── answer_70.png │ ├── answer_71.jpg │ ├── answer_71.py │ ├── answer_72.jpg │ ├── answer_72.py │ ├── answer_72_mask.png │ ├── answer_73.jpg │ ├── answer_73.py │ ├── answer_74.jpg │ ├── answer_74.py │ ├── answer_75.py │ ├── answer_75_1.jpg │ ├── answer_75_16.jpg │ ├── answer_75_2.jpg │ ├── answer_75_32.jpg │ ├── answer_75_4.jpg │ ├── answer_75_8.jpg │ ├── answer_76.jpg │ ├── answer_76.py │ ├── answer_77.jpg │ ├── answer_77.py │ ├── answer_78.png │ ├── answer_78.py │ ├── answer_79.png │ ├── answer_79.py │ ├── answer_80.jpg │ └── answer_80.py ├── gazo.png └── imori.jpg ├── Question_81_90 ├── README.md ├── answers │ ├── answer_81.jpg │ ├── answer_81.py │ ├── answer_82.png │ ├── answer_82.py │ ├── answer_83.jpg │ ├── answer_83.py │ ├── answer_84.png │ ├── answer_84.py │ ├── answer_85.py │ ├── answer_86.py │ ├── answer_87.py │ ├── answer_88.py │ ├── answer_89.py │ └── answer_90.py ├── dataset │ ├── test_akahara_1.jpg │ ├── test_akahara_2.jpg │ ├── test_madara_1.jpg │ ├── test_madara_2.jpg │ ├── train_akahara_1.jpg │ ├── train_akahara_2.jpg │ ├── train_akahara_3.jpg │ ├── train_akahara_4.jpg │ ├── train_akahara_5.jpg │ ├── train_madara_1.jpg │ ├── train_madara_2.jpg │ ├── train_madara_3.jpg │ ├── train_madara_4.jpg │ └── train_madara_5.jpg ├── imori.jpg ├── out_mask.png └── thorino.jpg ├── Question_91_100 ├── README.md ├── answers │ ├── _answer_100.py │ ├── _answer_95.py │ ├── _answer_96.py │ ├── _answer_98.py │ ├── _answer_99.py │ ├── answer_100.jpg │ ├── answer_100.py │ ├── answer_100_gt.jpg │ ├── answer_91.jpg │ ├── answer_91.py │ ├── answer_92.jpg │ ├── answer_92.py │ ├── answer_92_k10.jpg │ ├── answer_92_m.jpg │ ├── answer_93.py │ ├── answer_94.jpg │ ├── answer_94.py │ ├── answer_95.py │ ├── answer_96.py │ ├── answer_97.py │ ├── answer_98.jpg │ ├── answer_98.py │ ├── answer_99.jpg │ └── answer_99.py ├── imori.jpg ├── imori_1.jpg ├── imori_2.jpg ├── imori_many.jpg └── madara.jpg ├── README.md ├── Tutorial ├── README.md ├── README_opencv_c_install.md ├── answer.cpp ├── assets │ ├── out.jpg │ ├── out_cpp.png │ ├── out_practice.jpg │ ├── sample.jpg │ ├── sample.png │ ├── sample1.png │ ├── sample10.png │ ├── sample2.png │ ├── sample3.png │ ├── sample4.png │ ├── sample5.png │ ├── sample6.png │ ├── sample7.png │ ├── sample8.png │ └── sample9.png ├── imori.jpg └── sample.cpp ├── assets ├── imori.jpg ├── imori_512x512.jpg └── sample.png ├── requirements.txt └── yobi ├── crop.py ├── crop_hog.py ├── crop_hog_nn.py ├── hog.py ├── neural.py ├── neuralnet.py ├── neuralnet2.py └── nn.py /Dataset/test/akahara_0009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/test/akahara_0009.jpg -------------------------------------------------------------------------------- /Dataset/test/akahara_0010.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/test/akahara_0010.jpg -------------------------------------------------------------------------------- /Dataset/test/akahara_0011.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/test/akahara_0011.jpg -------------------------------------------------------------------------------- /Dataset/test/madara_0009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/test/madara_0009.jpg -------------------------------------------------------------------------------- /Dataset/test/madara_0010.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/test/madara_0010.jpg -------------------------------------------------------------------------------- /Dataset/test/madara_0011.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/test/madara_0011.jpg -------------------------------------------------------------------------------- /Dataset/train/akahara_0004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/train/akahara_0004.jpg -------------------------------------------------------------------------------- /Dataset/train/akahara_0005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/train/akahara_0005.jpg -------------------------------------------------------------------------------- /Dataset/train/akahara_0007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/train/akahara_0007.jpg -------------------------------------------------------------------------------- /Dataset/train/madara_0001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/train/madara_0001.jpg -------------------------------------------------------------------------------- /Dataset/train/madara_0003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/train/madara_0003.jpg -------------------------------------------------------------------------------- /Dataset/train/madara_0006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Dataset/train/madara_0006.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_1.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_1.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | # Read image 4 | img = cv2.imread("imori.jpg") 5 | b = img[:, :, 0].copy() 6 | g = img[:, :, 1].copy() 7 | r = img[:, :, 2].copy() 8 | 9 | # RGB > BGR 10 | img[:, :, 0] = r 11 | img[:, :, 1] = g 12 | img[:, :, 2] = b 13 | 14 | # Save result 15 | cv2.imwrite("out.jpg", img) 16 | cv2.imshow("result", img) 17 | cv2.waitKey(0) 18 | cv2.destroyAllWindows() 19 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_10.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_10.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori_noise.jpg") 6 | H, W, C = img.shape 7 | 8 | 9 | # Median Filter 10 | K_size = 3 11 | 12 | ## Zero padding 13 | pad = K_size // 2 14 | out = np.zeros((H + pad*2, W + pad*2, C), dtype=np.float) 15 | out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float) 16 | 17 | tmp = out.copy() 18 | 19 | for y in range(H): 20 | for x in range(W): 21 | for c in range(C): 22 | out[pad+y, pad+x, c] = np.median(tmp[y:y+K_size, x:x+K_size, c]) 23 | 24 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 25 | 26 | # Save result 27 | cv2.imwrite("out.jpg", out) 28 | cv2.imshow("result", out) 29 | cv2.waitKey(0) 30 | cv2.destroyAllWindows() 31 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_2.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | b = img[:, :, 0].copy() 7 | g = img[:, :, 1].copy() 8 | r = img[:, :, 2].copy() 9 | 10 | # Gray scale 11 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 12 | out = out.astype(np.uint8) 13 | 14 | # Save result 15 | cv2.imwrite("out.jpg", out) 16 | cv2.imshow("result", out) 17 | cv2.waitKey(0) 18 | cv2.destroyAllWindows() 19 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_3.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_3.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | b = img[:, :, 0].copy() 7 | g = img[:, :, 1].copy() 8 | r = img[:, :, 2].copy() 9 | 10 | # Grayscale 11 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 12 | out = out.astype(np.uint8) 13 | 14 | # Binarization 15 | th = 128 16 | out[out < th] = 0 17 | out[out >= th] = 255 18 | 19 | # Save result 20 | cv2.imwrite("out.jpg", out) 21 | cv2.imshow("result", out) 22 | cv2.waitKey(0) 23 | cv2.destroyAllWindows() 24 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_4.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_4.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | out = out.astype(np.uint8) 12 | 13 | # Determine threshold of Otsu's binarization 14 | max_sigma = 0 15 | max_t = 0 16 | 17 | for _t in range(1, 255): 18 | v0 = out[np.where(out < _t)] 19 | m0 = np.mean(v0) if len(v0) > 0 else 0. 20 | w0 = len(v0) / (H * W) 21 | v1 = out[np.where(out >= _t)] 22 | m1 = np.mean(v1) if len(v1) > 0 else 0. 23 | w1 = len(v1) / (H * W) 24 | sigma = w0 * w1 * ((m0 - m1) ** 2) 25 | if sigma > max_sigma: 26 | max_sigma = sigma 27 | max_t = _t 28 | 29 | # Binarization 30 | print("threshold >>", max_t) 31 | th = max_t 32 | out[out < th] = 0 33 | out[out >= th] = 255 34 | 35 | # Save result 36 | cv2.imwrite("out.jpg", out) 37 | cv2.imshow("result", out) 38 | cv2.waitKey(0) 39 | cv2.destroyAllWindows() 40 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_5.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_5.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float32) / 255. 6 | 7 | # RGB > HSV 8 | out = np.zeros_like(img) 9 | 10 | max_v = np.max(img, axis=2).copy() 11 | min_v = np.min(img, axis=2).copy() 12 | min_arg = np.argmin(img, axis=2) 13 | 14 | H = np.zeros_like(max_v) 15 | 16 | H[np.where(max_v == min_v)] = 0 17 | ## if min == B 18 | ind = np.where(min_arg == 0) 19 | H[ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 20 | ## if min == R 21 | ind = np.where(min_arg == 2) 22 | H[ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 23 | ## if min == G 24 | ind = np.where(min_arg == 1) 25 | H[ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 26 | 27 | V = max_v.copy() 28 | S = max_v.copy() - min_v.copy() 29 | 30 | # Transpose Hue 31 | H = (H + 180) % 360 32 | 33 | # HSV > RGB 34 | 35 | C = S 36 | H_ = H / 60 37 | X = C * (1 - np.abs( H_ % 2 - 1)) 38 | Z = np.zeros_like(H) 39 | 40 | vals = [[Z,X,C], [Z,C,X], [X,C,Z], [C,X,Z], [C,Z,X], [X,Z,C]] 41 | 42 | for i in range(6): 43 | ind = np.where((i <= H_) & (H_ < (i+1))) 44 | out[..., 0][ind] = (V-C)[ind] + vals[i][0][ind] 45 | out[..., 1][ind] = (V-C)[ind] + vals[i][1][ind] 46 | out[..., 2][ind] = (V-C)[ind] + vals[i][2][ind] 47 | 48 | out[np.where(max_v == min_v)] = 0 49 | out = (out * 255).astype(np.uint8) 50 | 51 | # Save result 52 | cv2.imwrite("out.jpg", out) 53 | cv2.imshow("result", out) 54 | cv2.waitKey(0) 55 | cv2.destroyAllWindows() 56 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_6.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_6.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg") 6 | 7 | # Dicrease color 8 | out = img.copy() 9 | 10 | out = out // 64 * 64 + 32 11 | 12 | cv2.imwrite("out.jpg", out) 13 | cv2.imshow("result", out) 14 | cv2.waitKey(0) 15 | cv2.destroyAllWindows() 16 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_7.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_7.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg") 6 | 7 | # Average Pooling 8 | out = img.copy() 9 | 10 | H, W, C = img.shape 11 | G = 8 12 | Nh = int(H / G) 13 | Nw = int(W / G) 14 | 15 | for y in range(Nh): 16 | for x in range(Nw): 17 | for c in range(C): 18 | out[G*y:G*(y+1), G*x:G*(x+1), c] = np.mean(out[G*y:G*(y+1), G*x:G*(x+1), c]).astype(np.int) 19 | 20 | # Save result 21 | cv2.imwrite("out.jpg", out) 22 | cv2.imshow("result", out) 23 | cv2.waitKey(0) 24 | cv2.destroyAllWindows() 25 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_8.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_8.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg") 6 | 7 | # Max Pooling 8 | out = img.copy() 9 | 10 | H, W, C = img.shape 11 | G = 8 12 | Nh = int(H / G) 13 | Nw = int(W / G) 14 | 15 | for y in range(Nh): 16 | for x in range(Nw): 17 | for c in range(C): 18 | out[G*y:G*(y+1), G*x:G*(x+1), c] = np.max(out[G*y:G*(y+1), G*x:G*(x+1), c]) 19 | 20 | # Save result 21 | cv2.imwrite("out.jpg", out) 22 | cv2.imshow("result", out) 23 | cv2.waitKey(0) 24 | cv2.destroyAllWindows() 25 | -------------------------------------------------------------------------------- /Question_01_10/answers/answer_9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/answers/answer_9.jpg -------------------------------------------------------------------------------- /Question_01_10/answers/answer_9.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori_noise.jpg") 6 | H, W, C = img.shape 7 | 8 | 9 | # Gaussian Filter 10 | K_size = 3 11 | sigma = 1.3 12 | 13 | ## Zero padding 14 | pad = K_size // 2 15 | out = np.zeros((H + pad*2, W + pad*2, C), dtype=np.float) 16 | out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float) 17 | 18 | ## Kernel 19 | K = np.zeros((K_size, K_size), dtype=np.float) 20 | for x in range(-pad, -pad+K_size): 21 | 22 | for y in range(-pad, -pad+K_size): 23 | K[y+pad, x+pad] = np.exp( -(x**2 + y**2) / (2* (sigma**2))) 24 | K /= (sigma * np.sqrt(2 * np.pi)) 25 | K /= K.sum() 26 | 27 | tmp = out.copy() 28 | 29 | for y in range(H): 30 | for x in range(W): 31 | for c in range(C): 32 | out[pad+y, pad+x, c] = np.sum(K * tmp[y:y+K_size, x:x+K_size, c]) 33 | 34 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 35 | 36 | # Save result 37 | cv2.imwrite("out.jpg", out) 38 | cv2.imshow("result", out) 39 | cv2.waitKey(0) 40 | cv2.destroyAllWindows() 41 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, const char* argv[]){ 6 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 7 | 8 | int width = img.rows; 9 | int height = img.cols; 10 | 11 | cv::Mat out = img.clone(); 12 | 13 | for (int j=0; j(j, i)[0]; 16 | out.at(j, i)[0] = img.at(j,i)[2]; 17 | out.at(j,i)[2] = tmp; 18 | } 19 | } 20 | 21 | //cv::imwrite("out.jpg", out); 22 | cv::imshow("sample", out); 23 | cv::waitKey(0); 24 | cv::destroyAllWindows(); 25 | 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, const char* argv[]){ 7 | cv::Mat img = cv::imread("imori_noise.jpg", cv::IMREAD_COLOR); 8 | 9 | int width = img.rows; 10 | int height = img.cols; 11 | 12 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 13 | 14 | int k_size = 3; 15 | int p = floor(k_size / 2); 16 | 17 | // filtering 18 | double v = 0; 19 | int vs[k_size * k_size]; 20 | int count = 0; 21 | 22 | for (int j = 0; j < height; j++){ 23 | for (int i = 0; i < width; i++){ 24 | for (int c = 0; c < 3; c++){ 25 | v = 0; 26 | count = 0; 27 | 28 | for (int i = 0; i < k_size * k_size; i++){ 29 | vs[i] = 999; 30 | } 31 | 32 | for (int _j = -p; _j < p+1; _j++){ 33 | for (int _i = -p; _i < p+1; _i++){ 34 | if (((j+_j) >= 0) && ((i+_i) >= 0)){ 35 | vs[count++] = (int)img.at(j+_j, i+_i)[c]; 36 | } 37 | } 38 | } 39 | 40 | std::sort(vs, vs+(k_size * k_size)); 41 | out.at(j,i)[c] = (uchar)vs[int(floor(count / 2)) + 1]; 42 | } 43 | } 44 | } 45 | 46 | //cv::imwrite("out.jpg", out); 47 | cv::imshow("answer", out); 48 | cv::waitKey(0); 49 | cv::destroyAllWindows(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, const char* argv[]){ 6 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 7 | 8 | int width = img.rows; 9 | int height = img.cols; 10 | 11 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 12 | 13 | for (int j=0; j(j, i) = (int)((float)img.at(j,i)[0] * 0.0722 + \ 16 | (float)img.at(j,i)[1] * 0.7152 + \ 17 | (float)img.at(j,i)[2] * 0.2126); 18 | } 19 | } 20 | 21 | //cv::imwrite("out.jpg", out); 22 | cv::imshow("answer", out); 23 | cv::waitKey(0); 24 | cv::destroyAllWindows(); 25 | 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, const char* argv[]){ 6 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 7 | 8 | int width = img.rows; 9 | int height = img.cols; 10 | 11 | int th = 128; 12 | 13 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 14 | 15 | for (int j=0; j(j,i)[0] * 0.0722 + \ 18 | (float)img.at(j,i)[1] * 0.7152 + \ 19 | (float)img.at(j,i)[2] * 0.2126); 20 | if (val < th){ 21 | val = 0; 22 | } else { 23 | val = 255; 24 | } 25 | out.at(j,i) = val; 26 | } 27 | } 28 | 29 | //cv::imwrite("out.jpg", out); 30 | cv::imshow("answer", out); 31 | cv::waitKey(0); 32 | cv::destroyAllWindows(); 33 | 34 | return 0; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, const char* argv[]){ 7 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 8 | 9 | int width = img.rows; 10 | int height = img.cols; 11 | 12 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 13 | 14 | // gray 15 | int val = 0; 16 | for (int j = 0; j < height; j++){ 17 | for (int i = 0; i < width; i++){ 18 | val = (int)((float)img.at(j,i)[0] * 0.0722 + \ 19 | (float)img.at(j,i)[1] * 0.7152 + \ 20 | (float)img.at(j,i)[2] * 0.2126); 21 | out.at(j,i) = (uchar)val; 22 | } 23 | } 24 | 25 | // determine threshold 26 | double w0 = 0, w1 = 0; 27 | double m0 = 0, m1 = 0; 28 | double max_sb = 0, sb = 0; 29 | int th = 0; 30 | 31 | for (int t = 0; t < 255; t++){ 32 | w0 = 0; 33 | w1 = 0; 34 | m0 = 0; 35 | m1 = 0; 36 | for (int j = 0; j < height; j++){ 37 | for (int i = 0; i < width; i++){ 38 | val = (int)(out.at(j,i)); 39 | 40 | if (val < t){ 41 | w0++; 42 | m0 += val; 43 | } else { 44 | w1++; 45 | m1 += val; 46 | } 47 | } 48 | } 49 | 50 | m0 /= w0; 51 | m1 /= w1; 52 | w0 /= (height * width); 53 | w1 /= (height * width); 54 | sb = w0 * w1 * pow((m0 - m1), 2); 55 | 56 | if(sb > max_sb){ 57 | max_sb = sb; 58 | th = t; 59 | } 60 | 61 | } 62 | 63 | // binalization 64 | for (int j = 0; j < height; j++){ 65 | for (int i = 0; i < width; i++){ 66 | val = (int)(out.at(j,i)); 67 | if (val < th){ 68 | val = 0; 69 | } else { 70 | val = 255; 71 | } 72 | out.at(j,i) = (uchar)val; 73 | } 74 | } 75 | 76 | std::cout << "threshold >> " << th << std::endl; 77 | 78 | //cv::imwrite("out.jpg", out); 79 | cv::imshow("answer", out); 80 | cv::waitKey(0); 81 | cv::destroyAllWindows(); 82 | 83 | return 0; 84 | 85 | } 86 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, const char* argv[]){ 7 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 8 | 9 | int width = img.rows; 10 | int height = img.cols; 11 | 12 | double _max, _min; 13 | double r, g ,b; 14 | double h, s, v; 15 | double c, _h, x; 16 | double _r, _g, _b; 17 | 18 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 19 | 20 | for (int j=0; j(j,i)[2] / 255; 24 | g = (float)img.at(j,i)[1] / 255; 25 | b = (float)img.at(j,i)[0] / 255; 26 | 27 | _max = fmax(r, fmax(g, b)); 28 | _min = fmin(r, fmin(g, b)); 29 | 30 | if(_max == _min){ 31 | h = 0; 32 | } else if (_min == b) { 33 | h = 60 * (g - r) / (_max - _min) + 60; 34 | } else if (_min == r) { 35 | h = 60 * (b - g) / (_max - _min) + 180; 36 | } else if (_min == g) { 37 | h = 60 * (r - b) / (_max - _min) + 300; 38 | } 39 | v = _max; 40 | s = _max - _min; 41 | 42 | // inverse hue 43 | h = fmod((h + 180), 360); 44 | 45 | // inverse HSV 46 | c = s; 47 | _h = h / 60; 48 | x = c * (1 - abs(fmod(_h, 2) - 1)); 49 | 50 | _r = _g = _b = v - c; 51 | 52 | if (_h < 1) { 53 | _r += c; 54 | _g += x; 55 | } else if (_h < 2) { 56 | _r += x; 57 | _g += c; 58 | } else if (_h < 3) { 59 | _g += c; 60 | _b += x; 61 | } else if (_h < 4) { 62 | _g += x; 63 | _b += c; 64 | } else if (_h < 5) { 65 | _r += x; 66 | _b += c; 67 | } else if (_h < 6) { 68 | _r += c; 69 | _b += x; 70 | } 71 | 72 | out.at(j,i)[0] = (uchar)(_b * 255); 73 | out.at(j,i)[1] = (uchar)(_g * 255); 74 | out.at(j,i)[2] = (uchar)(_r * 255); 75 | } 76 | } 77 | 78 | //cv::imwrite("out.jpg", out); 79 | cv::imshow("answer", out); 80 | cv::waitKey(0); 81 | cv::destroyAllWindows(); 82 | 83 | return 0; 84 | 85 | } 86 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, const char* argv[]){ 7 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 8 | 9 | int width = img.rows; 10 | int height = img.cols; 11 | 12 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 13 | 14 | for (int j = 0; j < height; j++){ 15 | for (int i = 0; i < width; i++){ 16 | for (int c = 0; c < 3; c++){ 17 | out.at(j,i)[c] = (uchar)(floor((double)img.at(j,i)[c] / 64) * 64 + 32); 18 | } 19 | } 20 | } 21 | 22 | //cv::imwrite("out.jpg", out); 23 | cv::imshow("answer", out); 24 | cv::waitKey(0); 25 | cv::destroyAllWindows(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, const char* argv[]){ 7 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 8 | 9 | int width = img.rows; 10 | int height = img.cols; 11 | 12 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 13 | 14 | int r = 8; 15 | double v = 0; 16 | 17 | for (int j = 0; j < height; j+=r){ 18 | for (int i = 0; i < width; i+=r){ 19 | for (int c = 0; c < 3; c++){ 20 | v = 0; 21 | for (int _j = 0; _j < r; _j++){ 22 | for (int _i = 0; _i < r; _i++){ 23 | v += (double)img.at(j+_j, i+_i)[c]; 24 | } 25 | } 26 | v /= (r * r); 27 | for (int _j = 0; _j < r; _j++){ 28 | for (int _i = 0; _i < r; _i++){ 29 | out.at(j+_j, i+_i)[c] = (uchar)v; 30 | } 31 | } 32 | } 33 | } 34 | } 35 | 36 | //cv::imwrite("out.jpg", out); 37 | cv::imshow("answer", out); 38 | cv::waitKey(0); 39 | cv::destroyAllWindows(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, const char* argv[]){ 7 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 8 | 9 | int width = img.rows; 10 | int height = img.cols; 11 | 12 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 13 | 14 | int r = 8; 15 | uchar v = 0; 16 | 17 | for (int j = 0; j < height; j+=r){ 18 | for (int i = 0; i < width; i+=r){ 19 | for (int c = 0; c < 3; c++){ 20 | v = 0; 21 | for (int _j = 0; _j < r; _j++){ 22 | for (int _i = 0; _i < r; _i++){ 23 | v = fmax(img.at(j+_j, i+_i)[c], v); 24 | } 25 | } 26 | for (int _j = 0; _j < r; _j++){ 27 | for (int _i = 0; _i < r; _i++){ 28 | out.at(j+_j, i+_i)[c] = v; 29 | } 30 | } 31 | } 32 | } 33 | } 34 | 35 | //cv::imwrite("out.jpg", out); 36 | cv::imshow("answer", out); 37 | cv::waitKey(0); 38 | cv::destroyAllWindows(); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, const char* argv[]){ 7 | cv::Mat img = cv::imread("imori_noise.jpg", cv::IMREAD_COLOR); 8 | 9 | int width = img.rows; 10 | int height = img.cols; 11 | 12 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 13 | 14 | // prepare kernel 15 | double s = 1.3; 16 | int k_size = 3; 17 | int p = floor(k_size / 2); 18 | int x = 0, y = 0; 19 | double k_sum = 0; 20 | 21 | float k[k_size][k_size]; 22 | for (int j = 0; j < k_size; j++){ 23 | for (int i = 0; i < k_size; i++){ 24 | y = j - p; 25 | x = i - p; 26 | k[j][i] = 1 / (s * sqrt(2 * M_PI)) * exp( - (x*x + y*y) / (2*s*s)); 27 | k_sum += k[j][i]; 28 | } 29 | } 30 | 31 | for (int j = 0; j < k_size; j++){ 32 | for (int i = 0; i < k_size; i++){ 33 | k[j][i] /= k_sum; 34 | } 35 | } 36 | 37 | 38 | // filtering 39 | double v = 0; 40 | 41 | for (int j = 0; j < height; j++){ 42 | for (int i = 0; i < width; i++){ 43 | for (int c = 0; c < 3; c++){ 44 | v = 0; 45 | for (int _j = -p; _j < p+1; _j++){ 46 | for (int _i = -p; _i < p+1; _i++){ 47 | if (((j+_j) >= 0) && ((i+_i) >= 0)){ 48 | v += (double)img.at(j+_j, i+_i)[c] * k[_j+p][_i+p]; 49 | } 50 | } 51 | } 52 | out.at(j,i)[c] = v; 53 | } 54 | } 55 | } 56 | 57 | //cv::imwrite("out.jpg", out); 58 | cv::imshow("answer", out); 59 | cv::waitKey(0); 60 | cv::destroyAllWindows(); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Question_01_10/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/imori.jpg -------------------------------------------------------------------------------- /Question_01_10/imori_noise.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_01_10/imori_noise.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_11.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_11.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg") 6 | H, W, C = img.shape 7 | 8 | 9 | # Median Filter 10 | K_size = 3 11 | 12 | ## Zero padding 13 | pad = K_size // 2 14 | out = np.zeros((H + pad*2, W + pad*2, C), dtype=np.float) 15 | out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float) 16 | tmp = out.copy() 17 | 18 | for y in range(H): 19 | for x in range(W): 20 | for c in range(C): 21 | out[pad+y, pad+x, c] = np.mean(tmp[y:y+K_size, x:x+K_size, c]) 22 | 23 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 24 | 25 | # Save result 26 | cv2.imwrite("out.jpg", out) 27 | cv2.imshow("result", out) 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_12.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_12.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg") 6 | H, W, C = img.shape 7 | 8 | 9 | # Median Filter 10 | K_size = 3 11 | 12 | K = np.diag( [1] * K_size ).astype(np.float) 13 | K /= K_size 14 | 15 | ## Zero padding 16 | pad = K_size // 2 17 | out = np.zeros((H + pad*2, W + pad*2, C), dtype=np.float) 18 | out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float) 19 | tmp = out.copy() 20 | 21 | for y in range(H): 22 | for x in range(W): 23 | for c in range(C): 24 | out[pad+y, pad+x, c] = np.sum(K * tmp[y:y+K_size, x:x+K_size, c]) 25 | 26 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 27 | 28 | # Save result 29 | cv2.imwrite("out.jpg", out) 30 | cv2.imshow("result", out) 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_13.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_13.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | H, W, C = img.shape 7 | 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | gray = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | gray = gray.astype(np.uint8) 15 | 16 | # Max-Min Filter 17 | K_size = 3 18 | 19 | ## Zero padding 20 | pad = K_size // 2 21 | out = np.zeros((H + pad*2, W + pad*2), dtype=np.float) 22 | out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float) 23 | tmp = out.copy() 24 | 25 | for y in range(H): 26 | for x in range(W): 27 | out[pad+y, pad+x] = np.max(tmp[y:y+K_size, x:x+K_size]) - np.min(tmp[y:y+K_size, x:x+K_size]) 28 | 29 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 30 | 31 | # Save result 32 | cv2.imwrite("out.jpg", out) 33 | cv2.imshow("result", out) 34 | cv2.waitKey(0) 35 | cv2.destroyAllWindows() 36 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_14.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | H, W, C = img.shape 7 | 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | gray = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | gray = gray.astype(np.uint8) 15 | 16 | # sobel Filter 17 | K_size = 3 18 | 19 | ## Zero padding 20 | pad = K_size // 2 21 | out = np.zeros((H + pad*2, W + pad*2), dtype=np.float) 22 | out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float) 23 | tmp = out.copy() 24 | 25 | out_v = out.copy() 26 | out_h = out.copy() 27 | 28 | ## Sobel vertical 29 | Kv = [[0., -1., 0.],[0., 1., 0.],[0., 0., 0.]] 30 | ## Sobel horizontal 31 | Kh = [[0., 0., 0.],[-1., 1., 0.], [0., 0., 0.]] 32 | 33 | for y in range(H): 34 | for x in range(W): 35 | out_v[pad+y, pad+x] = np.sum(Kv * (tmp[y:y+K_size, x:x+K_size])) 36 | out_h[pad+y, pad+x] = np.sum(Kh * (tmp[y:y+K_size, x:x+K_size])) 37 | 38 | #out_v = np.abs(out_v) 39 | #out_h = np.abs(out_h) 40 | out_v[out_v < 0] = 0 41 | out_h[out_h < 0] = 0 42 | out_v[out_v > 255] = 255 43 | out_h[out_h > 255] = 255 44 | 45 | out_v = out_v[pad:pad+H, pad:pad+W].astype(np.uint8) 46 | out_h = out_h[pad:pad+H, pad:pad+W].astype(np.uint8) 47 | 48 | # Save result 49 | cv2.imwrite("out_v.jpg", out_v) 50 | cv2.imshow("result", out_v) 51 | cv2.waitKey(0) 52 | 53 | cv2.imwrite("out_h.jpg", out_h) 54 | cv2.imshow("result", out_h) 55 | cv2.waitKey(0) 56 | cv2.destroyAllWindows() 57 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_14_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_14_h.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_14_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_14_v.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_15.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | H, W, C = img.shape 7 | 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | gray = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | gray = gray.astype(np.uint8) 15 | 16 | # sobel Filter 17 | K_size = 3 18 | 19 | ## Zero padding 20 | pad = K_size // 2 21 | out = np.zeros((H + pad*2, W + pad*2), dtype=np.float) 22 | out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float) 23 | tmp = out.copy() 24 | 25 | ## Sobel vertical 26 | K = [[1., 0., -1.],[2., 0., -2.],[1., 0., -1.]] 27 | ## Sobel horizontal 28 | #K = [[1., 2., 1.],[0., 0., 0.], [-1., -2., -1.]] 29 | 30 | for y in range(H): 31 | for x in range(W): 32 | out[pad+y, pad+x] = np.sum(K * (tmp[y:y+K_size, x:x+K_size])) 33 | 34 | out[out < 0] = 0 35 | out[out > 255] = 255 36 | 37 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 38 | 39 | # Save result 40 | cv2.imwrite("out.jpg", out) 41 | cv2.imshow("result", out) 42 | cv2.waitKey(0) 43 | cv2.destroyAllWindows() 44 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_15_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_15_h.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_15_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_15_v.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_16.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | H, W, C = img.shape 7 | 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | gray = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | gray = gray.astype(np.uint8) 15 | 16 | # sobel Filter 17 | K_size = 3 18 | 19 | ## Zero padding 20 | pad = K_size // 2 21 | out = np.zeros((H + pad*2, W + pad*2), dtype=np.float) 22 | out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float) 23 | tmp = out.copy() 24 | 25 | ## Sobel vertical 26 | K = [[-1., -1., -1.],[0., 0., 0.], [1., 1., 1.]] 27 | ## Sobel horizontal 28 | #K = [[-1., 0., 1.],[-1., 0., 1.],[-1., 0., 1.]] 29 | 30 | for y in range(H): 31 | for x in range(W): 32 | out[pad+y, pad+x] = np.sum(K * (tmp[y:y+K_size, x:x+K_size])) 33 | 34 | out[out < 0] = 0 35 | out[out > 255] = 255 36 | 37 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 38 | 39 | # Save result 40 | cv2.imwrite("out.jpg", out) 41 | cv2.imshow("result", out) 42 | cv2.waitKey(0) 43 | cv2.destroyAllWindows() 44 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_16_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_16_h.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_16_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_16_v.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_17.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_17.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | H, W, C = img.shape 7 | 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | gray = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | gray = gray.astype(np.uint8) 15 | 16 | # sobel Filter 17 | K_size = 3 18 | 19 | ## Zero padding 20 | pad = K_size // 2 21 | out = np.zeros((H + pad*2, W + pad*2), dtype=np.float) 22 | out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float) 23 | tmp = out.copy() 24 | 25 | ## Laplacian vertical 26 | K = [[0., 1., 0.],[1., -4., 1.], [0., 1., 0.]] 27 | 28 | for y in range(H): 29 | for x in range(W): 30 | out[pad+y, pad+x] = np.sum(K * (tmp[y:y+K_size, x:x+K_size])) 31 | 32 | out[out < 0] = 0 33 | out[out > 255] = 255 34 | 35 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 36 | 37 | # Save result 38 | cv2.imwrite("out.jpg", out) 39 | cv2.imshow("result", out) 40 | cv2.waitKey(0) 41 | cv2.destroyAllWindows() 42 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_18.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_18.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori.jpg").astype(np.float) 6 | H, W, C = img.shape 7 | 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | gray = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | gray = gray.astype(np.uint8) 15 | 16 | # sobel Filter 17 | K_size = 3 18 | 19 | ## Zero padding 20 | pad = K_size // 2 21 | out = np.zeros((H + pad*2, W + pad*2), dtype=np.float) 22 | out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float) 23 | tmp = out.copy() 24 | 25 | ## Emboss vertical 26 | K = [[-2., -1., 0.],[-1., 1., 1.], [0., 1., 2.]] 27 | 28 | for y in range(H): 29 | for x in range(W): 30 | out[pad+y, pad+x] = np.sum(K * (tmp[y:y+K_size, x:x+K_size])) 31 | 32 | out[out < 0] = 0 33 | out[out > 255] = 255 34 | 35 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 36 | 37 | # Save result 38 | cv2.imwrite("out.jpg", out) 39 | cv2.imshow("result", out) 40 | cv2.waitKey(0) 41 | cv2.destroyAllWindows() 42 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_19.jpg -------------------------------------------------------------------------------- /Question_11_20/answers/answer_19.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read image 5 | img = cv2.imread("imori_noise.jpg") 6 | H, W, C = img.shape 7 | 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | gray = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | gray = gray.astype(np.uint8) 15 | 16 | # Gaussian Filter 17 | K_size = 5 18 | s = 3 19 | 20 | ## Zero padding 21 | pad = K_size // 2 22 | out = np.zeros((H + pad*2, W + pad*2), dtype=np.float) 23 | out[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float) 24 | tmp = out.copy() 25 | 26 | ## Kernel 27 | K = np.zeros((K_size, K_size), dtype=np.float) 28 | for x in range(-pad, -pad+K_size): 29 | for y in range(-pad, -pad+K_size): 30 | K[y+pad, x+pad] = (x**2 + y**2 - s**2) * np.exp( -(x**2 + y**2) / (2* (s**2))) 31 | K /= (2 * np.pi * (s**6)) 32 | K /= K.sum() 33 | 34 | for y in range(H): 35 | for x in range(W): 36 | out[pad+y, pad+x] = np.sum(K * tmp[y:y+K_size, x:x+K_size]) 37 | 38 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 39 | 40 | # Save result 41 | cv2.imwrite("out.jpg", out) 42 | cv2.imshow("result", out) 43 | cv2.waitKey(0) 44 | cv2.destroyAllWindows() 45 | -------------------------------------------------------------------------------- /Question_11_20/answers/answer_20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/answers/answer_20.png -------------------------------------------------------------------------------- /Question_11_20/answers/answer_20.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori_dark.jpg").astype(np.float) 7 | 8 | # Display histogram 9 | plt.hist(img.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 10 | plt.savefig("out.png") 11 | plt.show() 12 | -------------------------------------------------------------------------------- /Question_11_20/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/imori.jpg -------------------------------------------------------------------------------- /Question_11_20/imori_dark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/imori_dark.jpg -------------------------------------------------------------------------------- /Question_11_20/imori_noise.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_11_20/imori_noise.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_21.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori_dark.jpg").astype(np.float) 7 | H, W, C = img.shape 8 | 9 | # Trans [0, 255] 10 | a, b = 0., 255. 11 | 12 | vmin = img.min() 13 | vmax = img.max() 14 | 15 | out = img.copy() 16 | out[outb] = b 18 | out = (b-a) / (vmax - vmin) * (out - vmin) + a 19 | out = out.astype(np.uint8) 20 | 21 | # Display histogram 22 | plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 23 | plt.savefig("out_his.png") 24 | plt.show() 25 | 26 | # Save result 27 | cv2.imshow("result", out) 28 | cv2.waitKey(0) 29 | cv2.imwrite("out.jpg", out) 30 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_21_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_21_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_21_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_21_2.png -------------------------------------------------------------------------------- /Question_21_30/answers/answer_22.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori_dark.jpg").astype(np.float) 7 | H, W, C = img.shape 8 | 9 | # Trans [0, 255] 10 | m0 = 128 11 | s0 = 52 12 | 13 | m = np.mean(img) 14 | s = np.std(img) 15 | 16 | out = img.copy() 17 | out = s0 / s * (out - m) + m0 18 | out[out < 0] = 0 19 | out[out > 255] = 255 20 | out = out.astype(np.uint8) 21 | 22 | # Display histogram 23 | plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 24 | plt.savefig("out_his.png") 25 | plt.show() 26 | 27 | # Save result 28 | cv2.imshow("result", out) 29 | cv2.waitKey(0) 30 | cv2.imwrite("out.jpg", out) 31 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_22_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_22_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_22_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_22_2.png -------------------------------------------------------------------------------- /Question_21_30/answers/answer_23.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float) 7 | H, W, C = img.shape 8 | 9 | # Histogram flattening 10 | S = H * W * C * 1. 11 | 12 | out = img.copy() 13 | 14 | sum_h = 0. 15 | z_max = 255. 16 | 17 | for i in range(1, 255): 18 | ind = np.where(img == i) 19 | sum_h += len(img[ind]) 20 | z_prime = z_max / S * sum_h 21 | out[ind] = z_prime 22 | 23 | out = out.astype(np.uint8) 24 | 25 | # Display histogram 26 | plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 27 | plt.savefig("out_his.png") 28 | plt.show() 29 | 30 | # Save result 31 | cv2.imshow("result", out) 32 | cv2.waitKey(0) 33 | cv2.imwrite("out.jpg", out) 34 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_23_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_23_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_23_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_23_2.png -------------------------------------------------------------------------------- /Question_21_30/answers/answer_24.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_24.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_24.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori_gamma.jpg").astype(np.float) 7 | 8 | # Gammma correction 9 | c = 1. 10 | g = 2.2 11 | 12 | out = img.copy() 13 | out /= 255. 14 | out = (1/c * out) ** (1/g) 15 | 16 | out *= 255 17 | out = out.astype(np.uint8) 18 | 19 | # Save result 20 | cv2.imshow("result", out) 21 | cv2.waitKey(0) 22 | cv2.imwrite("out.jpg", out) 23 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_25.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_25.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_25.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float) 7 | H, W, C = img.shape 8 | 9 | 10 | # Nearest Neighbor 11 | a = 1.5 12 | aH = int(a * H) 13 | aW = int(a * W) 14 | 15 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 16 | x = np.tile(np.arange(aW), (aH, 1)) 17 | y = np.round(y / a).astype(np.int) 18 | x = np.round(x / a).astype(np.int) 19 | 20 | out = img[y,x] 21 | 22 | out = out.astype(np.uint8) 23 | 24 | # Save result 25 | cv2.imshow("result", out) 26 | cv2.waitKey(0) 27 | cv2.imwrite("out.jpg", out) 28 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_26.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_26.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_26.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float) 7 | H, W, C = img.shape 8 | 9 | 10 | # Bi-lenear 11 | a = 1.5 12 | aH = int(a * H) 13 | aW = int(a * W) 14 | 15 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 16 | x = np.tile(np.arange(aW), (aH, 1)) 17 | y = (y / a) 18 | x = (x / a) 19 | 20 | ix = np.floor(x).astype(np.int) 21 | iy = np.floor(y).astype(np.int) 22 | 23 | ix = np.minimum(ix, W-2) 24 | iy = np.minimum(iy, H-2) 25 | 26 | dx = x - ix 27 | dy = y - iy 28 | 29 | dx = np.repeat(np.expand_dims(dx, axis=-1), 3, axis=-1) 30 | dy = np.repeat(np.expand_dims(dy, axis=-1), 3, axis=-1) 31 | 32 | 33 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 34 | 35 | out[out>255] = 255 36 | out = out.astype(np.uint8) 37 | 38 | # Save result 39 | cv2.imshow("result", out) 40 | cv2.waitKey(0) 41 | cv2.imwrite("out.jpg", out) 42 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_27.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_27.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_27.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | 10 | # Bi-cubic 11 | a = 1.5 12 | aH = int(a * H) 13 | aW = int(a * W) 14 | 15 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 16 | x = np.tile(np.arange(aW), (aH, 1)) 17 | y = (y / a) 18 | x = (x / a) 19 | 20 | ix = np.floor(x).astype(np.int) 21 | iy = np.floor(y).astype(np.int) 22 | 23 | ix = np.minimum(ix, W-1) 24 | iy = np.minimum(iy, H-1) 25 | 26 | dx2 = x - ix 27 | dy2 = y - iy 28 | dx1 = dx2 + 1 29 | dy1 = dy2 + 1 30 | dx3 = 1 - dx2 31 | dy3 = 1 - dy2 32 | dx4 = 1 + dx3 33 | dy4 = 1 + dy3 34 | 35 | dxs = [dx1, dx2, dx3, dx4] 36 | dys = [dy1, dy2, dy3, dy4] 37 | 38 | def weight(t): 39 | a = -1. 40 | at = np.abs(t) 41 | w = np.zeros_like(t) 42 | ind = np.where(at <= 1) 43 | w[ind] = ((a+2) * np.power(at, 3) - (a+3) * np.power(at, 2) + 1)[ind] 44 | ind = np.where((at > 1) & (at <= 2)) 45 | w[ind] = (a*np.power(at, 3) - 5*a*np.power(at, 2) + 8*a*at - 4*a)[ind] 46 | return w 47 | 48 | w_sum = np.zeros((aH, aW, C), dtype=np.float32) 49 | out = np.zeros((aH, aW, C), dtype=np.float32) 50 | 51 | for j in range(-1, 3): 52 | for i in range(-1, 3): 53 | ind_x = np.minimum(np.maximum(ix + i, 0), W-1) 54 | ind_y = np.minimum(np.maximum(iy + j, 0), H-1) 55 | 56 | wx = weight(dxs[i+1]) 57 | wy = weight(dys[j+1]) 58 | wx = np.repeat(np.expand_dims(wx, axis=-1), 3, axis=-1) 59 | wy = np.repeat(np.expand_dims(wy, axis=-1), 3, axis=-1) 60 | 61 | w_sum += wx * wy 62 | out += wx * wy * img[ind_y, ind_x] 63 | 64 | out /= w_sum 65 | out[out>255] = 255 66 | out = out.astype(np.uint8) 67 | 68 | # Save result 69 | cv2.imshow("result", out) 70 | cv2.waitKey(0) 71 | cv2.imwrite("out.jpg", out) 72 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_28.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_28.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_28.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | 10 | # Affine 11 | a = 1. 12 | b = 0. 13 | c = 0. 14 | d = 1. 15 | tx = 30 16 | ty = -30 17 | 18 | y = np.arange(H).repeat(W).reshape(W, -1) 19 | x = np.tile(np.arange(W), (H, 1)) 20 | 21 | out = np.zeros((H+1, W+1, C), dtype=np.float32) 22 | 23 | x_new = a * x + b * y + tx 24 | y_new = c * x + d * y + ty 25 | 26 | x_new = np.minimum(np.maximum(x_new, 0), W).astype(np.int) 27 | y_new = np.minimum(np.maximum(y_new, 0), H).astype(np.int) 28 | 29 | out[y_new, x_new] = img[y, x] 30 | out = out[:H, :W] 31 | out = out.astype(np.uint8) 32 | 33 | # Save result 34 | cv2.imshow("result", out) 35 | cv2.waitKey(0) 36 | cv2.imwrite("out.jpg", out) 37 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_29.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | _img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = _img.shape 8 | 9 | 10 | # Affine 11 | a = 1.3 12 | b = 0. 13 | c = 0. 14 | d = 0.8 15 | tx = 30 16 | ty = -30 17 | 18 | img = np.zeros((H+2, W+2, C), dtype=np.float32) 19 | img[1:H+1, 1:W+1] = _img 20 | 21 | H_new = np.round(H * d).astype(np.int) 22 | W_new = np.round(W * a).astype(np.int) 23 | out = np.zeros((H_new+1, W_new+1, C), dtype=np.float32) 24 | 25 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 26 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 27 | 28 | adbc = a * d - b * c 29 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 30 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 31 | 32 | x = np.minimum(np.maximum(x, 0), W+1).astype(np.int) 33 | y = np.minimum(np.maximum(y, 0), H+1).astype(np.int) 34 | 35 | out[y_new, x_new] = img[y, x] 36 | 37 | out = out[:H_new, :W_new] 38 | out = out.astype(np.uint8) 39 | 40 | # Save result 41 | cv2.imshow("result", out) 42 | cv2.waitKey(0) 43 | cv2.imwrite("out.jpg", out) 44 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_29_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_29_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_29_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_29_2.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_30_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_30_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_30_1.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | _img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = _img.shape 8 | 9 | 10 | # Affine 11 | 12 | A = 30. 13 | theta = - np.pi * A / 180. 14 | 15 | a = np.cos(theta) 16 | b = -np.sin(theta) 17 | c = np.sin(theta) 18 | d = np.cos(theta) 19 | tx = 0 20 | ty = 0 21 | 22 | img = np.zeros((H+2, W+2, C), dtype=np.float32) 23 | img[1:H+1, 1:W+1] = _img 24 | 25 | H_new = np.round(H).astype(np.int) 26 | W_new = np.round(W).astype(np.int) 27 | out = np.zeros((H_new, W_new, C), dtype=np.float32) 28 | 29 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 30 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 31 | 32 | adbc = a * d - b * c 33 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 34 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 35 | 36 | x = np.minimum(np.maximum(x, 0), W+1).astype(np.int) 37 | y = np.minimum(np.maximum(y, 0), H+1).astype(np.int) 38 | 39 | out[y_new, x_new] = img[y, x] 40 | 41 | out = out.astype(np.uint8) 42 | 43 | # Save result 44 | cv2.imshow("result", out) 45 | cv2.waitKey(0) 46 | cv2.imwrite("out.jpg", out) 47 | -------------------------------------------------------------------------------- /Question_21_30/answers/answer_30_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/answers/answer_30_2.jpg -------------------------------------------------------------------------------- /Question_21_30/answers/answer_30_2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | _img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = _img.shape 8 | 9 | 10 | # Affine 11 | A = 30. 12 | theta = - np.pi * A / 180. 13 | 14 | a = np.cos(theta) 15 | b = -np.sin(theta) 16 | c = np.sin(theta) 17 | d = np.cos(theta) 18 | tx = 0 19 | ty = 0 20 | 21 | img = np.zeros((H+2, W+2, C), dtype=np.float32) 22 | img[1:H+1, 1:W+1] = _img 23 | 24 | H_new = np.round(H).astype(np.int) 25 | W_new = np.round(W).astype(np.int) 26 | out = np.zeros((H_new, W_new, C), dtype=np.float32) 27 | 28 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 29 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 30 | 31 | adbc = a * d - b * c 32 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 33 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 34 | 35 | dcx = (x.max() + x.min()) // 2 - W // 2 36 | dcy = (y.max() + y.min()) // 2 - H // 2 37 | 38 | x -= dcx 39 | y -= dcy 40 | 41 | x = np.minimum(np.maximum(x, 0), W+1).astype(np.int) 42 | y = np.minimum(np.maximum(y, 0), H+1).astype(np.int) 43 | 44 | out[y_new, x_new] = img[y, x] 45 | out = out.astype(np.uint8) 46 | 47 | # Save result 48 | cv2.imshow("result", out) 49 | cv2.waitKey(0) 50 | cv2.imwrite("out.jpg", out) 51 | -------------------------------------------------------------------------------- /Question_21_30/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/imori.jpg -------------------------------------------------------------------------------- /Question_21_30/imori_dark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/imori_dark.jpg -------------------------------------------------------------------------------- /Question_21_30/imori_gamma.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/imori_gamma.jpg -------------------------------------------------------------------------------- /Question_21_30/out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/out.jpg -------------------------------------------------------------------------------- /Question_21_30/question_24_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/question_24_1.jpg -------------------------------------------------------------------------------- /Question_21_30/question_24_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_21_30/question_24_2.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_31.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | _img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = _img.shape 8 | 9 | 10 | # Affine 11 | dx = 30. 12 | dy = 30. 13 | a = 1. 14 | b = dx / H 15 | c = dy / W 16 | d = 1. 17 | tx = 0. 18 | ty = 0. 19 | 20 | img = np.zeros((H+2, W+2, C), dtype=np.float32) 21 | img[1:H+1, 1:W+1] = _img 22 | 23 | H_new = np.ceil(dy + H).astype(np.int) 24 | W_new = np.ceil(dx + W).astype(np.int) 25 | out = np.zeros((H_new, W_new, C), dtype=np.float32) 26 | 27 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 28 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 29 | 30 | adbc = a * d - b * c 31 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 32 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 33 | 34 | x = np.minimum(np.maximum(x, 0), W+1).astype(np.int) 35 | y = np.minimum(np.maximum(y, 0), H+1).astype(np.int) 36 | 37 | out[y_new, x_new] = img[y, x] 38 | out = out.astype(np.uint8) 39 | 40 | # Save result 41 | cv2.imshow("result", out) 42 | cv2.waitKey(0) 43 | cv2.imwrite("out.jpg", out) 44 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_31_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_31_1.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_31_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_31_2.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_31_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_31_3.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_32.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_32.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_32.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray scale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | """ 13 | fimg = np.fft.fft2(gray) 14 | 15 | # 第1象限と第3象限, 第2象限と第4象限を入れ替え 16 | fimg = np.fft.fftshift(fimg) 17 | print(fimg.shape) 18 | # パワースペクトルの計算 19 | mag = 20*np.log(np.abs(fimg)) 20 | 21 | # 输入画像とスペクトル画像をグラフ描画 22 | plt.subplot(121) 23 | plt.imshow(gray, cmap = 'gray') 24 | plt.subplot(122) 25 | plt.imshow(mag, cmap = 'gray') 26 | plt.show() 27 | """ 28 | 29 | # DFT 30 | K = W 31 | L = H 32 | M = W 33 | N = H 34 | 35 | G = np.zeros((L, K), dtype=np.complex) 36 | 37 | x = np.tile(np.arange(W), (H, 1)) 38 | y = np.arange(H).repeat(W).reshape(H, -1) 39 | 40 | for l in range(L): 41 | for k in range(K): 42 | G[l, k] = np.sum(gray * np.exp(-2j * np.pi * (x * k / M + y * l / N))) / np.sqrt(M * N) 43 | #for n in range(N): 44 | # for m in range(M): 45 | # v += gray[n, m] * np.exp(-2j * np.pi * (m * k / M + n * l / N)) 46 | #G[l, k] = v / np.sqrt(M * N) 47 | 48 | ps = (np.abs(G) / np.abs(G).max() * 255).astype(np.uint8) 49 | cv2.imwrite("out_ps.jpg", ps) 50 | 51 | # IDFT 52 | out = np.zeros((H, W), dtype=np.float32) 53 | 54 | for n in range(N): 55 | for m in range(M): 56 | out[n,m] = np.abs(np.sum(G * np.exp(2j * np.pi * (x * m / M + y * n / N)))) / np.sqrt(M * N) 57 | 58 | out[out>255] = 255 59 | out = out.astype(np.uint8) 60 | 61 | # Save result 62 | cv2.imshow("result", out) 63 | cv2.waitKey(0) 64 | cv2.imwrite("out.jpg", out) 65 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_32_ps.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_32_ps.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_33.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_33.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_33.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray scale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # DFT 13 | K = W 14 | L = H 15 | M = W 16 | N = H 17 | 18 | G = np.zeros((L, K), dtype=np.complex) 19 | 20 | x = np.tile(np.arange(W), (H, 1)) 21 | y = np.arange(H).repeat(W).reshape(H, -1) 22 | 23 | for l in range(L): 24 | for k in range(K): 25 | G[l, k] = np.sum(gray * np.exp(-2j * np.pi * (x * k / M + y * l / N))) / np.sqrt(M * N) 26 | 27 | # low-pass filter 28 | _G = np.zeros_like(G) 29 | _G[:H//2, :W//2] = G[H//2:, W//2:] 30 | _G[:H//2, W//2:] = G[H//2:, :W//2] 31 | _G[H//2:, :W//2] = G[:H//2, W//2:] 32 | _G[H//2:, W//2:] = G[:H//2, :W//2] 33 | p = 0.5 34 | _x = x - W // 2 35 | _y = y - H // 2 36 | r = np.sqrt(_x ** 2 + _y ** 2) 37 | mask = np.ones((H, W), dtype=np.float32) 38 | mask[r>(W//2*p)] = 0 39 | 40 | _G *= mask 41 | 42 | G[:H//2, :W//2] = _G[H//2:, W//2:] 43 | G[:H//2, W//2:] = _G[H//2:, :W//2] 44 | G[H//2:, :W//2] = _G[:H//2, W//2:] 45 | G[H//2:, W//2:] = _G[:H//2, :W//2] 46 | 47 | # IDFT 48 | out = np.zeros((H, W), dtype=np.float32) 49 | 50 | for n in range(N): 51 | for m in range(M): 52 | out[n,m] = np.abs(np.sum(G * np.exp(2j * np.pi * (x * m / M + y * n / N)))) / np.sqrt(M * N) 53 | 54 | out[out>255] = 255 55 | out = out.astype(np.uint8) 56 | 57 | # Save result 58 | cv2.imshow("result", out) 59 | cv2.waitKey(0) 60 | cv2.imwrite("out.jpg", out) 61 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_34.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_34.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_34.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray scale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # DFT 13 | K = W 14 | L = H 15 | M = W 16 | N = H 17 | 18 | G = np.zeros((L, K), dtype=np.complex) 19 | 20 | x = np.tile(np.arange(W), (H, 1)) 21 | y = np.arange(H).repeat(W).reshape(H, -1) 22 | 23 | for l in range(L): 24 | for k in range(K): 25 | G[l, k] = np.sum(gray * np.exp(-2j * np.pi * (x * k / M + y * l / N))) / np.sqrt(M * N) 26 | 27 | # low-pass filter 28 | _G = np.zeros_like(G) 29 | _G[:H//2, :W//2] = G[H//2:, W//2:] 30 | _G[:H//2, W//2:] = G[H//2:, :W//2] 31 | _G[H//2:, :W//2] = G[:H//2, W//2:] 32 | _G[H//2:, W//2:] = G[:H//2, :W//2] 33 | p = 0.2 34 | _x = x - W // 2 35 | _y = y - H // 2 36 | r = np.sqrt(_x ** 2 + _y ** 2) 37 | mask = np.ones((H, W), dtype=np.float32) 38 | mask[r<(W//2*p)] = 0 39 | 40 | _G *= mask 41 | 42 | G[:H//2, :W//2] = _G[H//2:, W//2:] 43 | G[:H//2, W//2:] = _G[H//2:, :W//2] 44 | G[H//2:, :W//2] = _G[:H//2, W//2:] 45 | G[H//2:, W//2:] = _G[:H//2, :W//2] 46 | 47 | # IDFT 48 | out = np.zeros((H, W), dtype=np.float32) 49 | 50 | for n in range(N): 51 | for m in range(M): 52 | out[n,m] = np.abs(np.sum(G * np.exp(2j * np.pi * (x * m / M + y * n / N)))) / np.sqrt(M * N) 53 | 54 | out[out>255] = 255 55 | out = out.astype(np.uint8) 56 | 57 | # Save result 58 | cv2.imshow("result", out) 59 | cv2.waitKey(0) 60 | cv2.imwrite("out.jpg", out) 61 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_35.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_35.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_35.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray scale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # DFT 13 | K = W 14 | L = H 15 | M = W 16 | N = H 17 | 18 | G = np.zeros((L, K), dtype=np.complex) 19 | 20 | x = np.tile(np.arange(W), (H, 1)) 21 | y = np.arange(H).repeat(W).reshape(H, -1) 22 | 23 | for l in range(L): 24 | for k in range(K): 25 | G[l, k] = np.sum(gray * np.exp(-2j * np.pi * (x * k / M + y * l / N))) / np.sqrt(M * N) 26 | 27 | # low-pass filter 28 | _G = np.zeros_like(G) 29 | _G[:H//2, :W//2] = G[H//2:, W//2:] 30 | _G[:H//2, W//2:] = G[H//2:, :W//2] 31 | _G[H//2:, :W//2] = G[:H//2, W//2:] 32 | _G[H//2:, W//2:] = G[:H//2, :W//2] 33 | p1 = 0.1 34 | p2 = 0.5 35 | _x = x - W // 2 36 | _y = y - H // 2 37 | r = np.sqrt(_x ** 2 + _y ** 2) 38 | mask = np.zeros((H, W), dtype=np.float32) 39 | mask[np.where((r > (W//2*p1)) & (r < (W//2*p2)))] = 1 40 | 41 | _G *= mask 42 | 43 | G[:H//2, :W//2] = _G[H//2:, W//2:] 44 | G[:H//2, W//2:] = _G[H//2:, :W//2] 45 | G[H//2:, :W//2] = _G[:H//2, W//2:] 46 | G[H//2:, W//2:] = _G[:H//2, :W//2] 47 | 48 | # IDFT 49 | out = np.zeros((H, W), dtype=np.float32) 50 | 51 | for n in range(N): 52 | for m in range(M): 53 | out[n,m] = np.abs(np.sum(G * np.exp(2j * np.pi * (x * m / M + y * n / N)))) / np.sqrt(M * N) 54 | 55 | out[out>255] = 255 56 | out = out.astype(np.uint8) 57 | 58 | # Save result 59 | cv2.imshow("result", out) 60 | cv2.waitKey(0) 61 | cv2.imwrite("out.jpg", out) 62 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_36.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_36.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_36.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray scale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # DCT 13 | T = 8 14 | K = 8 15 | X = np.zeros((H, W), dtype=np.float32) 16 | 17 | #indx = np.tile(np.arange(T), (T, 1)) 18 | #indy = np.arange(T).repeat(T).reshape(T, -1) 19 | #dct = np.ones_like(indx, dtype=np.float32) 20 | #dct[:, 0] /= np.sqrt(2) 21 | #dct[0] /= np.sqrt(2) 22 | 23 | def w(x, y, u, v): 24 | cu = 1. 25 | cv = 1. 26 | if u == 0: 27 | cu /= np.sqrt(2) 28 | if v == 0: 29 | cv /= np.sqrt(2) 30 | theta = np.pi / (2 * T) 31 | return (( 2 * cu * cv / T) * np.cos((2*x+1)*u*theta) * np.cos((2*y+1)*v*theta)) 32 | 33 | for yi in range(0, H, T): 34 | for xi in range(0, W, T): 35 | for v in range(T): 36 | for u in range(T): 37 | for y in range(T): 38 | for x in range(T): 39 | X[v+yi, u+xi] += gray[y+yi, x+xi] * w(x,y,u,v) 40 | """ 41 | _x = indx + xi * T 42 | _y = indy + yi * T 43 | _u = u + xi * T 44 | _v = v + yi * T 45 | X[_v, _u] = np.sum(C * gray[_y, _x] * np.cos((2*indx+1)*u*np.pi/(2*T)) * np.cos((2*indy+1)*v*np.pi/(2*T))) 46 | """ 47 | 48 | # IDCT 49 | out = np.zeros((H, W), dtype=np.float32) 50 | 51 | for yi in range(0, H, T): 52 | for xi in range(0, W, T): 53 | for y in range(T): 54 | for x in range(T): 55 | for v in range(K): 56 | for u in range(K): 57 | out[y+yi, x+xi] += X[v+yi, u+xi] * w(x,y,u,v) 58 | """ 59 | _u = indx + xi * T 60 | _v = indy + yi * T 61 | _x = x + yi * T 62 | _y = y + xi * T 63 | out[_y, _x] = np.sum(C * X[_v, _u] * np.cos((2*x+1)*indx*np.pi/(2*T)) * np.cos((2*y+1)*indy*np.pi/(2*T))) * 4. / (T ** 2) 64 | """ 65 | out[out>255] = 255 66 | out = np.round(out).astype(np.uint8) 67 | 68 | # Save result 69 | cv2.imshow("result", out) 70 | cv2.waitKey(0) 71 | cv2.imwrite("out.jpg", out) 72 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_37.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_37.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_37.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray scale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # DCT 13 | T = 8 14 | K = 4 15 | X = np.zeros((H, W), dtype=np.float64) 16 | #indx = np.tile(np.arange(T), (T, 1)) 17 | #indy = np.arange(T).repeat(T).reshape(T, -1) 18 | #dct = np.ones_like(indx, dtype=np.float32) 19 | #dct[:, 0] /= np.sqrt(2) 20 | #dct[0] /= np.sqrt(2) 21 | 22 | def w(x, y, u, v): 23 | cu = 1. 24 | cv = 1. 25 | if u == 0: 26 | cu /= np.sqrt(2) 27 | if v == 0: 28 | cv /= np.sqrt(2) 29 | theta = np.pi / (2 * T) 30 | return (( 2 * cu * cv / T) * np.cos((2*x+1)*u*theta) * np.cos((2*y+1)*v*theta)) 31 | 32 | for yi in range(0, H, T): 33 | for xi in range(0, W, T): 34 | for v in range(T): 35 | for u in range(T): 36 | for y in range(T): 37 | for x in range(T): 38 | X[v+yi, u+xi] += gray[y+yi, x+xi] * w(x,y,u,v) 39 | """ 40 | _x = indx + xi * T 41 | _y = indy + yi * T 42 | _u = u + xi * T 43 | _v = v + yi * T 44 | X[_v, _u] = np.sum(C * gray[_y, _x] * np.cos((2*indx+1)*u*np.pi/(2*T)) * np.cos((2*indy+1)*v*np.pi/(2*T))) 45 | """ 46 | 47 | # IDCT 48 | out = np.zeros((H, W), dtype=np.float64) 49 | 50 | for yi in range(0, H, T): 51 | for xi in range(0, W, T): 52 | for y in range(T): 53 | for x in range(T): 54 | for v in range(K): 55 | for u in range(K): 56 | out[y+yi, x+xi] += X[v+yi, u+xi] * w(x,y,u,v) 57 | """ 58 | _u = indx + xi * T 59 | _v = indy + yi * T 60 | _x = x + yi * T 61 | _y = y + xi * T 62 | out[_y, _x] = np.sum(C * X[_v, _u] * np.cos((2*x+1)*indx*np.pi/(2*T)) * np.cos((2*y+1)*indy*np.pi/(2*T))) * 4. / (T ** 2) 63 | """ 64 | 65 | out[out>255] = 255 66 | out = np.floor(out).astype(np.uint8) 67 | 68 | # MSE 69 | v_max = 255. 70 | mse = np.sum(np.power(np.abs(gray.astype(np.float32) - out.astype(np.float32)), 2)) / (H * W) 71 | psnr = 10 * np.log10(v_max ** 2 / mse) 72 | 73 | print("PSNR >>", psnr) 74 | 75 | bitrate = 1. * T * K ** 2 / (T ** 2) 76 | print("bitrate >>", bitrate) 77 | 78 | # Save result 79 | cv2.imshow("result", out) 80 | cv2.waitKey(0) 81 | cv2.imwrite("out.jpg", out) 82 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_38.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_38.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_38.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray scale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # DCT 13 | T = 8 14 | K = 8 15 | X = np.zeros((H, W), dtype=np.float64) 16 | #indx = np.tile(np.arange(T), (T, 1)) 17 | #indy = np.arange(T).repeat(T).reshape(T, -1) 18 | #dct = np.ones_like(indx, dtype=np.float32) 19 | #dct[:, 0] /= np.sqrt(2) 20 | #dct[0] /= np.sqrt(2) 21 | 22 | Q = np.array(((16, 11, 10, 16, 24, 40, 51, 61), 23 | (12, 12, 14, 19, 26, 58, 60, 55), 24 | (14, 13, 16, 24, 40, 57, 69, 56), 25 | (14, 17, 22, 29, 51, 87, 80, 62), 26 | (18, 22, 37, 56, 68, 109, 103, 77), 27 | (24, 35, 55, 64, 81, 104, 113, 92), 28 | (49, 64, 78, 87, 103, 121, 120, 101), 29 | (72, 92, 95, 98, 112, 100, 103, 99)), dtype=np.float32) 30 | 31 | def w(x, y, u, v): 32 | cu = 1. 33 | cv = 1. 34 | if u == 0: 35 | cu /= np.sqrt(2) 36 | if v == 0: 37 | cv /= np.sqrt(2) 38 | theta = np.pi / (2 * T) 39 | return (( 2 * cu * cv / T) * np.cos((2*x+1)*u*theta) * np.cos((2*y+1)*v*theta)) 40 | 41 | for yi in range(0, H, T): 42 | for xi in range(0, W, T): 43 | for v in range(T): 44 | for u in range(T): 45 | for y in range(T): 46 | for x in range(T): 47 | X[v+yi, u+xi] += gray[y+yi, x+xi] * w(x,y,u,v) 48 | X[yi:yi+T, xi:xi+T] = np.round(X[yi:yi+T, xi:xi+T] / Q) * Q 49 | 50 | #_x = indx + xi * T 51 | #_y = indy + yi * T 52 | #_u = u + xi * T 53 | #_v = v + yi * T 54 | #X[_v, _u] = np.sum(C * gray[_y, _x] * np.cos((2*indx+1)*u*np.pi/(2*T)) * np.cos((2*indy+1)*v*np.pi/(2*T))) 55 | 56 | # IDCT 57 | out = np.zeros((H, W), dtype=np.float64) 58 | 59 | for yi in range(0, H, T): 60 | for xi in range(0, W, T): 61 | for y in range(T): 62 | for x in range(T): 63 | for v in range(K): 64 | for u in range(K): 65 | out[y+yi, x+xi] += X[v+yi, u+xi] * w(x,y,u,v) 66 | """ 67 | _u = indx + xi * T 68 | _v = indy + yi * T 69 | _x = x + yi * T 70 | _y = y + xi * T 71 | out[_y, _x] = np.sum(C * X[_v, _u] * np.cos((2*x+1)*indx*np.pi/(2*T)) * np.cos((2*y+1)*indy*np.pi/(2*T))) * 4. / (T ** 2) 72 | """ 73 | out[out>255] = 255 74 | out = np.floor(out).astype(np.uint8) 75 | 76 | # MSE 77 | v_max = 255. 78 | mse = np.sum(np.power(np.abs(gray.astype(np.float32) - out.astype(np.float32)), 2)) / (H * W) 79 | psnr = 10 * np.log10(v_max ** 2 / mse) 80 | 81 | print("PSNR >>", psnr) 82 | 83 | bitrate = 1. * T * K ** 2 / (T ** 2) 84 | print("bitrate >>", bitrate) 85 | 86 | # Save result 87 | cv2.imshow("result", out) 88 | cv2.waitKey(0) 89 | cv2.imwrite("out.jpg", out) 90 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_39.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_39.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_39.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # RGB > YCbCr 10 | Y = 0.2990 * img[..., 2] + 0.5870 * img[..., 1] + 0.1140 * img[..., 0] 11 | Cb = -0.1687 * img[..., 2] - 0.3313 * img[..., 1] + 0.5 * img[..., 0] + 128. 12 | Cr = 0.5 * img[..., 2] - 0.4187 * img[..., 1] - 0.0813 * img[..., 0] + 128. 13 | 14 | Y *= 0.7 15 | 16 | # YCbCr > RGB 17 | out = np.zeros_like(img, dtype=np.float32) 18 | out[..., 2] = Y + (Cr - 128.) * 1.4020 19 | out[..., 1] = Y - (Cb - 128.) * 0.3441 - (Cr - 128.) * 0.7139 20 | out[..., 0] = Y + (Cb - 128.) * 1.7718 21 | 22 | out = out.astype(np.uint8) 23 | 24 | # Save result 25 | cv2.imshow("result", out) 26 | cv2.waitKey(0) 27 | cv2.imwrite("out.jpg", out) 28 | -------------------------------------------------------------------------------- /Question_31_40/answers/answer_40.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/answers/answer_40.jpg -------------------------------------------------------------------------------- /Question_31_40/answers/answer_40.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # RGB > YCbCr 10 | Y = 0.2990 * img[..., 2] + 0.5870 * img[..., 1] + 0.1140 * img[..., 0] 11 | Cb = -0.1687 * img[..., 2] - 0.3313 * img[..., 1] + 0.5 * img[..., 0] + 128. 12 | Cr = 0.5 * img[..., 2] - 0.4187 * img[..., 1] - 0.0813 * img[..., 0] + 128. 13 | 14 | YCC = np.zeros_like(img, dtype=np.float32) 15 | YCC[..., 0] = Y 16 | YCC[..., 1] = Cb 17 | YCC[..., 2] = Cr 18 | 19 | 20 | # DCT 21 | T = 8 22 | K = 8 23 | X = np.zeros((H, W, C), dtype=np.float64) 24 | 25 | Q1 = np.array(((16, 11, 10, 16, 24, 40, 51, 61), 26 | (12, 12, 14, 19, 26, 58, 60, 55), 27 | (14, 13, 16, 24, 40, 57, 69, 56), 28 | (14, 17, 22, 29, 51, 87, 80, 62), 29 | (18, 22, 37, 56, 68, 109, 103, 77), 30 | (24, 35, 55, 64, 81, 104, 113, 92), 31 | (49, 64, 78, 87, 103, 121, 120, 101), 32 | (72, 92, 95, 98, 112, 100, 103, 99)), dtype=np.float32) 33 | 34 | Q2 = np.array(((17, 18, 24, 47, 99, 99, 99, 99), 35 | (18, 21, 26, 66, 99, 99, 99, 99), 36 | (24, 26, 56, 99, 99, 99, 99, 99), 37 | (47, 66, 99, 99, 99, 99, 99, 99), 38 | (99, 99, 99, 99, 99, 99, 99, 99), 39 | (99, 99, 99, 99, 99, 99, 99, 99), 40 | (99, 99, 99, 99, 99, 99, 99, 99), 41 | (99, 99, 99, 99, 99, 99, 99, 99)), dtype=np.float32) 42 | 43 | def w(x, y, u, v): 44 | cu = 1. 45 | cv = 1. 46 | if u == 0: 47 | cu /= np.sqrt(2) 48 | if v == 0: 49 | cv /= np.sqrt(2) 50 | theta = np.pi / (2 * T) 51 | return (( 2 * cu * cv / T) * np.cos((2*x+1)*u*theta) * np.cos((2*y+1)*v*theta)) 52 | 53 | for yi in range(0, H, T): 54 | for xi in range(0, W, T): 55 | for v in range(T): 56 | for u in range(T): 57 | for y in range(T): 58 | for x in range(T): 59 | for c in range(C): 60 | X[v+yi, u+xi, c] += YCC[y+yi, x+xi, c] * w(x,y,u,v) 61 | 62 | X[yi:yi+T, xi:xi+T, 0] = np.round(X[yi:yi+T, xi:xi+T, 0] / Q1) * Q1 63 | X[yi:yi+T, xi:xi+T, 1] = np.round(X[yi:yi+T, xi:xi+T, 1] / Q2) * Q2 64 | X[yi:yi+T, xi:xi+T, 2] = np.round(X[yi:yi+T, xi:xi+T, 2] / Q2) * Q2 65 | 66 | 67 | # IDCT 68 | IYCC = np.zeros((H, W, 3), dtype=np.float64) 69 | 70 | for yi in range(0, H, T): 71 | for xi in range(0, W, T): 72 | for y in range(T): 73 | for x in range(T): 74 | for v in range(K): 75 | for u in range(K): 76 | IYCC[y+yi, x+xi] += X[v+yi, u+xi] * w(x,y,u,v) 77 | 78 | 79 | # YCbCr > RGB 80 | out = np.zeros_like(img, dtype=np.float32) 81 | out[..., 2] = IYCC[..., 0] + (IYCC[..., 2] - 128.) * 1.4020 82 | out[..., 1] = IYCC[..., 0] - (IYCC[..., 1] - 128.) * 0.3441 - (IYCC[..., 2] - 128.) * 0.7139 83 | out[..., 0] = IYCC[..., 0] + (IYCC[..., 1] - 128.) * 1.7718 84 | 85 | out[out>255] = 255 86 | out = out.astype(np.uint8) 87 | 88 | # MSE 89 | v_max = 255. 90 | mse = np.sum(np.power(np.abs(img.astype(np.float32) - out.astype(np.float32)), 2)) / (H * W * C) 91 | psnr = 10 * np.log10(v_max ** 2 / mse) 92 | 93 | print("PSNR >>", psnr) 94 | 95 | bitrate = 1. * T * K ** 2 / (T ** 2) 96 | print("bitrate >>", bitrate) 97 | 98 | # Save result 99 | cv2.imshow("result", out) 100 | cv2.waitKey(0) 101 | cv2.imwrite("out.jpg", out) 102 | -------------------------------------------------------------------------------- /Question_31_40/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/imori.jpg -------------------------------------------------------------------------------- /Question_31_40/imori_gray.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_31_40/imori_gray.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_4.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_41.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Gaussian Filter 13 | K_size = 5 14 | sigma = 1.4 15 | 16 | ## Zero padding 17 | pad = K_size // 2 18 | gau = np.zeros((H + pad*2, W + pad*2), dtype=np.float32) 19 | #gau[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float32) 20 | gau = np.pad(gray, (pad, pad), 'edge') 21 | tmp = gau.copy() 22 | 23 | ## Kernel 24 | K = np.zeros((K_size, K_size), dtype=np.float32) 25 | for x in range(-pad, -pad+K_size): 26 | for y in range(-pad, -pad+K_size): 27 | K[y+pad, x+pad] = np.exp( -(x**2 + y**2) / (2* (sigma**2))) 28 | K /= (sigma * np.sqrt(2 * np.pi)) 29 | K /= K.sum() 30 | 31 | for y in range(H): 32 | for x in range(W): 33 | gau[pad+y, pad+x] = np.sum(K * tmp[y:y+K_size, x:x+K_size]) 34 | 35 | ## Sobel vertical 36 | KSV = np.array(((-1., -2., -1.), (0., 0., 0.), (1., 2., 1.)), dtype=np.float32) 37 | ## Sobel horizontal 38 | KSH = np.array(((-1., 0., 1.), (-2., 0., 2.), (-1., 0., 1.)), dtype=np.float32) 39 | 40 | gau = gau[pad-1:H+pad+1, pad-1:W+pad+1] 41 | fy = np.zeros_like(gau, dtype=np.float32) 42 | fx = np.zeros_like(gau, dtype=np.float32) 43 | K_size = 3 44 | pad = K_size // 2 45 | 46 | for y in range(H): 47 | for x in range(W): 48 | fy[pad+y, pad+x] = np.sum(KSV * gau[y:y+K_size, x:x+K_size]) 49 | fx[pad+y, pad+x] = np.sum(KSH * gau[y:y+K_size, x:x+K_size]) 50 | 51 | fx = fx[pad:pad+H, pad:pad+W] 52 | fy = fy[pad:pad+H, pad:pad+W] 53 | 54 | # Non-maximum suppression 55 | edge = np.sqrt(np.power(fx, 2) + np.power(fy, 2)) 56 | fx[fx == 0] = 1e-5 57 | tan = np.arctan(fy / fx) 58 | ## Angle quantization 59 | angle = np.zeros_like(tan, dtype=np.uint8) 60 | angle[np.where((tan > -0.4142) & (tan <= 0.4142))] = 0 61 | angle[np.where((tan > 0.4142) & (tan < 2.4142))] = 45 62 | angle[np.where((tan >= 2.4142) | (tan <= -2.4142))] = 95 63 | angle[np.where((tan > -2.4142) & (tan <= -0.4142))] = 135 64 | 65 | out = angle.astype(np.uint8) 66 | 67 | # Save result 68 | cv2.imwrite("out.jpg", out) 69 | cv2.imshow("result", out) 70 | cv2.waitKey(0) 71 | cv2.destroyAllWindows() 72 | -------------------------------------------------------------------------------- /Question_41_50/answers/answer_41_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_41_1.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_41_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_41_2.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_42.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_42.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_42.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Gray 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Gaussian Filter 13 | K_size = 5 14 | sigma = 1.4 15 | 16 | ## Zero padding 17 | pad = K_size // 2 18 | gau = np.zeros((H + pad*2, W + pad*2), dtype=np.float32) 19 | #gau[pad:pad+H, pad:pad+W] = gray.copy().astype(np.float32) 20 | gau = np.pad(gray, (pad, pad), 'edge') 21 | tmp = gau.copy() 22 | 23 | ## Kernel 24 | K = np.zeros((K_size, K_size), dtype=np.float32) 25 | for x in range(-pad, -pad+K_size): 26 | for y in range(-pad, -pad+K_size): 27 | K[y+pad, x+pad] = np.exp( -(x**2 + y**2) / (2* (sigma**2))) 28 | K /= (sigma * np.sqrt(2 * np.pi)) 29 | K /= K.sum() 30 | 31 | for y in range(H): 32 | for x in range(W): 33 | gau[pad+y, pad+x] = np.sum(K * tmp[y:y+K_size, x:x+K_size]) 34 | 35 | ## Sobel vertical 36 | KSV = np.array(((-1., -2., -1.), (0., 0., 0.), (1., 2., 1.)), dtype=np.float32) 37 | ## Sobel horizontal 38 | KSH = np.array(((-1., 0., 1.), (-2., 0., 2.), (-1., 0., 1.)), dtype=np.float32) 39 | 40 | gau = gau[pad-1:H+pad+1, pad-1:W+pad+1] 41 | fy = np.zeros_like(gau, dtype=np.float32) 42 | fx = np.zeros_like(gau, dtype=np.float32) 43 | K_size = 3 44 | pad = K_size // 2 45 | 46 | for y in range(H): 47 | for x in range(W): 48 | fy[pad+y, pad+x] = np.sum(KSV * gau[y:y+K_size, x:x+K_size]) 49 | fx[pad+y, pad+x] = np.sum(KSH * gau[y:y+K_size, x:x+K_size]) 50 | 51 | fx = fx[pad:pad+H, pad:pad+W] 52 | fy = fy[pad:pad+H, pad:pad+W] 53 | 54 | # Non-maximum suppression 55 | edge = np.sqrt(np.power(fx, 2) + np.power(fy, 2)) 56 | fx[fx == 0] = 1e-5 57 | tan = np.arctan(fy / fx) 58 | ## Angle quantization 59 | angle = np.zeros_like(tan, dtype=np.uint8) 60 | angle[np.where((tan > -0.4142) & (tan <= 0.4142))] = 0 61 | angle[np.where((tan > 0.4142) & (tan < 2.4142))] = 45 62 | angle[np.where((tan >= 2.4142) | (tan <= -2.4142))] = 95 63 | angle[np.where((tan > -2.4142) & (tan <= -0.4142))] = 135 64 | 65 | for y in range(H): 66 | for x in range(W): 67 | if angle[y, x] == 0: 68 | dx1, dy1, dx2, dy2 = -1, 0, 1, 0 69 | elif angle[y, x] == 45: 70 | dx1, dy1, dx2, dy2 = -1, 1, 1, -1 71 | elif angle[y, x] == 90: 72 | dx1, dy1, dx2, dy2 = 0, -1, 0, 1 73 | elif angle[y, x] == 135: 74 | dx1, dy1, dx2, dy2 = -1, -1, 1, 1 75 | if x == 0: 76 | dx1 = max(dx1, 0) 77 | dx2 = max(dx2, 0) 78 | if x == W-1: 79 | dx1 = min(dx1, 0) 80 | dx2 = min(dx2, 0) 81 | if y == 0: 82 | dy1 = max(dy1, 0) 83 | dy2 = max(dy2, 0) 84 | if y == H-1: 85 | dy1 = min(dy1, 0) 86 | dy2 = min(dy2, 0) 87 | if max(max(edge[y, x], edge[y+dy1, x+dx1]), edge[y+dy2, x+dx2]) != edge[y, x]: 88 | edge[y, x] = 0 89 | 90 | out = edge.astype(np.uint8) 91 | 92 | # Save result 93 | cv2.imwrite("out.jpg", out) 94 | cv2.imshow("result", out) 95 | cv2.waitKey(0) 96 | cv2.destroyAllWindows() 97 | -------------------------------------------------------------------------------- /Question_41_50/answers/answer_43.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_43.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_44.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_44.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_45.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_45.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_46.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_46.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_47.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_47.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_47.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | 37 | # Morphology - dilate 38 | Dil_time = 2 39 | MF = np.array(((0, 1, 0), 40 | (1, 0, 1), 41 | (0, 1, 0)), dtype=np.int) 42 | 43 | 44 | for i in range(Dil_time): 45 | tmp = np.pad(out, (1, 1), 'edge') 46 | for y in range(1, H+1): 47 | for x in range(1, W+1): 48 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 49 | out[y-1, x-1] = 255 50 | 51 | # Save result 52 | cv2.imwrite("out.jpg", out) 53 | cv2.imshow("result", out) 54 | cv2.waitKey(0) 55 | cv2.destroyAllWindows() 56 | -------------------------------------------------------------------------------- /Question_41_50/answers/answer_48.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_48.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_48.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | 37 | # Morphology filter 38 | MF = np.array(((0, 1, 0), 39 | (1, 0, 1), 40 | (0, 1, 0)), dtype=np.int) 41 | 42 | # Morphology - erode 43 | Erode_time = 2 44 | 45 | for i in range(Erode_time): 46 | tmp = np.pad(out, (1, 1), 'edge') 47 | for y in range(1, H+1): 48 | for x in range(1, W+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 50 | out[y-1, x-1] = 0 51 | 52 | 53 | # Save result 54 | cv2.imwrite("out.jpg", out) 55 | cv2.imshow("result", out) 56 | cv2.waitKey(0) 57 | cv2.destroyAllWindows() 58 | -------------------------------------------------------------------------------- /Question_41_50/answers/answer_49.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_49.jpg -------------------------------------------------------------------------------- /Question_41_50/answers/answer_49.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | # Morphology filter 37 | MF = np.array(((0, 1, 0), 38 | (1, 0, 1), 39 | (0, 1, 0)), dtype=np.int) 40 | 41 | # Morphology - erode 42 | Erode_time = 1 43 | 44 | for i in range(Erode_time): 45 | tmp = np.pad(out, (1, 1), 'edge') 46 | for y in range(1, H+1): 47 | for x in range(1, W+1): 48 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 49 | out[y-1, x-1] = 0 50 | 51 | # Morphology - dilate 52 | Dil_time = 1 53 | 54 | for i in range(Dil_time): 55 | tmp = np.pad(out, (1, 1), 'edge') 56 | for y in range(1, H+1): 57 | for x in range(1, W+1): 58 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 59 | out[y-1, x-1] = 255 60 | 61 | # Save result 62 | cv2.imwrite("out.jpg", out) 63 | cv2.imshow("result", out) 64 | cv2.waitKey(0) 65 | cv2.destroyAllWindows() 66 | -------------------------------------------------------------------------------- /Question_41_50/answers/answer_50.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/answers/answer_50.jpg -------------------------------------------------------------------------------- /Question_41_50/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/imori.jpg -------------------------------------------------------------------------------- /Question_41_50/thorino.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_41_50/thorino.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_4.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_51.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_51.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_51.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)[0]] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)[0]] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | # Morphology filter 37 | MF = np.array(((0, 1, 0), 38 | (1, 0, 1), 39 | (0, 1, 0)), dtype=np.int) 40 | 41 | # Morphology - erode 42 | Erode_time = 1 43 | erode = out.copy() 44 | 45 | for i in range(Erode_time): 46 | tmp = np.pad(out, (1, 1), 'edge') 47 | for y in range(1, H+1): 48 | for x in range(1, W+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 50 | erode[y-1, x-1] = 0 51 | 52 | # Morphology - dilate 53 | Dil_time = 1 54 | dilate = out.copy() 55 | 56 | for i in range(Dil_time): 57 | tmp = np.pad(out, (1, 1), 'edge') 58 | for y in range(1, H+1): 59 | for x in range(1, W+1): 60 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 61 | dilate[y-1, x-1] = 255 62 | 63 | out = np.abs(erode - dilate) * 255 64 | 65 | # Save result 66 | cv2.imwrite("out.jpg", out) 67 | cv2.imshow("result", out) 68 | cv2.waitKey(0) 69 | cv2.destroyAllWindows() 70 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_52.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_52.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_52.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | # Morphology filter 37 | MF = np.array(((0, 1, 0), 38 | (1, 0, 1), 39 | (0, 1, 0)), dtype=np.int) 40 | 41 | # Morphology - erode 42 | Erode_time = 3 43 | mor = out.copy() 44 | 45 | for i in range(Erode_time): 46 | tmp = np.pad(out, (1, 1), 'edge') 47 | for y in range(1, H+1): 48 | for x in range(1, W+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 50 | mor[y-1, x-1] = 0 51 | 52 | # Morphology - dilate 53 | Dil_time = 3 54 | 55 | for i in range(Dil_time): 56 | tmp = np.pad(mor, (1, 1), 'edge') 57 | for y in range(1, H+1): 58 | for x in range(1, W+1): 59 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 60 | mor[y-1, x-1] = 255 61 | 62 | out = out - mor 63 | 64 | # Save result 65 | cv2.imwrite("out.jpg", out) 66 | cv2.imshow("result", out) 67 | cv2.waitKey(0) 68 | cv2.destroyAllWindows() 69 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_53.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_53.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_53.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | # Morphology filter 37 | MF = np.array(((0, 1, 0), 38 | (1, 0, 1), 39 | (0, 1, 0)), dtype=np.int) 40 | 41 | # Morphology - dilate 42 | Dil_time = 3 43 | mor = out.copy() 44 | 45 | for i in range(Dil_time): 46 | tmp = np.pad(out, (1, 1), 'edge') 47 | for y in range(1, H+1): 48 | for x in range(1, W+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 50 | mor[y-1, x-1] = 255 51 | 52 | # Morphology - erode 53 | Erode_time = 3 54 | 55 | for i in range(Erode_time): 56 | tmp = np.pad(mor, (1, 1), 'edge') 57 | for y in range(1, H+1): 58 | for x in range(1, W+1): 59 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 60 | mor[y-1, x-1] = 0 61 | 62 | out = mor - out 63 | 64 | # Save result 65 | cv2.imwrite("out.jpg", out) 66 | cv2.imshow("result", out) 67 | cv2.waitKey(0) 68 | cv2.destroyAllWindows() 69 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_54.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_54.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_54.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Read templete image 10 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 11 | Ht, Wt, Ct = temp.shape 12 | 13 | 14 | # Templete matching 15 | i, j = -1, -1 16 | v = 255 * H * W * C 17 | for y in range(H-Ht): 18 | for x in range(W-Wt): 19 | _v = np.sum((img[y:y+Ht, x:x+Wt] - temp) ** 2) 20 | if _v < v: 21 | v = _v 22 | i, j = x, y 23 | 24 | out = img.copy() 25 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 26 | out = out.astype(np.uint8) 27 | 28 | # Save result 29 | cv2.imwrite("out.jpg", out) 30 | cv2.imshow("result", out) 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_55.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_55.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_55.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Read templete image 10 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 11 | Ht, Wt, Ct = temp.shape 12 | 13 | 14 | # Templete matching 15 | i, j = -1, -1 16 | v = 255 * H * W * C 17 | for y in range(H-Ht): 18 | for x in range(W-Wt): 19 | _v = np.sum(np.abs(img[y:y+Ht, x:x+Wt] - temp)) 20 | if _v < v: 21 | v = _v 22 | i, j = x, y 23 | 24 | out = img.copy() 25 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 26 | out = out.astype(np.uint8) 27 | 28 | # Save result 29 | cv2.imwrite("out.jpg", out) 30 | cv2.imshow("result", out) 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_56.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_56.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_56.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Read templete image 10 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 11 | Ht, Wt, Ct = temp.shape 12 | 13 | 14 | # Templete matching 15 | i, j = -1, -1 16 | v = -1 17 | for y in range(H-Ht): 18 | for x in range(W-Wt): 19 | _v = np.sum(img[y:y+Ht, x:x+Wt] * temp) 20 | _v /= (np.sqrt(np.sum(img[y:y+Ht, x:x+Wt]**2)) * np.sqrt(np.sum(temp**2))) 21 | if _v > v: 22 | v = _v 23 | i, j = x, y 24 | 25 | out = img.copy() 26 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 27 | out = out.astype(np.uint8) 28 | 29 | # Save result 30 | cv2.imwrite("out.jpg", out) 31 | cv2.imshow("result", out) 32 | cv2.waitKey(0) 33 | cv2.destroyAllWindows() 34 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_57.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_57.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_57.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | mi = np.mean(img) 10 | 11 | # Read templete image 12 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 13 | Ht, Wt, Ct = temp.shape 14 | 15 | mt = np.mean(temp) 16 | 17 | # Templete matching 18 | i, j = -1, -1 19 | v = -1 20 | for y in range(H-Ht): 21 | for x in range(W-Wt): 22 | _v = np.sum((img[y:y+Ht, x:x+Wt]-mi) * (temp-mt)) 23 | _v /= (np.sqrt(np.sum((img[y:y+Ht, x:x+Wt]-mi)**2)) * np.sqrt(np.sum((temp-mt)**2))) 24 | if _v > v: 25 | v = _v 26 | i, j = x, y 27 | 28 | out = img.copy() 29 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 30 | out = out.astype(np.uint8) 31 | 32 | # Save result 33 | cv2.imwrite("out.jpg", out) 34 | cv2.imshow("result", out) 35 | cv2.waitKey(0) 36 | cv2.destroyAllWindows() 37 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_58.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_58.png -------------------------------------------------------------------------------- /Question_51_60/answers/answer_58.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("seg.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | label = np.zeros((H, W), dtype=np.int) 10 | label[img[..., 0]>0] = 1 11 | 12 | LUT = [0 for _ in range(H*W)] 13 | 14 | n = 1 15 | 16 | for y in range(H): 17 | for x in range(W): 18 | if label[y, x] == 0: 19 | continue 20 | c3 = label[max(y-1,0), x] 21 | c5 = label[y, max(x-1,0)] 22 | if c3 < 2 and c5 < 2: 23 | n += 1 24 | label[y, x] = n 25 | else: 26 | _vs = [c3, c5] 27 | vs = [a for a in _vs if a > 1] 28 | v = min(vs) 29 | label[y, x] = v 30 | 31 | minv = v 32 | for _v in vs: 33 | if LUT[_v] != 0: 34 | minv = min(minv, LUT[_v]) 35 | for _v in vs: 36 | LUT[_v] = minv 37 | 38 | count = 1 39 | 40 | for l in range(2, n+1): 41 | flag = True 42 | for i in range(n+1): 43 | if LUT[i] == l: 44 | if flag: 45 | count += 1 46 | flag = False 47 | LUT[i] = count 48 | 49 | COLORS = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0]] 50 | out = np.zeros((H, W, C), dtype=np.uint8) 51 | 52 | for i, lut in enumerate(LUT[2:]): 53 | out[label == (i+2)] = COLORS[lut-2] 54 | 55 | # Save result 56 | cv2.imwrite("out.png", out) 57 | cv2.imshow("result", out) 58 | cv2.waitKey(0) 59 | cv2.destroyAllWindows() 60 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_59.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_59.png -------------------------------------------------------------------------------- /Question_51_60/answers/answer_59.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("seg.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | label = np.zeros((H, W), dtype=np.int) 10 | label[img[..., 0]>0] = 1 11 | 12 | LUT = [0 for _ in range(H*W)] 13 | 14 | n = 1 15 | 16 | for y in range(H): 17 | for x in range(W): 18 | if label[y, x] == 0: 19 | continue 20 | c2 = label[max(y-1,0), min(x+1, W-1)] 21 | c3 = label[max(y-1,0), x] 22 | c4 = label[max(y-1,0), max(x-1,0)] 23 | c5 = label[y, max(x-1,0)] 24 | if c3 < 2 and c5 < 2 and c2 < 2 and c4 < 2: 25 | n += 1 26 | label[y, x] = n 27 | else: 28 | _vs = [c3, c5, c2, c4] 29 | vs = [a for a in _vs if a > 1] 30 | v = min(vs) 31 | label[y, x] = v 32 | 33 | minv = v 34 | for _v in vs: 35 | if LUT[_v] != 0: 36 | minv = min(minv, LUT[_v]) 37 | for _v in vs: 38 | LUT[_v] = minv 39 | 40 | count = 1 41 | 42 | for l in range(2, n+1): 43 | flag = True 44 | for i in range(n+1): 45 | if LUT[i] == l: 46 | if flag: 47 | count += 1 48 | flag = False 49 | LUT[i] = count 50 | 51 | COLORS = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0]] 52 | out = np.zeros((H, W, C), dtype=np.uint8) 53 | 54 | for i, lut in enumerate(LUT[2:]): 55 | out[label == (i+2)] = COLORS[lut-2] 56 | 57 | # Save result 58 | cv2.imwrite("out.png", out) 59 | cv2.imshow("result", out) 60 | cv2.waitKey(0) 61 | cv2.destroyAllWindows() 62 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_60.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/answers/answer_60.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_60.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | img2 = cv2.imread("thorino.jpg").astype(np.float32) 10 | 11 | a = 0.6 12 | out = img * a + img2 * (1 - a) 13 | out = out.astype(np.uint8) 14 | 15 | # Save result 16 | cv2.imwrite("out.jpg", out) 17 | cv2.imshow("result", out) 18 | cv2.waitKey(0) 19 | cv2.destroyAllWindows() 20 | -------------------------------------------------------------------------------- /Question_51_60/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/imori.jpg -------------------------------------------------------------------------------- /Question_51_60/imori_part.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/imori_part.jpg -------------------------------------------------------------------------------- /Question_51_60/seg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/seg.png -------------------------------------------------------------------------------- /Question_51_60/thorino.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_51_60/thorino.jpg -------------------------------------------------------------------------------- /Question_61_70/answers/answer_61.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_61.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_61.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("renketsu.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | tmp = np.zeros((H, W), dtype=np.int) 10 | tmp[img[..., 0]>0] = 1 11 | 12 | out = np.zeros((H, W, 3), dtype=np.uint8) 13 | 14 | for y in range(H): 15 | for x in range(W): 16 | if tmp[y, x] < 1: 17 | continue 18 | 19 | c = 0 20 | c += (tmp[y,min(x+1,W-1)] - tmp[y,min(x+1,W-1)] * tmp[max(y-1,0),min(x+1,W-1)] * tmp[max(y-1,0),x]) 21 | c += (tmp[max(y-1,0),x] - tmp[max(y-1,0),x] * tmp[max(y-1,0),max(x-1,0)] * tmp[y,max(x-1,0)]) 22 | c += (tmp[y,max(x-1,0)] - tmp[y,max(x-1,0)] * tmp[min(y+1,H-1),max(x-1,0)] * tmp[min(y+1,H-1),x]) 23 | c += (tmp[min(y+1,H-1),x] - tmp[min(y+1,H-1),x] * tmp[min(y+1,H-1),min(x+1,W-1)] * tmp[y,min(x+1,W-1)]) 24 | 25 | if c == 0: 26 | out[y,x] = [0, 0, 255] 27 | elif c == 1: 28 | out[y,x] = [0, 255, 0] 29 | elif c == 2: 30 | out[y,x] = [255, 0, 0] 31 | elif c == 3: 32 | out[y,x] = [255, 255, 0] 33 | elif c == 4: 34 | out[y,x] = [255, 0, 255] 35 | 36 | out = out.astype(np.uint8) 37 | 38 | # Save result 39 | cv2.imwrite("out.png", out) 40 | cv2.imshow("result", out) 41 | cv2.waitKey(0) 42 | cv2.destroyAllWindows() 43 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_62.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_62.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_62.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("renketsu.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | _tmp = np.zeros((H, W), dtype=np.int) 10 | _tmp[img[..., 0]>0] = 1 11 | 12 | tmp = 1 - _tmp 13 | 14 | out = np.zeros((H, W, 3), dtype=np.uint8) 15 | 16 | for y in range(H): 17 | for x in range(W): 18 | if _tmp[y, x] < 1: 19 | continue 20 | 21 | c = 0 22 | c += (tmp[y,min(x+1,W-1)] - tmp[y,min(x+1,W-1)] * tmp[max(y-1,0),min(x+1,W-1)] * tmp[max(y-1,0),x]) 23 | c += (tmp[max(y-1,0),x] - tmp[max(y-1,0),x] * tmp[max(y-1,0),max(x-1,0)] * tmp[y,max(x-1,0)]) 24 | c += (tmp[y,max(x-1,0)] - tmp[y,max(x-1,0)] * tmp[min(y+1,H-1),max(x-1,0)] * tmp[min(y+1,H-1),x]) 25 | c += (tmp[min(y+1,H-1),x] - tmp[min(y+1,H-1),x] * tmp[min(y+1,H-1),min(x+1,W-1)] * tmp[y,min(x+1,W-1)]) 26 | 27 | if c == 0: 28 | out[y,x] = [0, 0, 255] 29 | elif c == 1: 30 | out[y,x] = [0, 255, 0] 31 | elif c == 2: 32 | out[y,x] = [255, 0, 0] 33 | elif c == 3: 34 | out[y,x] = [255, 255, 0] 35 | elif c == 4: 36 | out[y,x] = [255, 0, 255] 37 | 38 | out = out.astype(np.uint8) 39 | 40 | # Save result 41 | cv2.imwrite("out.png", out) 42 | cv2.imshow("result", out) 43 | cv2.waitKey(0) 44 | cv2.destroyAllWindows() 45 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_63.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_63.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_63.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("gazo.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | out = np.zeros((H, W), dtype=np.int) 10 | out[img[..., 0]>0] = 1 11 | 12 | count = 1 13 | while count > 0: 14 | count = 0 15 | tmp = out.copy() 16 | for y in range(H): 17 | for x in range(W): 18 | if out[y, x] < 1: 19 | continue 20 | 21 | judge = 0 22 | 23 | ## condition 1 24 | if (tmp[y,min(x+1,W-1)] + tmp[max(y-1,0), x] + tmp[y,max(x-1,0)] + tmp[min(y+1,H-1),x]) < 4: 25 | judge += 1 26 | 27 | ## condition 2 28 | c = 0 29 | c += (out[y,min(x+1,W-1)] - out[y,min(x+1,W-1)]*out[max(y-1,0),min(x+1,W-1)]*out[max(y-1,0),x]) 30 | c += (out[max(y-1,0),x] - out[max(y-1,0),x]*out[max(y-1,0),max(x-1,0)]*out[y,max(x-1,0)]) 31 | c += (out[y,max(x-1,0)] - out[y,max(x-1,0)]*out[min(y+1,H-1),max(x-1,0)]*out[min(y+1,H-1),x]) 32 | c += (out[min(y+1,H-1),x] - out[min(y+1,H-1),x]*out[min(y+1,H-1),min(x+1,W-1)]*out[y,min(x+1,W-1)]) 33 | if c == 1: 34 | judge += 1 35 | 36 | ##x condition 3 37 | if np.sum(out[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) >= 4: 38 | judge += 1 39 | 40 | if judge == 3: 41 | out[y,x] = 0 42 | count += 1 43 | 44 | out = out.astype(np.uint8) * 255 45 | 46 | # Save result 47 | cv2.imwrite("out.png", out) 48 | cv2.imshow("result", out) 49 | cv2.waitKey(0) 50 | cv2.destroyAllWindows() 51 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_64.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("gazo.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | out = np.zeros((H, W), dtype=np.int) 10 | out[img[..., 0]>0] = 1 11 | 12 | tmp = out.copy() 13 | _tmp = 1 - tmp 14 | 15 | count = 1 16 | while count > 0: 17 | count = 0 18 | tmp = out.copy() 19 | _tmp = 1 - tmp 20 | 21 | tmp2 = out.copy() 22 | _tmp2 = 1 - tmp2 23 | 24 | for y in range(H): 25 | for x in range(W): 26 | if out[y, x] < 1: 27 | continue 28 | 29 | judge = 0 30 | 31 | ## condition 1 32 | if (tmp[y,min(x+1,W-1)] * tmp[max(y-1,0), x] * tmp[y,max(x-1,0)] * tmp[min(y+1,H-1),x]) == 0: 33 | judge += 1 34 | 35 | ## condition 2 36 | c = 0 37 | c += (_tmp[y,min(x+1,W-1)] - _tmp[y,min(x+1,W-1)] * _tmp[max(y-1,0),min(x+1,W-1)] * _tmp[max(y-1,0),x]) 38 | c += (_tmp[max(y-1,0),x] - _tmp[max(y-1,0),x] * _tmp[max(y-1,0),max(x-1,0)] * _tmp[y,max(x-1,0)]) 39 | c += (_tmp[y,max(x-1,0)] - _tmp[y,max(x-1,0)] * _tmp[min(y+1,H-1),max(x-1,0)] * _tmp[min(y+1,H-1),x]) 40 | c += (_tmp[min(y+1,H-1),x] - _tmp[min(y+1,H-1),x] * _tmp[min(y+1,H-1),min(x+1,W-1)] * _tmp[y,min(x+1,W-1)]) 41 | if c == 1: 42 | judge += 1 43 | 44 | ## condition 3 45 | if np.sum(tmp[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) >= 3: 46 | judge += 1 47 | 48 | ## condition 4 49 | if np.sum(out[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) >= 2: 50 | judge += 1 51 | 52 | ## condition 5 53 | flag = False 54 | 55 | if (out[max(y-1,0), x] != tmp[max(y-1,0), x]) and (out[y, max(x-1,0)] != tmp[y, max(x-1,0)]): 56 | flag = True 57 | 58 | _tmp2 = 1-out 59 | 60 | c = 0 61 | c += (_tmp2[y,min(x+1,W-1)] - _tmp2[y,min(x+1,W-1)] * _tmp2[max(y-1,0),min(x+1,W-1)] * (1-tmp[max(y-1,0),x])) 62 | c += ((1-tmp[max(y-1,0),x]) - (1-tmp[max(y-1,0),x]) * _tmp2[max(y-1,0),max(x-1,0)] * _tmp2[y,max(x-1,0)]) 63 | c += (_tmp2[y,max(x-1,0)] - _tmp2[y,max(x-1,0)] * _tmp2[min(y+1,H-1),max(x-1,0)] * _tmp2[min(y+1,H-1),x]) 64 | c += (_tmp2[min(y+1,H-1),x] - _tmp2[min(y+1,H-1),x] * _tmp2[min(y+1,H-1),min(x+1,W-1)] * _tmp2[y,min(x+1,W-1)]) 65 | if c == 1: 66 | flag = True 67 | 68 | c = 0 69 | c += (_tmp2[y,min(x+1,W-1)] - _tmp2[y,min(x+1,W-1)] * _tmp2[max(y-1,0),min(x+1,W-1)] * _tmp2[max(y-1,0),x]) 70 | c += (_tmp2[max(y-1,0),x] - _tmp2[max(y-1,0),x] * _tmp2[max(y-1,0),max(x-1,0)] * (1-tmp[y,max(x-1,0)])) 71 | c += ((1-tmp[y,max(x-1,0)]) - (1-tmp[y,max(x-1,0)]) * _tmp2[min(y+1,H-1),max(x-1,0)] * _tmp2[min(y+1,H-1),x]) 72 | c += (_tmp2[min(y+1,H-1),x] - _tmp2[min(y+1,H-1),x] * _tmp2[min(y+1,H-1),min(x+1,W-1)] * _tmp2[y,min(x+1,W-1)]) 73 | if c == 1: 74 | flag = True 75 | 76 | if flag: 77 | judge += 1 78 | 79 | if judge >= 5: 80 | out[y,x] = 0 81 | count += 1 82 | 83 | out = out.astype(np.uint8) * 255 84 | 85 | # Save result 86 | cv2.imwrite("out.png", out) 87 | cv2.imshow("result", out) 88 | cv2.waitKey(0) 89 | cv2.destroyAllWindows() 90 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_65.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_65.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_66.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Magnitude and gradient 13 | gray = np.pad(gray, (1, 1), 'edge') 14 | 15 | gx = gray[1:H+1, 2:] - gray[1:H+1, :W] 16 | gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] 17 | gx[gx == 0] = 0.000001 18 | 19 | mag = np.sqrt(gx ** 2 + gy ** 2) 20 | gra = np.arctan(gy / gx) 21 | gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 22 | 23 | # Gradient histogram 24 | gra_n = np.zeros_like(gra, dtype=np.int) 25 | 26 | d = np.pi / 9 27 | for i in range(9): 28 | gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i 29 | 30 | # Draw 31 | _mag = (mag / mag.max() * 255).astype(np.uint8) 32 | 33 | cv2.imwrite("out_mag.jpg", _mag) 34 | 35 | # Save result 36 | out = np.zeros((H, W, 3), dtype=np.uint8) 37 | C = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 0, 255], [0, 255, 255], 38 | [127, 127, 0], [127, 0, 127], [0, 127, 127]] 39 | for i in range(9): 40 | out[gra_n == i] = C[i] 41 | 42 | cv2.imwrite("out_gra.jpg", out) 43 | cv2.imshow("result", out) 44 | cv2.waitKey(0) 45 | cv2.destroyAllWindows() 46 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_66_gra.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_66_gra.jpg -------------------------------------------------------------------------------- /Question_61_70/answers/answer_66_mag.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_66_mag.jpg -------------------------------------------------------------------------------- /Question_61_70/answers/answer_67.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_67.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_67.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Magnitude and gradient 13 | gray = np.pad(gray, (1, 1), 'edge') 14 | 15 | gx = gray[1:H+1, 2:] - gray[1:H+1, :W] 16 | gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] 17 | gx[gx == 0] = 0.000001 18 | 19 | mag = np.sqrt(gx ** 2 + gy ** 2) 20 | gra = np.arctan(gy / gx) 21 | gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 22 | 23 | # Gradient histogram 24 | gra_n = np.zeros_like(gra, dtype=np.int) 25 | 26 | d = np.pi / 9 27 | for i in range(9): 28 | gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i 29 | 30 | N = 8 31 | HH = H // N 32 | HW = W // N 33 | Hist = np.zeros((HH, HW, 9), dtype=np.float32) 34 | for y in range(HH): 35 | for x in range(HW): 36 | for j in range(N): 37 | for i in range(N): 38 | Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] 39 | 40 | 41 | for i in range(9): 42 | plt.subplot(3,3,i+1) 43 | plt.imshow(Hist[..., i]) 44 | plt.axis('off') 45 | plt.xticks(color="None") 46 | plt.yticks(color="None") 47 | plt.savefig("out.png") 48 | plt.show() 49 | 50 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_68.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_68.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_68.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Magnitude and gradient 13 | gray = np.pad(gray, (1, 1), 'edge') 14 | 15 | gx = gray[1:H+1, 2:] - gray[1:H+1, :W] 16 | gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] 17 | gx[gx == 0] = 0.000001 18 | 19 | mag = np.sqrt(gx ** 2 + gy ** 2) 20 | gra = np.arctan(gy / gx) 21 | gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 22 | 23 | # Gradient histogram 24 | gra_n = np.zeros_like(gra, dtype=np.int) 25 | 26 | d = np.pi / 9 27 | for i in range(9): 28 | gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i 29 | 30 | 31 | N = 8 32 | HH = H // N 33 | HW = W // N 34 | Hist = np.zeros((HH, HW, 9), dtype=np.float32) 35 | for y in range(HH): 36 | for x in range(HW): 37 | for j in range(N): 38 | for i in range(N): 39 | Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] 40 | 41 | ## Normalization 42 | C = 3 43 | eps = 1 44 | for y in range(HH): 45 | for x in range(HW): 46 | #for i in range(9): 47 | Hist[y, x] /= np.sqrt(np.sum(Hist[max(y-1,0):min(y+2, HH), max(x-1,0):min(x+2, HW)] ** 2) + eps) 48 | 49 | for i in range(9): 50 | plt.subplot(3,3,i+1) 51 | plt.imshow(Hist[..., i]) 52 | plt.axis('off') 53 | plt.xticks(color="None") 54 | plt.yticks(color="None") 55 | plt.savefig("out.png") 56 | plt.show() 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_69.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_69.jpg -------------------------------------------------------------------------------- /Question_61_70/answers/answer_69.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Magnitude and gradient 13 | gray = np.pad(gray, (1, 1), 'edge') 14 | 15 | gx = gray[1:H+1, 2:] - gray[1:H+1, :W] 16 | gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] 17 | gx[gx == 0] = 0.000001 18 | 19 | mag = np.sqrt(gx ** 2 + gy ** 2) 20 | gra = np.arctan(gy / gx) 21 | gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 22 | 23 | # Gradient histogram 24 | gra_n = np.zeros_like(gra, dtype=np.int) 25 | 26 | d = np.pi / 9 27 | for i in range(9): 28 | gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i 29 | 30 | 31 | N = 8 32 | HH = H // N 33 | HW = W // N 34 | Hist = np.zeros((HH, HW, 9), dtype=np.float32) 35 | for y in range(HH): 36 | for x in range(HW): 37 | for j in range(N): 38 | for i in range(N): 39 | Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] 40 | 41 | ## Normalization 42 | C = 3 43 | eps = 1 44 | for y in range(HH): 45 | for x in range(HW): 46 | #for i in range(9): 47 | Hist[y, x] /= np.sqrt(np.sum(Hist[max(y-1,0):min(y+2, HH), max(x-1,0):min(x+2, HW)] ** 2) + eps) 48 | 49 | ## Draw 50 | out = gray[1:H+1, 1:W+1].copy().astype(np.uint8) 51 | 52 | for y in range(HH): 53 | for x in range(HW): 54 | cx = x * N + N // 2 55 | cy = y * N + N // 2 56 | x1 = cx + N // 2 - 1 57 | y1 = cy 58 | x2 = cx - N // 2 + 1 59 | y2 = cy 60 | 61 | h = Hist[y, x] / np.sum(Hist[y, x]) 62 | h /= h.max() 63 | 64 | for c in range(9): 65 | #angle = (20 * c + 10 - 90) / 180. * np.pi 66 | angle = (20 * c + 10) / 180. * np.pi 67 | rx = int(np.sin(angle) * (x1 - cx) + np.cos(angle) * (y1 - cy) + cx) 68 | ry = int(np.cos(angle) * (x1 - cx) - np.cos(angle) * (y1 - cy) + cy) 69 | lx = int(np.sin(angle) * (x2 - cx) + np.cos(angle) * (y2 - cy) + cx) 70 | ly = int(np.cos(angle) * (x2 - cx) - np.cos(angle) * (y2 - cy) + cy) 71 | 72 | c = int(255. * h[c]) 73 | cv2.line(out, (lx, ly), (rx, ry), (c, c, c), thickness=1) 74 | 75 | # Save result 76 | cv2.imwrite("out.jpg", out) 77 | cv2.imshow("result", out) 78 | cv2.waitKey(0) 79 | cv2.destroyAllWindows() 80 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/answers/answer_70.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_70.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) / 255. 7 | 8 | # RGB > HSV 9 | out = np.zeros_like(img) 10 | 11 | max_v = np.max(img, axis=2).copy() 12 | min_v = np.min(img, axis=2).copy() 13 | min_arg = np.argmin(img, axis=2) 14 | 15 | H = np.zeros_like(max_v) 16 | 17 | H[np.where(max_v == min_v)] = 0 18 | ## if min == B 19 | ind = np.where(min_arg == 0) 20 | H[ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 21 | ## if min == R 22 | ind = np.where(min_arg == 2) 23 | H[ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 24 | ## if min == G 25 | ind = np.where(min_arg == 1) 26 | H[ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 27 | 28 | V = max_v.copy() 29 | S = max_v.copy() - min_v.copy() 30 | 31 | # color tracking 32 | mask = np.zeros_like(H) 33 | mask[np.where((H>180) & (H<260))] = 255 34 | 35 | out = mask.astype(np.uint8) 36 | 37 | # Save result 38 | cv2.imwrite("out.png", out) 39 | cv2.imshow("result", out) 40 | cv2.waitKey(0) 41 | cv2.destroyAllWindows() 42 | -------------------------------------------------------------------------------- /Question_61_70/gazo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/gazo.png -------------------------------------------------------------------------------- /Question_61_70/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/imori.jpg -------------------------------------------------------------------------------- /Question_61_70/jieshi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/jieshi.png -------------------------------------------------------------------------------- /Question_61_70/renketsu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_61_70/renketsu.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_70.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_71.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_71.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_71.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) / 255. 7 | 8 | # RGB > HSV 9 | 10 | max_v = np.max(img, axis=2).copy() 11 | min_v = np.min(img, axis=2).copy() 12 | min_arg = np.argmin(img, axis=2) 13 | 14 | H = np.zeros_like(max_v) 15 | 16 | H[np.where(max_v == min_v)] = 0 17 | ## if min == B 18 | ind = np.where(min_arg == 0) 19 | H[ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 20 | ## if min == R 21 | ind = np.where(min_arg == 2) 22 | H[ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 23 | ## if min == G 24 | ind = np.where(min_arg == 1) 25 | H[ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 26 | 27 | V = max_v.copy() 28 | S = max_v.copy() - min_v.copy() 29 | 30 | # color tracking 31 | mask = np.zeros_like(H) 32 | mask[np.where((H>180) & (H<260))] = 1 33 | 34 | # masking 35 | mask = 1 - mask 36 | out = img.copy() * 255. 37 | 38 | for c in range(3): 39 | out[..., c] *= mask 40 | 41 | out = out.astype(np.uint8) 42 | 43 | # Save result 44 | cv2.imwrite("out.jpg", out) 45 | cv2.imshow("result", out) 46 | cv2.waitKey(0) 47 | cv2.destroyAllWindows() 48 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_72.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_72.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_72.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) / 255. 7 | 8 | # RGB > HSV 9 | 10 | max_v = np.max(img, axis=2).copy() 11 | min_v = np.min(img, axis=2).copy() 12 | min_arg = np.argmin(img, axis=2) 13 | 14 | H = np.zeros_like(max_v) 15 | 16 | H[np.where(max_v == min_v)] = 0 17 | ## if min == B 18 | ind = np.where(min_arg == 0) 19 | H[ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 20 | ## if min == R 21 | ind = np.where(min_arg == 2) 22 | H[ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 23 | ## if min == G 24 | ind = np.where(min_arg == 1) 25 | H[ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 26 | 27 | V = max_v.copy() 28 | S = max_v.copy() - min_v.copy() 29 | 30 | # color tracking 31 | mask = np.zeros_like(H) 32 | mask[np.where((H>180) & (H<260))] = 255 33 | 34 | h, w, _ = img.shape 35 | 36 | # Closing 37 | ## Morphology filter 38 | MF = np.array(((0, 1, 0), 39 | (1, 0, 1), 40 | (0, 1, 0)), dtype=np.int) 41 | 42 | ## Morphology - dilate 43 | Dil_time = 5 44 | 45 | for i in range(Dil_time): 46 | tmp = np.pad(mask, (1, 1), 'edge') 47 | for y in range(1, h+1): 48 | for x in range(1, w+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 50 | mask[y-1, x-1] = 255 51 | ## Morphology - erode 52 | Erode_time = 5 53 | 54 | for i in range(Erode_time): 55 | tmp = np.pad(mask, (1, 1), 'edge') 56 | for y in range(1, h+1): 57 | for x in range(1, w+1): 58 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 59 | mask[y-1, x-1] = 0 60 | 61 | # Opening 62 | ## Morphology - erode 63 | Erode_time = 5 64 | 65 | for i in range(Erode_time): 66 | tmp = np.pad(mask, (1, 1), 'edge') 67 | for y in range(1, h+1): 68 | for x in range(1, w+1): 69 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 70 | mask[y-1, x-1] = 0 71 | 72 | ## Morphology - dilate 73 | Dil_time = 5 74 | 75 | for i in range(Dil_time): 76 | tmp = np.pad(mask, (1, 1), 'edge') 77 | for y in range(1, h+1): 78 | for x in range(1, w+1): 79 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 80 | mask[y-1, x-1] = 255 81 | 82 | # masking 83 | cv2.imwrite("out_mask.png", mask.astype(np.uint8)) 84 | 85 | mask = 1 - mask / 255 86 | out = img.copy() * 255. 87 | 88 | for c in range(3): 89 | out[..., c] *= mask 90 | 91 | out = out.astype(np.uint8) 92 | 93 | # Save result 94 | cv2.imwrite("out.jpg", out) 95 | cv2.imshow("result", out) 96 | cv2.waitKey(0) 97 | cv2.destroyAllWindows() 98 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_72_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_72_mask.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_73.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_73.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_73.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | def resize(img, a): 13 | _h, _w = img.shape 14 | h = int(a * _h) 15 | w = int(a * _w) 16 | y = np.arange(h).repeat(w).reshape(w, -1) 17 | x = np.tile(np.arange(w), (h, 1)) 18 | y = (y / a) 19 | x = (x / a) 20 | 21 | ix = np.floor(x).astype(np.int) 22 | iy = np.floor(y).astype(np.int) 23 | ix = np.minimum(ix, _w-2) 24 | iy = np.minimum(iy, _w-2) 25 | 26 | dx = x - ix 27 | dy = y - iy 28 | 29 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 30 | out[out>255] = 255 31 | 32 | return out 33 | 34 | p = resize(gray, 0.5) 35 | p = resize(p, 2.) 36 | 37 | out = p.astype(np.uint8) 38 | 39 | # Save result 40 | cv2.imshow("result", out) 41 | cv2.waitKey(0) 42 | cv2.imwrite("out.jpg", out) 43 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_74.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_74.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_74.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | def resize(img, a): 13 | _h, _w = img.shape 14 | h = int(a * _h) 15 | w = int(a * _w) 16 | 17 | y = np.arange(h).repeat(w).reshape(w, -1) 18 | x = np.tile(np.arange(w), (h, 1)) 19 | y = (y / a) 20 | x = (x / a) 21 | 22 | ix = np.floor(x).astype(np.int) 23 | iy = np.floor(y).astype(np.int) 24 | ix = np.minimum(ix, _w-2) 25 | iy = np.minimum(iy, _w-2) 26 | 27 | dx = x - ix 28 | dy = y - iy 29 | 30 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 31 | out[out>255] = 255 32 | 33 | return out 34 | 35 | p = resize(gray, 0.5) 36 | p = resize(p, 2.) 37 | 38 | out = np.abs(gray - p) 39 | 40 | out = out / out.max() * 255 41 | 42 | out = out.astype(np.uint8) 43 | 44 | # Save result 45 | cv2.imshow("result", out) 46 | cv2.waitKey(0) 47 | cv2.imwrite("out.jpg", out) 48 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | def resize(img, a): 13 | _h, _w = img.shape 14 | h = int(a * _h) 15 | w = int(a * _w) 16 | """ 17 | y = np.arange(h).repeat(w).reshape(w, -1) 18 | x = np.tile(np.arange(w), (h, 1)) 19 | y = np.floor(y / a).astype(np.int) 20 | x = np.floor(x / a).astype(np.int) 21 | y = np.minimum(y, _h-1) 22 | x = np.minimum(x, _w-1) 23 | out = img[y,x] 24 | """ 25 | y = np.arange(h).repeat(w).reshape(w, -1) 26 | x = np.tile(np.arange(w), (h, 1)) 27 | y = (y / a) 28 | x = (x / a) 29 | 30 | ix = np.floor(x).astype(np.int) 31 | iy = np.floor(y).astype(np.int) 32 | ix = np.minimum(ix, _w-2) 33 | iy = np.minimum(iy, _w-2) 34 | 35 | dx = x - ix 36 | dy = y - iy 37 | #dx = np.repeat(np.expand_dims(dx, axis=-1), 3, axis=-1) 38 | #dy = np.repeat(np.expand_dims(dy, axis=-1), 3, axis=-1) 39 | 40 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 41 | out[out>255] = 255 42 | 43 | return out 44 | 45 | pyramid = [gray] 46 | for i in range(1, 6): 47 | a = 2. ** i 48 | p = resize(gray, 1. / a) 49 | pyramid.append(p) 50 | 51 | for i in range(6): 52 | cv2.imwrite("out_{}.jpg".format(2**i), pyramid[i].astype(np.uint8)) 53 | plt.subplot(1, 6, i+1) 54 | plt.imshow(pyramid[i], cmap='gray') 55 | plt.axis('off') 56 | plt.xticks(color="None") 57 | plt.yticks(color="None") 58 | 59 | plt.show() 60 | 61 | 62 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_75_1.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_75_16.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_75_2.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_32.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_75_32.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_75_4.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_75_8.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_76.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_76.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_76.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | def resize(img, a): 13 | _h, _w = img.shape 14 | h = int(a * _h) 15 | w = int(a * _w) 16 | """ 17 | y = np.arange(h).repeat(w).reshape(w, -1) 18 | x = np.tile(np.arange(w), (h, 1)) 19 | y = np.floor(y / a).astype(np.int) 20 | x = np.floor(x / a).astype(np.int) 21 | y = np.minimum(y, _h-1) 22 | x = np.minimum(x, _w-1) 23 | out = img[y,x] 24 | """ 25 | y = np.arange(h).repeat(w).reshape(w, -1) 26 | x = np.tile(np.arange(w), (h, 1)) 27 | y = (y / a) 28 | x = (x / a) 29 | 30 | ix = np.floor(x).astype(np.int) 31 | iy = np.floor(y).astype(np.int) 32 | ix = np.minimum(ix, _w-2) 33 | iy = np.minimum(iy, _w-2) 34 | 35 | dx = x - ix 36 | dy = y - iy 37 | #dx = np.repeat(np.expand_dims(dx, axis=-1), 3, axis=-1) 38 | #dy = np.repeat(np.expand_dims(dy, axis=-1), 3, axis=-1) 39 | 40 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 41 | out[out>255] = 255 42 | 43 | return out 44 | 45 | pyramid = [gray] 46 | for i in range(1, 6): 47 | a = 2. ** i 48 | p = resize(gray, 1. / a) 49 | p = resize(p, a) 50 | pyramid.append(p) 51 | 52 | out = np.zeros((H, W), dtype=np.float32) 53 | 54 | out += np.abs(pyramid[0] - pyramid[1]) 55 | out += np.abs(pyramid[0] - pyramid[3]) 56 | out += np.abs(pyramid[0] - pyramid[5]) 57 | out += np.abs(pyramid[1] - pyramid[4]) 58 | out += np.abs(pyramid[2] - pyramid[3]) 59 | out += np.abs(pyramid[3] - pyramid[5]) 60 | 61 | 62 | out = out / out.max() * 255 63 | 64 | out = out.astype(np.uint8) 65 | 66 | # Save result 67 | cv2.imshow("result", out) 68 | cv2.waitKey(0) 69 | cv2.imwrite("out.jpg", out) 70 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_77.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_77.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_77.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Gabor 6 | K_size = 111 7 | Sigma = 10 8 | Gamma = 1.2 9 | Lambda = 10. 10 | Psi = 0. 11 | angle = 0 12 | 13 | d = K_size // 2 14 | 15 | gabor = np.zeros((K_size, K_size), dtype=np.float32) 16 | 17 | for y in range(K_size): 18 | for x in range(K_size): 19 | px = x - d 20 | py = y - d 21 | theta = angle / 180. * np.pi 22 | _x = np.cos(theta) * px + np.sin(theta) * py 23 | _y = -np.sin(theta) * px + np.cos(theta) * py 24 | gabor[x, y] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) 25 | 26 | gabor /= np.sum(np.abs(gabor)) 27 | 28 | # Visualize 29 | out = gabor - np.min(gabor) 30 | out /= np.max(out) 31 | out *= 255 32 | out = out.astype(np.uint8) 33 | cv2.imwrite("out.jpg", out) 34 | cv2.imshow("result", out) 35 | cv2.waitKey(0) 36 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_78.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_78.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_78.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Gabor 7 | 8 | def gabor_f(k=111, s=10, g=1.2, l=10, p=0, A=0): 9 | d = k // 2 10 | 11 | gabor = np.zeros((k, k), dtype=np.float32) 12 | 13 | for y in range(k): 14 | for x in range(k): 15 | px = x - d 16 | py = y - d 17 | theta = A / 180. * np.pi 18 | _x = np.cos(theta) * px + np.sin(theta) * py 19 | _y = -np.sin(theta) * px + np.cos(theta) * py 20 | gabor[x, y] = np.exp(-(_x**2 + g**2 * _y**2) / (2 * s**2)) * np.cos(2*np.pi*_x/l + p) 21 | 22 | gabor /= np.sum(np.abs(gabor)) 23 | 24 | return gabor 25 | 26 | As = [0, 45, 90, 135] 27 | 28 | plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) 29 | 30 | for i, A in enumerate(As): 31 | gabor = gabor_f(A=A) 32 | out = gabor - np.min(gabor) 33 | out /= np.max(out) 34 | out *= 255 35 | out = out.astype(np.uint8) 36 | plt.subplot(1, 4, i+1) 37 | plt.imshow(out, cmap='gray') 38 | plt.axis('off') 39 | plt.title("Angle "+str(A)) 40 | 41 | plt.savefig("out.png") 42 | plt.show() 43 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_79.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_79.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_79.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | gray = gray.astype(np.uint8) 13 | 14 | # Gabor 15 | def gabor_f(k=111, s=10, g=1.2, l=10, p=0, A=0): 16 | d = k // 2 17 | 18 | gabor = np.zeros((k, k), dtype=np.float32) 19 | 20 | for y in range(k): 21 | for x in range(k): 22 | px = x - d 23 | py = y - d 24 | theta = A / 180. * np.pi 25 | _x = np.cos(theta) * px + np.sin(theta) * py 26 | _y = -np.sin(theta) * px + np.cos(theta) * py 27 | gabor[x, y] = np.exp(-(_x**2 + g**2 * _y**2) / (2 * s**2)) * np.cos(2*np.pi*_x/l + p) 28 | 29 | gabor /= np.sum(np.abs(gabor)) 30 | 31 | return gabor 32 | 33 | K_size = 11 34 | Sigma = 1.5 35 | Gamma = 1.2 36 | Lambda = 3. 37 | Psi = 0. 38 | 39 | gray = np.pad(gray, (K_size//2, K_size//2), 'edge') 40 | 41 | As = [0, 45, 90, 135] 42 | 43 | plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) 44 | 45 | for i, A in enumerate(As): 46 | gabor = gabor_f(k=K_size, s=Sigma, g=Gamma, l=Lambda, p=Psi, A=A) 47 | 48 | out = np.zeros((H, W), dtype=np.float32) 49 | 50 | for y in range(H): 51 | for x in range(W): 52 | out[y, x] = np.sum(gray[y:y+K_size, x:x+K_size] * gabor) 53 | 54 | out[out < 0] = 0 55 | out[out > 255] = 255 56 | 57 | plt.subplot(1, 4, i+1) 58 | plt.imshow(out, cmap='gray') 59 | plt.axis('off') 60 | plt.title("Angle "+str(A)) 61 | 62 | plt.savefig("out.png") 63 | plt.show() 64 | 65 | 66 | 67 | out = out.astype(np.uint8) 68 | 69 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_80.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/answers/answer_80.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_80.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | gray = gray.astype(np.uint8) 13 | 14 | # Gabor 15 | def gabor_f(k=111, s=10, g=1.2, l=10, p=0, A=0): 16 | d = k // 2 17 | 18 | gabor = np.zeros((k, k), dtype=np.float32) 19 | 20 | for y in range(k): 21 | for x in range(k): 22 | px = x - d 23 | py = y - d 24 | theta = A / 180. * np.pi 25 | _x = np.cos(theta) * px + np.sin(theta) * py 26 | _y = -np.sin(theta) * px + np.cos(theta) * py 27 | gabor[x, y] = np.exp(-(_x**2 + g**2 * _y**2) / (2 * s**2)) * np.cos(2*np.pi*_x/l + p) 28 | 29 | gabor /= np.sum(np.abs(gabor)) 30 | 31 | return gabor 32 | 33 | K_size = 11 34 | Sigma = 1.5 35 | Gamma = 1.2 36 | Lambda = 3. 37 | Psi = 0. 38 | 39 | gray = np.pad(gray, (K_size//2, K_size//2), 'edge') 40 | 41 | As = [0, 45, 90, 135] 42 | 43 | gs = [] 44 | 45 | plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) 46 | 47 | for i, A in enumerate(As): 48 | gabor = gabor_f(k=K_size, s=Sigma, g=Gamma, l=Lambda, p=Psi, A=A) 49 | 50 | out = np.zeros((H, W), dtype=np.float32) 51 | 52 | for y in range(H): 53 | for x in range(W): 54 | out[y, x] = np.sum(gray[y:y+K_size, x:x+K_size] * gabor) 55 | 56 | out[out < 0] = 0 57 | out[out > 255] = 255 58 | 59 | gs.append(out) 60 | 61 | 62 | out = np.zeros((H, W), dtype=np.float32) 63 | for g in gs: 64 | out += g 65 | 66 | 67 | out = out / out.max() * 255 68 | out = out.astype(np.uint8) 69 | 70 | cv2.imwrite("out.jpg", out) 71 | cv2.imshow("result", out) 72 | cv2.waitKey(0) 73 | 74 | -------------------------------------------------------------------------------- /Question_71_80/gazo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/gazo.png -------------------------------------------------------------------------------- /Question_71_80/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_71_80/imori.jpg -------------------------------------------------------------------------------- /Question_81_90/answers/answer_81.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/answers/answer_81.jpg -------------------------------------------------------------------------------- /Question_81_90/answers/answer_81.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("thorino.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | ## Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | gray = gray.astype(np.uint8) 12 | 13 | ## Sobel 14 | sobely = np.array(((1, 2, 1), 15 | (0, 0, 0), 16 | (-1, -2, -1)), dtype=np.float32) 17 | 18 | sobelx = np.array(((1, 0, -1), 19 | (2, 0, -2), 20 | (1, 0, -1)), dtype=np.float32) 21 | 22 | tmp = np.pad(gray, (1, 1), 'edge') 23 | 24 | Ix = np.zeros_like(gray, dtype=np.float32) 25 | Iy = np.zeros_like(gray, dtype=np.float32) 26 | 27 | for y in range(H): 28 | for x in range(W): 29 | Ix[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobelx) 30 | Iy[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobely) 31 | 32 | Ix2 = Ix ** 2 33 | IxIy = Ix * Iy 34 | Iy2 = Iy ** 2 35 | 36 | out = np.array((gray, gray, gray)) 37 | out = np.transpose(out, (1,2,0)) 38 | 39 | ## Hessian 40 | Hes = np.zeros((H, W)) 41 | 42 | for y in range(H): 43 | for x in range(W): 44 | Hes[y,x] = Ix2[y,x] * Iy2[y,x] - IxIy[y,x] ** 2 45 | 46 | ## Detect Corner 47 | for y in range(H): 48 | for x in range(W): 49 | if Hes[y,x] == np.max(Hes[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) and Hes[y,x] > np.max(Hes)*0.1: 50 | out[y, x] = [0, 0, 255] 51 | 52 | out = out.astype(np.uint8) 53 | 54 | cv2.imwrite("out.jpg", out) 55 | cv2.imshow("result", out) 56 | cv2.waitKey(0) 57 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_82.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/answers/answer_82.png -------------------------------------------------------------------------------- /Question_81_90/answers/answer_82.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("thorino.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | ## Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Harris 13 | 14 | ## Sobel 15 | sobely = np.array(((1, 2, 1), 16 | (0, 0, 0), 17 | (-1, -2, -1)), dtype=np.float32) 18 | 19 | sobelx = np.array(((1, 0, -1), 20 | (2, 0, -2), 21 | (1, 0, -1)), dtype=np.float32) 22 | 23 | tmp = np.pad(gray, (1, 1), 'edge') 24 | 25 | Ix = np.zeros_like(gray, dtype=np.float32) 26 | Iy = np.zeros_like(gray, dtype=np.float32) 27 | 28 | for y in range(H): 29 | for x in range(W): 30 | Ix[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobelx) 31 | Iy[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobely) 32 | 33 | Ix2 = Ix ** 2 34 | Iy2 = Iy ** 2 35 | Ixy = Ix * Iy 36 | 37 | ## gaussian 38 | K_size = 3 39 | sigma = 3 40 | Ix2_t = np.pad(Ix2, (K_size // 2, K_size // 2), 'edge') 41 | Iy2_t = np.pad(Iy2, (K_size // 2, K_size // 2), 'edge') 42 | Ixy_t = np.pad(Ixy, (K_size // 2, K_size // 2), 'edge') 43 | 44 | K = np.zeros((K_size, K_size), dtype=np.float) 45 | for x in range(K_size): 46 | for y in range(K_size): 47 | _x = x - K_size // 2 48 | _y = y - K_size // 2 49 | K[y, x] = np.exp( -(_x**2 + _y**2) / (2 * (sigma**2))) 50 | K /= (sigma * np.sqrt(2 * np.pi)) 51 | K /= K.sum() 52 | 53 | for y in range(H): 54 | for x in range(W): 55 | Ix2[y,x] = np.sum(Ix2_t[y:y+K_size, x:x+K_size] * K) 56 | Iy2[y,x] = np.sum(Iy2_t[y:y+K_size, x:x+K_size] * K) 57 | Ixy[y,x] = np.sum(Ixy_t[y:y+K_size, x:x+K_size] * K) 58 | 59 | out = np.array((gray, gray, gray)) 60 | out = np.transpose(out, (1,2,0)) 61 | 62 | plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) 63 | 64 | plt.subplot(1,3,1) 65 | plt.imshow(Ix2, cmap='gray') 66 | plt.title("Ix^2") 67 | plt.axis("off") 68 | 69 | plt.subplot(1,3,2) 70 | plt.imshow(Iy2, cmap='gray') 71 | plt.title("Iy^2") 72 | plt.axis("off") 73 | 74 | plt.subplot(1,3,3) 75 | plt.imshow(Ixy, cmap='gray') 76 | plt.title("Ixy") 77 | plt.axis("off") 78 | 79 | plt.savefig("out.png") 80 | plt.show() 81 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_83.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/answers/answer_83.jpg -------------------------------------------------------------------------------- /Question_81_90/answers/answer_83.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("thorino.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | ## Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | # Harris 13 | 14 | ## Sobel 15 | sobely = np.array(((1, 2, 1), 16 | (0, 0, 0), 17 | (-1, -2, -1)), dtype=np.float32) 18 | 19 | sobelx = np.array(((1, 0, -1), 20 | (2, 0, -2), 21 | (1, 0, -1)), dtype=np.float32) 22 | 23 | tmp = np.pad(gray, (1, 1), 'edge') 24 | 25 | Ix = np.zeros_like(gray, dtype=np.float32) 26 | Iy = np.zeros_like(gray, dtype=np.float32) 27 | 28 | for y in range(H): 29 | for x in range(W): 30 | Ix[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobelx) 31 | Iy[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobely) 32 | 33 | Ix2 = Ix ** 2 34 | Iy2 = Iy ** 2 35 | Ixy = Ix * Iy 36 | 37 | ## gaussian 38 | K_size = 3 39 | sigma = 3 40 | Ix2_t = np.pad(Ix2, (K_size // 2, K_size // 2), 'edge') 41 | Iy2_t = np.pad(Iy2, (K_size // 2, K_size // 2), 'edge') 42 | Ixy_t = np.pad(Ixy, (K_size // 2, K_size // 2), 'edge') 43 | 44 | K = np.zeros((K_size, K_size), dtype=np.float) 45 | for x in range(K_size): 46 | for y in range(K_size): 47 | _x = x - K_size // 2 48 | _y = y - K_size // 2 49 | K[y, x] = np.exp( -(_x**2 + _y**2) / (2 * (sigma**2))) 50 | K /= (sigma * np.sqrt(2 * np.pi)) 51 | K /= K.sum() 52 | 53 | for y in range(H): 54 | for x in range(W): 55 | Ix2[y,x] = np.sum(Ix2_t[y:y+K_size, x:x+K_size] * K) 56 | Iy2[y,x] = np.sum(Iy2_t[y:y+K_size, x:x+K_size] * K) 57 | Ixy[y,x] = np.sum(Ixy_t[y:y+K_size, x:x+K_size] * K) 58 | 59 | out = np.array((gray, gray, gray)) 60 | out = np.transpose(out, (1,2,0)) 61 | 62 | ## select corner 63 | k = 0.04 64 | M = (Ix2 * Iy2 - Ixy ** 2) - k * ((Ix2 + Iy2) ** 2) 65 | 66 | th = 0.1 67 | out[M >= np.max(M) * th] = [0, 0, 255] 68 | 69 | out = out.astype(np.uint8) 70 | 71 | cv2.imwrite("out.jpg", out) 72 | cv2.imshow("result", out) 73 | cv2.waitKey(0) 74 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_84.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/answers/answer_84.png -------------------------------------------------------------------------------- /Question_81_90/answers/answer_84.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | train = glob("dataset/train_*") 14 | train.sort() 15 | 16 | db = np.zeros((len(train), 13), dtype=np.int32) 17 | 18 | for i, path in enumerate(train): 19 | img = dic_color(cv2.imread(path)) 20 | ## histogram 21 | for j in range(4): 22 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 23 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 24 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 25 | 26 | ## class 27 | if 'akahara' in path: 28 | cls = 0 29 | elif 'madara' in path: 30 | cls = 1 31 | db[i, -1] = cls 32 | 33 | img_h = img.copy() // 64 34 | img_h[..., 1] += 4 35 | img_h[..., 2] += 8 36 | plt.subplot(2, 5, i+1) 37 | plt.hist(img_h.ravel(), bins=12, rwidth=0.8) 38 | plt.title(path) 39 | 40 | print(db) 41 | plt.show() 42 | 43 | 44 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_85.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | train = glob("dataset/train_*") 14 | train.sort() 15 | 16 | db = np.zeros((len(train), 13), dtype=np.int32) 17 | pdb = [] 18 | 19 | for i, path in enumerate(train): 20 | img = dic_color(cv2.imread(path)) 21 | ## histogram 22 | for j in range(4): 23 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 24 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 25 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 26 | 27 | ## class 28 | if 'akahara' in path: 29 | cls = 0 30 | elif 'madara' in path: 31 | cls = 1 32 | db[i, -1] = cls 33 | pdb.append(path) 34 | 35 | ## test 36 | test = glob("dataset/test_*") 37 | test.sort() 38 | 39 | success_num = 0. 40 | 41 | for path in test: 42 | img = dic_color(cv2.imread(path)) 43 | 44 | hist = np.zeros(12, dtype=np.int32) 45 | for j in range(4): 46 | hist[j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 47 | hist[j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 48 | hist[j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 49 | 50 | ## compute difference 51 | difs = np.abs(db[:, :12] - hist) 52 | difs = np.sum(difs, axis=1) 53 | pred_i = np.argmin(difs) 54 | pred = db[pred_i, -1] 55 | 56 | if pred == 0: 57 | pl = "akahara" 58 | elif pred == 1: 59 | pl = "madara" 60 | 61 | print(path, "is similar >>", pdb[pred_i], " Pred >>", pl) 62 | 63 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_86.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | train = glob("dataset/train_*") 14 | train.sort() 15 | 16 | db = np.zeros((len(train), 13), dtype=np.int32) 17 | pdb = [] 18 | 19 | for i, path in enumerate(train): 20 | img = dic_color(cv2.imread(path)) 21 | ## histogram 22 | for j in range(4): 23 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 24 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 25 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 26 | 27 | ## class 28 | if 'akahara' in path: 29 | cls = 0 30 | elif 'madara' in path: 31 | cls = 1 32 | db[i, -1] = cls 33 | pdb.append(path) 34 | 35 | ## test 36 | test = glob("dataset/test_*") 37 | test.sort() 38 | 39 | success_num = 0. 40 | 41 | for path in test: 42 | img = dic_color(cv2.imread(path)) 43 | 44 | hist = np.zeros(12, dtype=np.int32) 45 | for j in range(4): 46 | hist[j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 47 | hist[j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 48 | hist[j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 49 | 50 | ## compute difference 51 | difs = np.abs(db[:, :12] - hist) 52 | difs = np.sum(difs, axis=1) 53 | pred_i = np.argmin(difs) 54 | pred = db[pred_i, -1] 55 | 56 | if pred == 0: 57 | pl = "akahara" 58 | elif pred == 1: 59 | pl = "madara" 60 | 61 | print(path, "is similar >>", pdb[pred_i], " Pred >>", pl) 62 | 63 | ## Count success 64 | gt = "akahara" if "akahara" in path else "madara" 65 | if gt == pl: 66 | success_num += 1. 67 | 68 | accuracy = success_num / len(test) 69 | print("Accuracy >>", accuracy, "({}/{})".format(int(success_num), len(test))) 70 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_87.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | train = glob("dataset/train_*") 14 | train.sort() 15 | 16 | db = np.zeros((len(train), 13), dtype=np.int32) 17 | pdb = [] 18 | 19 | for i, path in enumerate(train): 20 | img = dic_color(cv2.imread(path)) 21 | ## histogram 22 | for j in range(4): 23 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 24 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 25 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 26 | 27 | ## class 28 | if 'akahara' in path: 29 | cls = 0 30 | elif 'madara' in path: 31 | cls = 1 32 | db[i, -1] = cls 33 | pdb.append(path) 34 | 35 | ## test 36 | test = glob("dataset/test_*") 37 | test.sort() 38 | 39 | success_num = 0. 40 | 41 | for path in test: 42 | img = dic_color(cv2.imread(path)) 43 | 44 | hist = np.zeros(12, dtype=np.int32) 45 | for j in range(4): 46 | hist[j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 47 | hist[j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 48 | hist[j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 49 | 50 | ## compute difference 51 | difs = np.abs(db[:, :12] - hist) 52 | difs = np.sum(difs, axis=1) 53 | pred_i = np.argsort(difs)[:3] 54 | pred = db[pred_i, -1] 55 | if len(pred[pred == 0]) > len(pred[pred == 1]): 56 | pl = "akahara" 57 | else: 58 | pl = 'madara' 59 | 60 | print(path, "is similar >> ", end='') 61 | for i in pred_i: 62 | print(pdb[i], end=', ') 63 | print("|Pred >>", pl) 64 | 65 | ## Count success 66 | gt = "akahara" if "akahara" in path else "madara" 67 | if gt == pl: 68 | success_num += 1. 69 | 70 | accuracy = success_num / len(test) 71 | print("Accuracy >>", accuracy, "({}/{})".format(int(success_num), len(test))) 72 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_88.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | train = glob("dataset/test_*") 14 | train.sort() 15 | 16 | db = np.zeros((len(train), 13), dtype=np.int32) 17 | pdb = [] 18 | 19 | for i, path in enumerate(train): 20 | img = dic_color(cv2.imread(path)) 21 | ## histogram 22 | for j in range(4): 23 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 24 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 25 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 26 | 27 | ## class 28 | if 'akahara' in path: 29 | cls = 0 30 | elif 'madara' in path: 31 | cls = 1 32 | db[i, -1] = cls 33 | pdb.append(path) 34 | 35 | # k-Means 36 | Class = 2 37 | 38 | feats = db.copy() 39 | np.random.seed(1) 40 | ## assign random class 41 | for i in range(len(feats)): 42 | if np.random.random() < 0.5: 43 | feats[i, -1] = 0 44 | else: 45 | feats[i, -1] = 1 46 | 47 | gs = np.zeros((Class, 12), dtype=np.float32) 48 | 49 | for i in range(Class): 50 | gs[i] = np.mean(feats[np.where(feats[..., -1] == i)[0], :12], axis=0) 51 | print("assigned label") 52 | print(feats) 53 | print("Grabity") 54 | print(gs) 55 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_89.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | train = glob("dataset/test_*") 14 | train.sort() 15 | 16 | db = np.zeros((len(train), 13), dtype=np.int32) 17 | pdb = [] 18 | 19 | for i, path in enumerate(train): 20 | img = dic_color(cv2.imread(path)) 21 | ## histogram 22 | for j in range(4): 23 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 24 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 25 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 26 | 27 | ## class 28 | if 'akahara' in path: 29 | cls = 0 30 | elif 'madara' in path: 31 | cls = 1 32 | db[i, -1] = cls 33 | pdb.append(path) 34 | 35 | # k-Means 36 | Class = 2 37 | 38 | feats = db.copy() 39 | np.random.seed(1) 40 | ## assign random class 41 | for i in range(len(feats)): 42 | if np.random.random() < 0.5: 43 | feats[i, -1] = 0 44 | else: 45 | feats[i, -1] = 1 46 | 47 | while True: 48 | gs = np.zeros((Class, 12), dtype=np.float32) 49 | change_count = 0 50 | 51 | ## compute gravity 52 | for i in range(Class): 53 | gs[i] = np.mean(feats[np.where(feats[..., -1] == i)[0], :12], axis=0) 54 | 55 | ## re-labeling 56 | for i in range(len(feats)): 57 | dis = np.square(np.sum(np.abs(gs - feats[i, :12]), axis=1)) 58 | pred = np.argmin(dis, axis=0) 59 | if int(feats[i, -1]) != pred: 60 | change_count += 1 61 | feats[i, -1] = pred 62 | 63 | if change_count < 1: 64 | break 65 | 66 | for i in range(len(train)): 67 | print(pdb[i], " Pred:", feats[i, -1]) 68 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_90.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | train = glob("dataset/train_*") 14 | train.sort() 15 | 16 | db = np.zeros((len(train), 13), dtype=np.int32) 17 | pdb = [] 18 | 19 | for i, path in enumerate(train): 20 | img = dic_color(cv2.imread(path)) 21 | ## histogram 22 | for j in range(4): 23 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 24 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 25 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 26 | 27 | ## class 28 | if 'akahara' in path: 29 | cls = 0 30 | elif 'madara' in path: 31 | cls = 1 32 | db[i, -1] = cls 33 | pdb.append(path) 34 | 35 | # k-Means 36 | Class = 2 37 | 38 | feats = db.copy() 39 | np.random.seed(4) 40 | ## assign random class 41 | for i in range(len(feats)): 42 | if np.random.random() < 0.3: 43 | feats[i, -1] = 0 44 | else: 45 | feats[i, -1] = 1 46 | 47 | while True: 48 | gs = np.zeros((Class, 12), dtype=np.float32) 49 | change_count = 0 50 | 51 | ## compute gravity 52 | for i in range(Class): 53 | gs[i] = np.mean(feats[np.where(feats[..., -1] == i)[0], :12], axis=0) 54 | 55 | ## re-labeling 56 | for i in range(len(feats)): 57 | dis = np.square(np.sum(np.abs(gs - feats[i, :12]), axis=1)) 58 | pred = np.argmin(dis, axis=0) 59 | if int(feats[i, -1]) != pred: 60 | change_count += 1 61 | feats[i, -1] = pred 62 | 63 | if change_count < 1: 64 | break 65 | 66 | for i in range(len(train)): 67 | print(pdb[i], " Pred:", feats[i, -1]) 68 | -------------------------------------------------------------------------------- /Question_81_90/dataset/test_akahara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/test_akahara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/test_akahara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/test_akahara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/test_madara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/test_madara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/test_madara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/test_madara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_akahara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_akahara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_akahara_3.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_akahara_4.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_akahara_5.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_madara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_madara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_madara_3.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_madara_4.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/dataset/train_madara_5.jpg -------------------------------------------------------------------------------- /Question_81_90/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/imori.jpg -------------------------------------------------------------------------------- /Question_81_90/out_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/out_mask.png -------------------------------------------------------------------------------- /Question_81_90/thorino.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_81_90/thorino.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/_answer_95.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | np.random.seed(0) 4 | 5 | 6 | class NN: 7 | def __init__(self, ind=2, w=64, w2=64, outd=1, lr=0.1): 8 | self.w2 = np.random.randn(ind, w) 9 | self.b2 = np.random.randn(w) 10 | self.w3 = np.random.randn(w, w2) 11 | self.b3 = np.random.randn(w2) 12 | self.wout = np.random.randn(w2, outd) 13 | self.bout = np.random.randn(outd) 14 | self.lr = lr 15 | 16 | def forward(self, x): 17 | self.z1 = x 18 | self.z2 = self.sigmoid(np.dot(self.z1, self.w2) + self.b2) 19 | self.z3 = self.sigmoid(np.dot(self.z2, self.w3) + self.b3) 20 | self.out = self.sigmoid(np.dot(self.z3, self.wout) + self.bout) 21 | return self.out 22 | 23 | def train(self, x, t): 24 | # backpropagation output layer 25 | out_d = 2*(self.out - t) * self.out * (1 - self.out) 26 | out_dW = np.dot(self.z3.T, out_d) 27 | out_dB = np.dot(np.ones([1, out_d.shape[0]]), out_d) 28 | self.wout -= self.lr * out_dW 29 | self.bout -= self.lr * out_dB[0] 30 | 31 | w3_d = np.dot(out_d, self.wout.T) * self.z3 * (1 - self.z3) 32 | w3_dW = np.dot(self.z2.T, w3_d) 33 | w3_dB = np.dot(np.ones([1, w3_d.shape[0]]), w3_d) 34 | self.w3 -= self.lr * w3_dW 35 | self.b3 -= self.lr * w3_dB[0] 36 | 37 | # backpropagation inter layer 38 | w2_d = np.dot(w3_d, self.w3.T) * self.z2 * (1 - self.z2) 39 | w2_dW = np.dot(self.z1.T, w2_d) 40 | w2_dB = np.dot(np.ones([1, w2_d.shape[0]]), w2_d) 41 | self.w2 -= self.lr * w2_dW 42 | self.b2 -= self.lr * w2_dB[0] 43 | 44 | def sigmoid(self, x): 45 | return 1. / (1. + np.exp(-x)) 46 | 47 | train_x = np.array([[0,0], [0,1], [1,0], [1,1]], dtype=np.float32) 48 | train_t = np.array([[0], [1], [1], [0]], dtype=np.float32) 49 | 50 | nn = NN(ind=train_x.shape[1]) 51 | 52 | # train 53 | for i in range(1000): 54 | nn.forward(train_x) 55 | nn.train(train_x, train_t) 56 | 57 | # test 58 | for j in range(4): 59 | x = train_x[j] 60 | t = train_t[j] 61 | print("in:", x, "pred:", nn.forward(x)) 62 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_100.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_100.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_100_gt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_100_gt.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_91.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_91.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_91.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # k-Means 10 | Class = 5 11 | 12 | np.random.seed(0) 13 | 14 | img = np.reshape(img, (H*W, -1)) 15 | 16 | i = np.random.choice(np.arange(H*W), Class, replace=False) 17 | Cs = img[i].copy() 18 | 19 | print(Cs) 20 | 21 | clss = np.zeros((H*W), dtype=int) 22 | 23 | for i in range(H*W): 24 | dis = np.sum(np.abs(Cs - img[i]), axis=1) 25 | clss[i] = np.argmin(dis) 26 | 27 | 28 | out = np.reshape(clss, (H, W)) * 50 29 | out = out.astype(np.uint8) 30 | 31 | cv2.imwrite("out.jpg", out) 32 | cv2.imshow("result", out) 33 | cv2.waitKey(0) 34 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_92.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # k-Means 10 | Class = 5 11 | 12 | np.random.seed(0) 13 | 14 | img = np.reshape(img, (H*W, -1)) 15 | 16 | 17 | i = np.random.choice(np.arange(H*W), Class, replace=False) 18 | Cs = img[i].copy() 19 | 20 | 21 | while True: 22 | 23 | clss = np.zeros((H*W), dtype=int) 24 | 25 | for i in range(H*W): 26 | dis = np.sum(np.abs(Cs - img[i]), axis=1) 27 | clss[i] = np.argmin(dis) 28 | 29 | Cs_tmp = np.zeros((Class, 3)) 30 | 31 | for i in range(Class): 32 | Cs_tmp[i] = np.mean(img[clss==i], axis=0) 33 | 34 | if (Cs == Cs_tmp).all(): 35 | break 36 | else: 37 | Cs = Cs_tmp.copy() 38 | 39 | out = np.zeros((H*W, 3), dtype=np.float32) 40 | 41 | for i in range(Class): 42 | out[clss == i] = Cs[i] 43 | 44 | print(Cs) 45 | 46 | out[out < 0] = 0 47 | out[out > 255] = 255 48 | out = np.reshape(out, (H, W, 3)) 49 | out = out.astype(np.uint8) 50 | 51 | cv2.imwrite("out.jpg", out) 52 | cv2.imshow("result", out) 53 | cv2.waitKey(0) 54 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92_k10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_92_k10.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92_m.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_92_m.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_93.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # [x1, y1, x2, y2] 4 | a = np.array((50, 50, 150, 150), dtype=np.float32) 5 | 6 | b = np.array((60, 60, 170, 160), dtype=np.float32) 7 | 8 | def iou(a, b): 9 | area_a = (a[2] - a[0]) * (a[3] - a[1]) 10 | area_b = (b[2] - b[0]) * (b[3] - b[1]) 11 | iou_x1 = np.maximum(a[0], b[0]) 12 | iou_y1 = np.maximum(a[1], b[1]) 13 | iou_x2 = np.minimum(a[2], b[2]) 14 | iou_y2 = np.minimum(a[3], b[3]) 15 | iou_w = iou_x2 - iou_x1 16 | iou_h = iou_y2 - iou_y1 17 | area_iou = iou_w * iou_h 18 | iou = area_iou / (area_a + area_b - area_iou) 19 | 20 | return iou 21 | 22 | print(iou(a, b)) 23 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_94.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_94.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_94.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | np.random.seed(0) 5 | 6 | # read image 7 | img = cv2.imread("imori_1.jpg") 8 | H, W, C = img.shape 9 | 10 | # Grayscale 11 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | 13 | gt = np.array((47, 41, 129, 103), dtype=np.float32) 14 | 15 | cv2.rectangle(img, (gt[0], gt[1]), (gt[2], gt[3]), (0,255,0), 1) 16 | 17 | def iou(a, b): 18 | area_a = (a[2] - a[0]) * (a[3] - a[1]) 19 | area_b = (b[2] - b[0]) * (b[3] - b[1]) 20 | iou_x1 = np.maximum(a[0], b[0]) 21 | iou_y1 = np.maximum(a[1], b[1]) 22 | iou_x2 = np.minimum(a[2], b[2]) 23 | iou_y2 = np.minimum(a[3], b[3]) 24 | iou_w = max(iou_x2 - iou_x1, 0) 25 | iou_h = max(iou_y2 - iou_y1, 0) 26 | area_iou = iou_w * iou_h 27 | iou = area_iou / (area_a + area_b - area_iou) 28 | return iou 29 | 30 | 31 | # crop and create database 32 | Crop_num = 200 33 | L = 60 34 | 35 | for i in range(Crop_num): 36 | x1 = np.random.randint(W-L) 37 | y1 = np.random.randint(H-L) 38 | x2 = x1 + L 39 | y2 = y1 + L 40 | crop = np.array((x1, y1, x2, y2)) 41 | 42 | _iou = iou(gt, crop) 43 | 44 | if _iou >= 0.5: 45 | cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 1) 46 | label = 1 47 | else: 48 | cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 1) 49 | label = 0 50 | 51 | cv2.imwrite("out.jpg", img) 52 | cv2.imshow("result", img) 53 | cv2.waitKey(0) 54 | 55 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_95.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | np.random.seed(0) 4 | 5 | 6 | class NN: 7 | def __init__(self, ind=2, w=64, w2=64, outd=1, lr=0.1): 8 | self.w1 = np.random.normal(0, 1, [ind, w]) 9 | self.b1 = np.random.normal(0, 1, [w]) 10 | self.w2 = np.random.normal(0, 1, [w, w2]) 11 | self.b2 = np.random.normal(0, 1, [w2]) 12 | self.wout = np.random.normal(0, 1, [w2, outd]) 13 | self.bout = np.random.normal(0, 1, [outd]) 14 | self.lr = lr 15 | 16 | def forward(self, x): 17 | self.z1 = x 18 | self.z2 = sigmoid(np.dot(self.z1, self.w1) + self.b1) 19 | self.z3 = sigmoid(np.dot(self.z2, self.w2) + self.b2) 20 | self.out = sigmoid(np.dot(self.z3, self.wout) + self.bout) 21 | return self.out 22 | 23 | def train(self, x, t): 24 | # backpropagation output layer 25 | #En = t * np.log(self.out) + (1-t) * np.log(1-self.out) 26 | En = (self.out - t) * self.out * (1 - self.out) 27 | grad_wout = np.dot(self.z3.T, En) 28 | grad_bout = np.dot(np.ones([En.shape[0]]), En) 29 | self.wout -= self.lr * grad_wout 30 | self.bout -= self.lr * grad_bout 31 | 32 | # backpropagation inter layer 33 | grad_u2 = np.dot(En, self.wout.T) * self.z3 * (1 - self.z3) 34 | grad_w2 = np.dot(self.z2.T, grad_u2) 35 | grad_b2 = np.dot(np.ones([grad_u2.shape[0]]), grad_u2) 36 | self.w2 -= self.lr * grad_w2 37 | self.b2 -= self.lr * grad_b2 38 | 39 | grad_u1 = np.dot(grad_u2, self.w2.T) * self.z2 * (1 - self.z2) 40 | grad_w1 = np.dot(self.z1.T, grad_u1) 41 | grad_b1 = np.dot(np.ones([grad_u1.shape[0]]), grad_u1) 42 | self.w1 -= self.lr * grad_w1 43 | self.b1 -= self.lr * grad_b1 44 | 45 | def sigmoid(x): 46 | return 1. / (1. + np.exp(-x)) 47 | 48 | train_x = np.array([[0,0], [0,1], [1,0], [1,1]], dtype=np.float32) 49 | train_t = np.array([[0], [1], [1], [0]], dtype=np.float32) 50 | 51 | nn = NN() 52 | 53 | # train 54 | for i in range(5000): 55 | nn.forward(train_x) 56 | #print("ite>>", i, 'y >>', nn.forward(train_x)) 57 | nn.train(train_x, train_t) 58 | 59 | # test 60 | for j in range(4): 61 | x = train_x[j] 62 | t = train_t[j] 63 | print("in:", x, "pred:", nn.forward(x)) 64 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_98.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_98.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_99.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/answers/answer_99.jpg -------------------------------------------------------------------------------- /Question_91_100/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/imori.jpg -------------------------------------------------------------------------------- /Question_91_100/imori_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/imori_1.jpg -------------------------------------------------------------------------------- /Question_91_100/imori_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/imori_2.jpg -------------------------------------------------------------------------------- /Question_91_100/imori_many.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/imori_many.jpg -------------------------------------------------------------------------------- /Question_91_100/madara.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Question_91_100/madara.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 图像处理 100 问!! 2 | 代码算法方面的问题请往原[ repo ](https://github.com/yoyoyo-yo/Gasyori100knock)提。 3 | 4 | 中文版本在[这里](https://github.com/gzr2017/ImageProcessing100Wen)。 5 | 英文版本在[这里]( https://github.com/KuKuXia/Image_Processing_100_Questions)。 6 | 7 | 添加了ipynb版本(现仅问题1-10) 8 | 更新(11-20) 9 | -------------------------------------------------------------------------------- /Tutorial/answer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, const char* argv[]){ 6 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 7 | 8 | int width = img.rows; 9 | int height = img.cols; 10 | 11 | cv::Mat out = img.clone(); 12 | 13 | for(int i=0; i(j, i)[0]; 16 | out.at(j, i)[0] = out.at(j, i)[2]; 17 | out.at(j, i)[2] = tmp; 18 | } 19 | } 20 | 21 | cv::imwrite("out.jpg", out); 22 | cv::imshow("sample", out); 23 | cv::waitKey(0); 24 | cv::destroyAllWindows(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Tutorial/assets/out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/out.jpg -------------------------------------------------------------------------------- /Tutorial/assets/out_cpp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/out_cpp.png -------------------------------------------------------------------------------- /Tutorial/assets/out_practice.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/out_practice.jpg -------------------------------------------------------------------------------- /Tutorial/assets/sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample.jpg -------------------------------------------------------------------------------- /Tutorial/assets/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample.png -------------------------------------------------------------------------------- /Tutorial/assets/sample1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample1.png -------------------------------------------------------------------------------- /Tutorial/assets/sample10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample10.png -------------------------------------------------------------------------------- /Tutorial/assets/sample2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample2.png -------------------------------------------------------------------------------- /Tutorial/assets/sample3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample3.png -------------------------------------------------------------------------------- /Tutorial/assets/sample4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample4.png -------------------------------------------------------------------------------- /Tutorial/assets/sample5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample5.png -------------------------------------------------------------------------------- /Tutorial/assets/sample6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample6.png -------------------------------------------------------------------------------- /Tutorial/assets/sample7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample7.png -------------------------------------------------------------------------------- /Tutorial/assets/sample8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample8.png -------------------------------------------------------------------------------- /Tutorial/assets/sample9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/assets/sample9.png -------------------------------------------------------------------------------- /Tutorial/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/Tutorial/imori.jpg -------------------------------------------------------------------------------- /Tutorial/sample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, const char* argv[]){ 6 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 7 | 8 | int width = img.rows; 9 | int height = img.cols; 10 | 11 | cv::Mat img2 = img.clone(); 12 | 13 | //cv::Mat disp(cv::Size(height, width*2+10), CV_8UC3, cv::Scalar(0,0,0)); 14 | 15 | int i = 0, j = 0; 16 | 17 | for(i=0; i(j, i)[0] = 0; 20 | img.at(j, i)[1] = 400; 21 | img.at(j, i)[2] = -200; 22 | } 23 | } 24 | 25 | cv::Mat disp; 26 | cv::Mat tmp[3]; 27 | tmp[0] = img; 28 | tmp[1] = cv::Mat (cv::Size(10, height), CV_8UC3, cv::Scalar(0,0,0)); 29 | tmp[2] = img2; 30 | cv::hconcat(tmp, 3, disp); 31 | 32 | cv::imshow("sample", disp); 33 | cv::waitKey(0); 34 | cv::destroyAllWindows(); 35 | 36 | cv::imwrite("out.jpg", disp); 37 | 38 | return 0; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /assets/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/assets/imori.jpg -------------------------------------------------------------------------------- /assets/imori_512x512.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/assets/imori_512x512.jpg -------------------------------------------------------------------------------- /assets/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lifeyst/ImageProcessing100Wen/fc073557a57b705beeb3e0da79e0f67a4a064d2a/assets/sample.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | matplotlib 3 | opencv-python 4 | -------------------------------------------------------------------------------- /yobi/crop.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # read image 5 | img = cv2.imread("imori_1.jpg") 6 | H, W, C = img.shape 7 | 8 | gt = np.array((47, 41, 129, 103), dtype=np.float32) 9 | cv2.rectangle(img, (gt[0], gt[1]), (gt[2], gt[3]), (0,255,255), 1) 10 | 11 | def iou(a, b): 12 | area_a = (a[2] - a[0]) * (a[3] - a[1]) 13 | area_b = (b[2] - b[0]) * (b[3] - b[1]) 14 | iou_x1 = np.maximum(a[0], b[0]) 15 | iou_y1 = np.maximum(a[1], b[1]) 16 | iou_x2 = np.minimum(a[2], b[2]) 17 | iou_y2 = np.minimum(a[3], b[3]) 18 | iou_w = max(iou_x2 - iou_x1, 0) 19 | iou_h = max(iou_y2 - iou_y1, 0) 20 | area_iou = iou_w * iou_h 21 | iou = area_iou / (area_a + area_b - area_iou) 22 | return iou 23 | 24 | np.random.seed(0) 25 | 26 | Crop_num = 100 27 | L = 56 28 | 29 | for _ in range(Crop_num): 30 | 31 | x1 = np.random.randint(W-L) 32 | y1 = np.random.randint(H-L) 33 | x2 = x1 + L 34 | y2 = y1 + L 35 | 36 | crop = np.array((x1, y1, x2, y2)) 37 | 38 | _iou = iou(gt, crop) 39 | 40 | if _iou >= 0.5: 41 | cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 1) 42 | else: 43 | cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 1) 44 | 45 | cv2.imshow("", img) 46 | cv2.waitKey(0) 47 | -------------------------------------------------------------------------------- /yobi/hog.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | def hog(gray): 13 | # Magnitude and gradient 14 | gray = np.pad(gray, (1, 1), 'edge') 15 | 16 | gx = gray[1:H+1, 2:] - gray[1:H+1, :W] 17 | gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] 18 | gx[gx == 0] = 0.000001 19 | 20 | mag = np.sqrt(gx ** 2 + gy ** 2) 21 | gra = np.arctan(gy / gx) 22 | gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 23 | 24 | # Gradient histogram 25 | gra_n = np.zeros_like(gra, dtype=np.int) 26 | 27 | d = np.pi / 9 28 | for i in range(9): 29 | gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i 30 | 31 | 32 | N = 8 33 | HH = H // N 34 | HW = W // N 35 | Hist = np.zeros((HH, HW, 9), dtype=np.float32) 36 | for y in range(HH): 37 | for x in range(HW): 38 | for j in range(N): 39 | for i in range(N): 40 | Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] 41 | 42 | ## Normalization 43 | C = 3 44 | eps = 1 45 | for y in range(HH): 46 | for x in range(HW): 47 | #for i in range(9): 48 | Hist[y, x] /= np.sqrt(np.sum(Hist[max(y-1,0):min(y+2, HH), max(x-1,0):min(x+2, HW)] ** 2) + eps) 49 | 50 | return Hist 51 | 52 | 53 | train = glob("../Question_81_90/train_*") 54 | train.sort() 55 | 56 | fs = np.zeros((len(train), 16*16*9)) 57 | ls = np.zeros((len(train))) 58 | 59 | for i, path in enumerate(train): 60 | img = cv2.imread(path).astype(np.float32) 61 | # Grayscale 62 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 63 | fs[i] = hog(gray).ravel() 64 | ls[i] = 0 if 'akahara' in path else 1 65 | 66 | from sklearn import svm 67 | clf = svm.SVC(kernel='linear') 68 | clf.fit(X=fs, y=ls) 69 | 70 | 71 | img = cv2.imread("imori.jpg").astype(np.float32) 72 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 73 | 74 | f = hog(gray).ravel() 75 | f = f[None, ...] 76 | print(clf.predict(f)) 77 | 78 | 79 | img = cv2.imread("madara.jpg").astype(np.float32) 80 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 81 | 82 | f = hog(gray).ravel() 83 | f = f[None, ...] 84 | print(clf.predict(f)) 85 | -------------------------------------------------------------------------------- /yobi/neuralnet.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | np.random.seed(4) 4 | 5 | class NN: 6 | def __init__(self, ind=2, w=10, outd=1, lr=1): 7 | self.w2 = np.random.randn(ind, w) 8 | self.w3 = np.random.randn(w, outd) 9 | self.b2 = np.random.randn(w) 10 | self.b3 = np.random.randn(1) 11 | self.lr = lr 12 | 13 | def forward(self, x): 14 | self.z1 = x 15 | #self.z1 = np.hstack((x, [1])) 16 | # add bias 17 | self.z2 = sigmoid(np.dot(self.z1, self.w2) + self.b2) 18 | # add bias 19 | #self.z2 = np.hstack((self.z2, [1])) 20 | self.z3 = np.dot(self.z2, self.w3) + self.b3 21 | self.out = sigmoid(self.z3) 22 | return self.out 23 | 24 | def backward(self, x, t): 25 | #w3_d = (self.out * (1 - self.out)) * (self.out - y) 26 | #xw3_delta = np.dot(self.w3.T * (self.out - xy), (self.out) * (1 - self.out)) 27 | #w3_dW = w3_d * self.z2.T[..., None] 28 | #w3_dB = w3_d 29 | 30 | w3_d = 2 * (self.out - t) * sigmoid_derivative(self.out) 31 | print(self.w3.shape) 32 | print(w3_d.shape) 33 | print(self.w3.T.shape) 34 | w3_dW = np.dot(self.w2.T, w3_d) 35 | w3_dB = w3_d 36 | 37 | self.w3 -= self.lr * w3_dW 38 | self.b3 -= self.lr * w3_dB 39 | 40 | #w2_d = np.dot((self.z2 * (1 - self.z2))[..., None], (self.z3.T * w3_d)) 41 | w2_d = np.dot(self.z3.T, w3_d) * (self.z2) * (1 - self.z2) 42 | w2_dW = w2_d[..., None] * self.z1 43 | w2_dB = w2_d 44 | 45 | self.w2 += self.lr * w2_dW 46 | self.b2 += self.lr * w2_dB 47 | 48 | def train(self, x, t): 49 | self.forward(x) 50 | self.backward(x, t) 51 | 52 | 53 | class NeuralNetwork: 54 | def __init__(self, x, y): 55 | self.input = x 56 | self.weights1 = np.random.rand(self.input.shape[1],4) 57 | self.weights2 = np.random.rand(4,1) 58 | self.y = y 59 | self.output = np.zeros(self.y.shape) 60 | 61 | def feedforward(self): 62 | self.layer1 = sigmoid(np.dot(self.input, self.weights1)) 63 | self.output = sigmoid(np.dot(self.layer1, self.weights2)) 64 | 65 | def backprop(self): 66 | # application of the chain rule to find derivative of the loss function with respect to weights2 and weights1 67 | d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output))) 68 | d_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1))) 69 | 70 | # update the weights with the derivative (slope) of the loss function 71 | self.weights1 += d_weights1 72 | self.weights2 += d_weights2 73 | 74 | 75 | def sigmoid(x): 76 | return 1. / (1. + np.exp(-x)) 77 | 78 | def sigmoid_derivative(x): 79 | return sigmoid(x) * (1. - sigmoid(x)) 80 | 81 | trainx = np.array(((0,0), (0,1), (1,0), (1,1)), dtype=np.float32) 82 | trainy = np.array((0, 1, 1, 0), dtype=np.float32) 83 | 84 | nn = NN() 85 | 86 | nn2 = NeuralNetwork(trainx, trainy) 87 | 88 | for i in range(10000): 89 | """ 90 | for j in range(4): 91 | nn.train(trainx[j%4], trainy[j%4]) 92 | #print("epoch: " , i) 93 | for j in range(4): 94 | if i == 0 or i == 9999: 95 | print(nn.forward(trainx[j]), end=' ') 96 | #print() 97 | """ 98 | nn2.feedforward() 99 | nn2.backprop() 100 | 101 | print(nn2.output) 102 | -------------------------------------------------------------------------------- /yobi/neuralnet2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def sigmoid(x): 4 | return 1.0/(1+ np.exp(-x)) 5 | 6 | def sigmoid_derivative(x): 7 | return x * (1.0 - x) 8 | 9 | class NeuralNetwork: 10 | def __init__(self, x, t): 11 | self.z1 = x 12 | self.w2 = np.random.rand(self.z1.shape[1],3) 13 | self.w3 = np.random.rand(3,1) 14 | self.t = t 15 | self.out = np.zeros(self.t.shape) 16 | 17 | def feedforward(self): 18 | self.z2 = sigmoid(np.dot(self.z1, self.w2)) 19 | self.out = sigmoid(np.dot(self.z2, self.w3)) 20 | 21 | def backprop(self): 22 | # application of the chain rule to find derivative of the loss function with respect to w3 and w2 23 | d_w3 = np.dot(self.z2.T, (2*(self.t - self.out) * sigmoid_derivative(self.out))) 24 | d_w2 = np.dot(self.z1.T, (np.dot(2*(self.t - self.out) * sigmoid_derivative(self.out), self.w3.T) * sigmoid_derivative(self.z2))) 25 | 26 | # update the weights with the derivative (slope) of the loss function 27 | self.w2 += d_w2 28 | self.w3 += d_w3 29 | 30 | 31 | if __name__ == "__main__": 32 | X = np.array([[0, 0], 33 | [0, 1], 34 | [1, 0], 35 | [1, 1]]) 36 | y = np.array([[0],[1],[1],[0]]) 37 | nn = NeuralNetwork(X,y) 38 | print(X.shape, y.shape) 39 | for i in range(15000): 40 | nn.feedforward() 41 | nn.backprop() 42 | 43 | print(nn.out) 44 | -------------------------------------------------------------------------------- /yobi/nn.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | np.random.seed(0) 4 | 5 | 6 | class NN: 7 | def __init__(self, ind=2, w=64, outd=1, lr=0.1): 8 | self.w2 = np.random.randn(ind, w) 9 | self.b2 = np.random.randn(w) 10 | self.wout = np.random.randn(w, outd) 11 | self.bout = np.random.randn(outd) 12 | self.lr = lr 13 | 14 | def forward(self, x): 15 | self.z1 = x 16 | self.z2 = self.sigmoid(np.dot(self.z1, self.w2) + self.b2) 17 | self.out = self.sigmoid(np.dot(self.z2, self.wout) + self.bout) 18 | return self.out 19 | 20 | def train(self, x, t): 21 | # backpropagation output layer 22 | out_d = 2*(self.out - t) * self.out * (1 - self.out) 23 | out_dW = np.dot(self.z2.T, out_d) 24 | out_dB = np.dot(np.ones([1, out_d.shape[0]]), out_d) 25 | self.wout -= self.lr * out_dW 26 | self.bout -= self.lr * out_dB[0] 27 | 28 | # backpropagation inter layer 29 | w2_d = np.dot(out_d, self.wout.T) * self.z2 * (1 - self.z2) 30 | w2_dW = np.dot(self.z1.T, w2_d) 31 | w2_dB = np.dot(np.ones([1, w2_d.shape[0]]), w2_d) 32 | self.w2 -= self.lr * w2_dW 33 | self.b2 -= self.lr * w2_dB[0] 34 | 35 | def sigmoid(self, x): 36 | return 1. / (1. + np.exp(-x)) 37 | 38 | train_x = np.array([[0,0], [0,1], [1,0], [1,1]], dtype=np.float32) 39 | train_t = np.array([[0], [1], [1], [0]], dtype=np.float32) 40 | 41 | nn = NN(ind=train_x.shape[1]) 42 | 43 | # training 44 | for i in range(1000): 45 | nn.forward(train_x) 46 | nn.train(train_x, train_t) 47 | 48 | # test 49 | for j in range(4): 50 | x = train_x[j] 51 | t = train_t[j] 52 | print("in:", x, "pred:", nn.forward(x)) 53 | --------------------------------------------------------------------------------