From 38305760a4db4c6f8addf176630197960e7fc6a9 Mon Sep 17 00:00:00 2001 From: yoyoyo-yo Date: Mon, 2 Sep 2019 07:58:39 +0900 Subject: [PATCH] aedd --- Question_41_50/answers/answer_49.py | 6 +- Question_61_70/answers/answer_61.py | 76 +++++---- Question_61_70/answers/answer_62.py | 81 ++++++---- Question_61_70/answers/answer_63.py | 92 ++++++----- Question_61_70/answers/answer_64.py | 171 +++++++++++---------- Question_61_70/answers/answer_65.py | 229 +++++++++++++++------------- Question_61_70/answers/answer_66.py | 98 +++++++++--- Question_61_70/answers/answer_67.py | 118 ++++++++++---- Question_61_70/answers/answer_68.py | 137 ++++++++++++----- Question_61_70/answers/answer_69.py | 213 +++++++++++++++++++------- Question_61_70/answers/answer_70.py | 67 +++++--- Question_71_80/answers/answer_71.py | 81 ++++++---- Question_71_80/answers/answer_72.py | 191 +++++++++++++---------- Question_71_80/answers/answer_73.py | 89 +++++++---- Question_71_80/answers/answer_74.py | 78 ++++++---- Question_71_80/answers/answer_75.py | 110 ++++++++----- Question_71_80/answers/answer_76.py | 146 +++++++++++------- Question_71_80/answers/answer_77.py | 49 +++--- Question_71_80/answers/answer_78.py | 50 ++++-- Question_71_80/answers/answer_79.py | 119 +++++++++------ Question_71_80/answers/answer_80.py | 125 +++++++++------ README.md | 1 + 22 files changed, 1489 insertions(+), 838 deletions(-) diff --git a/Question_41_50/answers/answer_49.py b/Question_41_50/answers/answer_49.py index 0a857d25..6785ff22 100644 --- a/Question_41_50/answers/answer_49.py +++ b/Question_41_50/answers/answer_49.py @@ -89,9 +89,9 @@ def Dilate(img, Dil_time=1): # Opening morphology def Morphology_Opening(img, time=1): - dil = Dilate(img, Dil_time=time) - erode = Erode(dil, Erode_time=time) - return erode + out = Erode(img, Erode_time=time) + out = Dilate(out, Dil_time=time) + return out # Read image diff --git a/Question_61_70/answers/answer_61.py b/Question_61_70/answers/answer_61.py index f0b2bb08..8cab5345 100644 --- a/Question_61_70/answers/answer_61.py +++ b/Question_61_70/answers/answer_61.py @@ -2,38 +2,54 @@ import numpy as np import matplotlib.pyplot as plt +# Connect 4 +def connect_4(img): + # get shape + H, W, C = img.shape + + # prepare temporary image + tmp = np.zeros((H, W), dtype=np.int) + + # binarize + tmp[img[..., 0] > 0] = 1 + + # prepare out image + out = np.zeros((H, W, 3), dtype=np.uint8) + + # each pixel + for y in range(H): + for x in range(W): + if tmp[y, x] < 1: + continue + + S = 0 + S += (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]) + S += (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)]) + S += (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]) + S += (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)]) + + if S == 0: + out[y,x] = [0, 0, 255] + elif S == 1: + out[y,x] = [0, 255, 0] + elif S == 2: + out[y,x] = [255, 0, 0] + elif S == 3: + out[y,x] = [255, 255, 0] + elif S == 4: + out[y,x] = [255, 0, 255] + + out = out.astype(np.uint8) + + return out + + + # Read image img = cv2.imread("renketsu.png").astype(np.float32) -H, W, C = img.shape - -tmp = np.zeros((H, W), dtype=np.int) -tmp[img[..., 0]>0] = 1 - -out = np.zeros((H, W, 3), dtype=np.uint8) - -for y in range(H): - for x in range(W): - if tmp[y, x] < 1: - continue - - c = 0 - 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]) - 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)]) - 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]) - 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)]) - - if c == 0: - out[y,x] = [0, 0, 255] - elif c == 1: - out[y,x] = [0, 255, 0] - elif c == 2: - out[y,x] = [255, 0, 0] - elif c == 3: - out[y,x] = [255, 255, 0] - elif c == 4: - out[y,x] = [255, 0, 255] - -out = out.astype(np.uint8) + +# connect 4 +out = connect_4(img) # Save result cv2.imwrite("out.png", out) diff --git a/Question_61_70/answers/answer_62.py b/Question_61_70/answers/answer_62.py index 7789f063..d450965b 100644 --- a/Question_61_70/answers/answer_62.py +++ b/Question_61_70/answers/answer_62.py @@ -2,40 +2,57 @@ import numpy as np import matplotlib.pyplot as plt +# connect 8 +def connect_8(img): + # get shape + H, W, C = img.shape + + # prepare temporary + _tmp = np.zeros((H, W), dtype=np.int) + + # get binarize + _tmp[img[..., 0] > 0] = 1 + + # inverse for connect 8 + tmp = 1 - _tmp + + # prepare image + out = np.zeros((H, W, 3), dtype=np.uint8) + + # each pixel + for y in range(H): + for x in range(W): + if _tmp[y, x] < 1: + continue + + S = 0 + S += (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]) + S += (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)]) + S += (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]) + S += (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)]) + + if S == 0: + out[y,x] = [0, 0, 255] + elif S == 1: + out[y,x] = [0, 255, 0] + elif S == 2: + out[y,x] = [255, 0, 0] + elif S == 3: + out[y,x] = [255, 255, 0] + elif S == 4: + out[y,x] = [255, 0, 255] + + out = out.astype(np.uint8) + + return out + + # Read image img = cv2.imread("renketsu.png").astype(np.float32) -H, W, C = img.shape - -_tmp = np.zeros((H, W), dtype=np.int) -_tmp[img[..., 0]>0] = 1 - -tmp = 1 - _tmp - -out = np.zeros((H, W, 3), dtype=np.uint8) - -for y in range(H): - for x in range(W): - if _tmp[y, x] < 1: - continue - - c = 0 - 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]) - 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)]) - 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]) - 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)]) - - if c == 0: - out[y,x] = [0, 0, 255] - elif c == 1: - out[y,x] = [0, 255, 0] - elif c == 2: - out[y,x] = [255, 0, 0] - elif c == 3: - out[y,x] = [255, 255, 0] - elif c == 4: - out[y,x] = [255, 0, 255] - -out = out.astype(np.uint8) + +# connect 8 +out = connect_8(img) + # Save result cv2.imwrite("out.png", out) diff --git a/Question_61_70/answers/answer_63.py b/Question_61_70/answers/answer_63.py index d499c0a7..432f2697 100644 --- a/Question_61_70/answers/answer_63.py +++ b/Question_61_70/answers/answer_63.py @@ -2,46 +2,62 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("gazo.png").astype(np.float32) -H, W, C = img.shape - -out = np.zeros((H, W), dtype=np.int) -out[img[..., 0]>0] = 1 - -count = 1 -while count > 0: - count = 0 - tmp = out.copy() - for y in range(H): - for x in range(W): - if out[y, x] < 1: - continue - - judge = 0 - - ## condition 1 - 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: - judge += 1 + +# thining algorythm +def thining(img): + # get shape + H, W, C = img.shape + + # prepare out image + out = np.zeros((H, W), dtype=np.int) + out[img[..., 0] > 0] = 1 + + count = 1 + while count > 0: + count = 0 + tmp = out.copy() + # each pixel ( rasta scan ) + for y in range(H): + for x in range(W): + # skip black pixel + if out[y, x] < 1: + continue + + # count satisfied conditions + judge = 0 - ## condition 2 - c = 0 - 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]) - 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)]) - 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]) - 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)]) - if c == 1: - judge += 1 + ## condition 1 + 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: + judge += 1 + + ## condition 2 + c = 0 + 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]) + 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)]) + 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]) + 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)]) + if c == 1: + judge += 1 + + ##x condition 3 + if np.sum(tmp[max(y-1, 0) : min(y+2, H), max(x-1, 0) : min(x+2, W)]) >= 4: + judge += 1 - ##x condition 3 - if np.sum(tmp[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) >= 4: - judge += 1 - - if judge == 3: - out[y,x] = 0 - count += 1 - -out = out.astype(np.uint8) * 255 + # if all conditions are satisfied + if judge == 3: + out[y, x] = 0 + count += 1 + + out = out.astype(np.uint8) * 255 + + return out + + +# Read image +img = cv2.imread("gazo.png").astype(np.float32) + +# thining +out = thining(img) # Save result cv2.imwrite("out.png", out) diff --git a/Question_61_70/answers/answer_64.py b/Question_61_70/answers/answer_64.py index 4b4553c3..115a6b70 100644 --- a/Question_61_70/answers/answer_64.py +++ b/Question_61_70/answers/answer_64.py @@ -2,93 +2,108 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("gazo.png").astype(np.float32) -H, W, C = img.shape -out = np.zeros((H, W), dtype=np.int) -out[img[..., 0]>0] = 1 +# hilditch thining +def hilditch(img): + # get shape + H, W, C = img.shape -tmp = out.copy() -_tmp = 1 - tmp + # prepare out image + out = np.zeros((H, W), dtype=np.int) + out[img[..., 0] > 0] = 1 -count = 1 -while count > 0: - count = 0 + # inverse pixel value tmp = out.copy() _tmp = 1 - tmp - tmp2 = out.copy() - _tmp2 = 1 - tmp2 - - for y in range(H): - for x in range(W): - if out[y, x] < 1: - continue - - judge = 0 - - ## condition 1 - 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: - judge += 1 + count = 1 + while count > 0: + count = 0 + tmp = out.copy() + _tmp = 1 - tmp + + tmp2 = out.copy() + _tmp2 = 1 - tmp2 + + # each pixel + for y in range(H): + for x in range(W): + # skip black pixel + if out[y, x] < 1: + continue - ## condition 2 - c = 0 - 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]) - 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)]) - 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]) - 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)]) - if c == 1: - judge += 1 + judge = 0 - ## condition 3 - if np.sum(tmp[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) >= 3: - judge += 1 - - ## condition 4 - if np.sum(out[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) >= 2: - judge += 1 - - ## condition 5 - _tmp2 = 1-out - - c = 0 - 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]) - c += (_tmp2[max(y-1,0),x] - _tmp2[max(y-1,0),x] * (1-tmp[max(y-1,0),max(x-1,0)]) * _tmp2[y,max(x-1,0)]) - 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]) - 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)]) - if c == 1 or (out[max(y-1,0), max(x-1,0)] != tmp[max(y-1,0), max(x-1,0)]): - judge += 1 - - c = 0 - 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])) - 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)]) - 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]) - 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)]) - if c == 1 or (out[max(y-1,0), x] != tmp[max(y-1,0), x]): - judge += 1 - - c = 0 - c += (_tmp2[y,min(x+1,W-1)] - _tmp2[y,min(x+1,W-1)] * (1-tmp[max(y-1,0),min(x+1,W-1)]) * _tmp2[max(y-1,0),x]) - c += (_tmp2[max(y-1,0),x] - _tmp2[max(y-1,0),x] * _tmp2[max(y-1,0),max(x-1,0)] * _tmp2[y,max(x-1,0)]) - 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]) - 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)]) - if c == 1 or (out[max(y-1,0), min(x+1,W-1)] != tmp[max(y-1,0), min(x+1,W-1)]): - judge += 1 - - c = 0 - 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]) - 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)])) - 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]) - 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)]) - if c == 1 or (out[y, max(x-1,0)] != tmp[y, max(x-1,0)]): - judge += 1 - - if judge >= 8: - out[y,x] = 0 - count += 1 + ## condition 1 + 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: + judge += 1 + + ## condition 2 + c = 0 + 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]) + 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)]) + 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]) + 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)]) + if c == 1: + judge += 1 + + ## condition 3 + if np.sum(tmp[max(y-1, 0) : min(y+2, H), max(x-1, 0) : min(x+2, W)]) >= 3: + judge += 1 + + ## condition 4 + if np.sum(out[max(y-1, 0) : min(y+2, H), max(x-1, 0) : min(x+2, W)]) >= 2: + judge += 1 + + ## condition 5 + _tmp2 = 1 - out + + c = 0 + 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]) + c += (_tmp2[max(y-1, 0), x] - _tmp2[max(y-1, 0), x] * (1 - tmp[max(y-1, 0), max(x-1, 0)]) * _tmp2[y, max(x-1, 0)]) + 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]) + 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)]) + if c == 1 or (out[max(y-1, 0), max(x-1,0 )] != tmp[max(y-1, 0), max(x-1, 0)]): + judge += 1 + + c = 0 + 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])) + 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)]) + 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]) + 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)]) + if c == 1 or (out[max(y-1, 0), x] != tmp[max(y-1, 0), x]): + judge += 1 + + c = 0 + c += (_tmp2[y, min(x+1, W-1)] - _tmp2[y, min(x+1, W-1)] * (1 - tmp[max(y-1, 0), min(x+1, W-1)]) * _tmp2[max(y-1, 0), x]) + c += (_tmp2[max(y-1, 0), x] - _tmp2[max(y-1, 0), x] * _tmp2[max(y-1, 0), max(x-1, 0)] * _tmp2[y, max(x-1, 0)]) + 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]) + 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)]) + if c == 1 or (out[max(y-1, 0), min(x+1, W-1)] != tmp[max(y-1, 0), min(x+1, W-1)]): + judge += 1 + + c = 0 + 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]) + 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)])) + 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]) + 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)]) + if c == 1 or (out[y, max(x-1, 0)] != tmp[y, max(x-1, 0)]): + judge += 1 -out = out.astype(np.uint8) * 255 + if judge >= 8: + out[y, x] = 0 + count += 1 + + out = out.astype(np.uint8) * 255 + + return out + + +# Read image +img = cv2.imread("gazo.png").astype(np.float32) + +# hilditch thining +out = hilditch(img) # Save result cv2.imwrite("out.png", out) diff --git a/Question_61_70/answers/answer_65.py b/Question_61_70/answers/answer_65.py index 07d9b344..6e6e8469 100644 --- a/Question_61_70/answers/answer_65.py +++ b/Question_61_70/answers/answer_65.py @@ -2,120 +2,135 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("gazo.png").astype(np.float32) -H, W, C = img.shape - -out = np.zeros((H, W), dtype=np.int) -out[img[..., 0]>0] = 1 - -out = 1 - out - -while True: - s1 = [] - s2 = [] - - # step 1 - for y in range(1, H-1): - for x in range(1, W-1): - - # condition 1 - if out[y, x] > 0: - continue - - # condition 2 - f1 = 0 - if (out[y-1, x+1] - out[y-1, x]) == 1: - f1 += 1 - if (out[y, x+1] - out[y-1, x+1]) == 1: - f1 += 1 - if (out[y+1, x+1] - out[y, x+1]) == 1: - f1 += 1 - if (out[y+1, x] - out[y+1,x+1]) == 1: - f1 += 1 - if (out[y+1, x-1] - out[y+1, x]) == 1: - f1 += 1 - if (out[y, x-1] - out[y+1, x-1]) == 1: - f1 += 1 - if (out[y-1, x-1] - out[y, x-1]) == 1: - f1 += 1 - if (out[y-1, x] - out[y-1, x-1]) == 1: - f1 += 1 - - if f1 != 1: - continue + +# Zhang Suen thining algorythm +def Zhang_Suen_thining(img): + # get shape + H, W, C = img.shape + + # prepare out image + out = np.zeros((H, W), dtype=np.int) + out[img[..., 0] > 0] = 1 + + # inverse + out = 1 - out + + while True: + s1 = [] + s2 = [] + + # step 1 ( rasta scan ) + for y in range(1, H-1): + for x in range(1, W-1): - # condition 3 - f2 = np.sum(out[y-1:y+2, x-1:x+2]) - if f2 < 2 or f2 > 6: - continue - - # condition 4 - if out[y-1, x] + out[y, x+1] + out[y+1, x] < 1: - continue - - # condition 5 - if out[y, x+1] + out[y+1, x] + out[y, x-1] < 1: - continue + # condition 1 + if out[y, x] > 0: + continue + + # condition 2 + f1 = 0 + if (out[y-1, x+1] - out[y-1, x]) == 1: + f1 += 1 + if (out[y, x+1] - out[y-1, x+1]) == 1: + f1 += 1 + if (out[y+1, x+1] - out[y, x+1]) == 1: + f1 += 1 + if (out[y+1, x] - out[y+1,x+1]) == 1: + f1 += 1 + if (out[y+1, x-1] - out[y+1, x]) == 1: + f1 += 1 + if (out[y, x-1] - out[y+1, x-1]) == 1: + f1 += 1 + if (out[y-1, x-1] - out[y, x-1]) == 1: + f1 += 1 + if (out[y-1, x] - out[y-1, x-1]) == 1: + f1 += 1 + + if f1 != 1: + continue + + # condition 3 + f2 = np.sum(out[y-1:y+2, x-1:x+2]) + if f2 < 2 or f2 > 6: + continue - s1.append([y, x]) - - for v in s1: - out[v[0], v[1]] = 1 - - # step 2 - for y in range(1, H-1): - for x in range(1, W-1): - - # condition 1 - if out[y, x] > 0: - continue - - # condition 2 - f1 = 0 - if (out[y-1, x+1] - out[y-1, x]) == 1: - f1 += 1 - if (out[y, x+1] - out[y-1, x+1]) == 1: - f1 += 1 - if (out[y+1, x+1] - out[y, x+1]) == 1: - f1 += 1 - if (out[y+1, x] - out[y+1,x+1]) == 1: - f1 += 1 - if (out[y+1, x-1] - out[y+1, x]) == 1: - f1 += 1 - if (out[y, x-1] - out[y+1, x-1]) == 1: - f1 += 1 - if (out[y-1, x-1] - out[y, x-1]) == 1: - f1 += 1 - if (out[y-1, x] - out[y-1, x-1]) == 1: - f1 += 1 - - if f1 != 1: - continue + # condition 4 + if out[y-1, x] + out[y, x+1] + out[y+1, x] < 1: + continue + + # condition 5 + if out[y, x+1] + out[y+1, x] + out[y, x-1] < 1: + continue + + s1.append([y, x]) + + for v in s1: + out[v[0], v[1]] = 1 + + # step 2 ( rasta scan ) + for y in range(1, H-1): + for x in range(1, W-1): - # condition 3 - f2 = np.sum(out[y-1:y+2, x-1:x+2]) - if f2 < 2 or f2 > 6: - continue - - # condition 4 - if out[y-1, x] + out[y, x+1] + out[y, x-1] < 1: - continue - - # condition 5 - if out[y-1, x] + out[y+1, x] + out[y, x-1] < 1: - continue + # condition 1 + if out[y, x] > 0: + continue + + # condition 2 + f1 = 0 + if (out[y-1, x+1] - out[y-1, x]) == 1: + f1 += 1 + if (out[y, x+1] - out[y-1, x+1]) == 1: + f1 += 1 + if (out[y+1, x+1] - out[y, x+1]) == 1: + f1 += 1 + if (out[y+1, x] - out[y+1,x+1]) == 1: + f1 += 1 + if (out[y+1, x-1] - out[y+1, x]) == 1: + f1 += 1 + if (out[y, x-1] - out[y+1, x-1]) == 1: + f1 += 1 + if (out[y-1, x-1] - out[y, x-1]) == 1: + f1 += 1 + if (out[y-1, x] - out[y-1, x-1]) == 1: + f1 += 1 + + if f1 != 1: + continue + + # condition 3 + f2 = np.sum(out[y-1:y+2, x-1:x+2]) + if f2 < 2 or f2 > 6: + continue - s2.append([y, x]) + # condition 4 + if out[y-1, x] + out[y, x+1] + out[y, x-1] < 1: + continue + + # condition 5 + if out[y-1, x] + out[y+1, x] + out[y, x-1] < 1: + continue + + s2.append([y, x]) - for v in s2: - out[v[0], v[1]] = 1 + for v in s2: + out[v[0], v[1]] = 1 + + # if not any pixel is changed + if len(s1) < 1 and len(s2) < 1: + break + + out = 1 - out + out = out.astype(np.uint8) * 255 + + return out + + +# Read image +img = cv2.imread("gazo.png").astype(np.float32) - if len(s1) < 1 and len(s2) < 1: - break +# Zhang Suen thining +out = Zhang_Suen_thining(img) -out = 1 - out -out = out.astype(np.uint8) * 255 # Save result cv2.imwrite("out.png", out) diff --git a/Question_61_70/answers/answer_66.py b/Question_61_70/answers/answer_66.py index a8180f17..9a831e53 100644 --- a/Question_61_70/answers/answer_66.py +++ b/Question_61_70/answers/answer_66.py @@ -2,42 +2,94 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape -# Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] +# get HOG step1 +def HOG_step1(img): + # Grayscale + def BGR2GRAY(img): + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray -# Magnitude and gradient -gray = np.pad(gray, (1, 1), 'edge') + # Magnitude and gradient + def get_gradXY(gray): + H, W = gray.shape -gx = gray[1:H+1, 2:] - gray[1:H+1, :W] -gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] -gx[gx == 0] = 0.000001 + # padding before grad + gray = np.pad(gray, (1, 1), 'edge') -mag = np.sqrt(gx ** 2 + gy ** 2) -gra = np.arctan(gy / gx) -gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 + # get grad x + gx = gray[1:H+1, 2:] - gray[1:H+1, :W] + # get grad y + gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] + # replace 0 with + gx[gx == 0] = 1e-6 -# Gradient histogram -gra_n = np.zeros_like(gra, dtype=np.int) + return gx, gy -d = np.pi / 9 -for i in range(9): - gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i + # get magnitude and gradient + def get_MagGrad(gx, gy): + # get gradient maginitude + magnitude = np.sqrt(gx ** 2 + gy ** 2) + + # get gradient angle + gradient = np.arctan(gy / gx) + + gradient[gradient < 0] = np.pi / 2 + gradient[gradient < 0] + np.pi / 2 + + return magnitude, gradient + + # Gradient histogram + def quantization(gradient): + # prepare quantization table + gradient_quantized = np.zeros_like(gradient, dtype=np.int) + + # quantization base + d = np.pi / 9 + + # quantization + for i in range(9): + gradient_quantized[np.where((gradient >= d * i) & (gradient <= d * (i + 1)))] = i + + return gradient_quantized + + # 1. BGR -> Gray + gray = BGR2GRAY(img) + + # 1. Gray -> Gradient x and y + gx, gy = get_gradXY(gray) -# Draw -_mag = (mag / mag.max() * 255).astype(np.uint8) + # 2. get gradient magnitude and angle + magnitude, gradient = get_MagGrad(gx, gy) -cv2.imwrite("out_mag.jpg", _mag) + # 3. Quantization + gradient_quantized = quantization(gradient) -# Save result + return magnitude, gradient_quantized + + +# Read image +img = cv2.imread("imori.jpg").astype(np.float32) + +# get HOG step1 +magnitude, gradient_quantized = HOG_step1(img) + +# Write gradient magnitude to file +_magnitude = (magnitude / magnitude.max() * 255).astype(np.uint8) + +cv2.imwrite("out_mag.jpg", _magnitude) + +# Write gradient angle to file +H, W, C = img.shape out = np.zeros((H, W, 3), dtype=np.uint8) + +# define color C = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 0, 255], [0, 255, 255], [127, 127, 0], [127, 0, 127], [0, 127, 127]] + +# draw color for i in range(9): - out[gra_n == i] = C[i] + out[gradient_quantized == i] = C[i] + cv2.imwrite("out_gra.jpg", out) cv2.imshow("result", out) diff --git a/Question_61_70/answers/answer_67.py b/Question_61_70/answers/answer_67.py index 81d8b312..98b36e3d 100644 --- a/Question_61_70/answers/answer_67.py +++ b/Question_61_70/answers/answer_67.py @@ -2,45 +2,103 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape +# get HOG step2 +def HOG_step2(img): + # Grayscale + def BGR2GRAY(img): + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray -# Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + # Magnitude and gradient + def get_gradXY(gray): + H, W = gray.shape -# Magnitude and gradient -gray = np.pad(gray, (1, 1), 'edge') + # padding before grad + gray = np.pad(gray, (1, 1), 'edge') -gx = gray[1:H+1, 2:] - gray[1:H+1, :W] -gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] -gx[gx == 0] = 0.000001 + # get grad x + gx = gray[1:H+1, 2:] - gray[1:H+1, :W] + # get grad y + gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] + # replace 0 with + gx[gx == 0] = 1e-6 -mag = np.sqrt(gx ** 2 + gy ** 2) -gra = np.arctan(gy / gx) -gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 + return gx, gy -# Gradient histogram -gra_n = np.zeros_like(gra, dtype=np.int) + # get magnitude and gradient + def get_MagGrad(gx, gy): + # get gradient maginitude + magnitude = np.sqrt(gx ** 2 + gy ** 2) -d = np.pi / 9 -for i in range(9): - gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i - -N = 8 -HH = H // N -HW = W // N -Hist = np.zeros((HH, HW, 9), dtype=np.float32) -for y in range(HH): - for x in range(HW): - for j in range(N): - for i in range(N): - Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] - + # get gradient angle + gradient = np.arctan(gy / gx) + + gradient[gradient < 0] = np.pi / 2 + gradient[gradient < 0] + np.pi / 2 + + return magnitude, gradient + + # Gradient histogram + def quantization(gradient): + # prepare quantization table + gradient_quantized = np.zeros_like(gradient, dtype=np.int) + + # quantization base + d = np.pi / 9 + + # quantization + for i in range(9): + gradient_quantized[np.where((gradient >= d * i) & (gradient <= d * (i + 1)))] = i + + return gradient_quantized + + + # get gradient histogram + def gradient_histogram(gradient_quantized, magnitude, N=8): + # get shape + H, W = magnitude.shape + + # get cell num + cell_N_H = H // N + cell_N_W = W // N + histogram = np.zeros((cell_N_H, cell_N_W, 9), dtype=np.float32) + + # each pixel + for y in range(cell_N_H): + for x in range(cell_N_W): + for j in range(N): + for i in range(N): + histogram[y, x, gradient_quantized[y * 4 + j, x * 4 + i]] += magnitude[y * 4 + j, x * 4 + i] + return histogram + + # 1. BGR -> Gray + gray = BGR2GRAY(img) + + # 1. Gray -> Gradient x and y + gx, gy = get_gradXY(gray) + + # 2. get gradient magnitude and angle + magnitude, gradient = get_MagGrad(gx, gy) + + # 3. Quantization + gradient_quantized = quantization(gradient) + + # 4. Gradient histogram + histogram = gradient_histogram(gradient_quantized, magnitude) + + return histogram + + +# Read image +img = cv2.imread("imori.jpg").astype(np.float32) + +# get HOG step2 +histogram = HOG_step2(img) + +# write histogram to file for i in range(9): plt.subplot(3,3,i+1) - plt.imshow(Hist[..., i]) + plt.imshow(histogram[..., i]) plt.axis('off') plt.xticks(color="None") plt.yticks(color="None") diff --git a/Question_61_70/answers/answer_68.py b/Question_61_70/answers/answer_68.py index 05769077..9da74972 100644 --- a/Question_61_70/answers/answer_68.py +++ b/Question_61_70/answers/answer_68.py @@ -2,53 +2,118 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape +# get HOG +def HOG(img): + # Grayscale + def BGR2GRAY(img): + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray -# Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + # Magnitude and gradient + def get_gradXY(gray): + H, W = gray.shape -# Magnitude and gradient -gray = np.pad(gray, (1, 1), 'edge') + # padding before grad + gray = np.pad(gray, (1, 1), 'edge') -gx = gray[1:H+1, 2:] - gray[1:H+1, :W] -gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] -gx[gx == 0] = 0.000001 + # get grad x + gx = gray[1:H+1, 2:] - gray[1:H+1, :W] + # get grad y + gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] + # replace 0 with + gx[gx == 0] = 1e-6 -mag = np.sqrt(gx ** 2 + gy ** 2) -gra = np.arctan(gy / gx) -gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 + return gx, gy -# Gradient histogram -gra_n = np.zeros_like(gra, dtype=np.int) + # get magnitude and gradient + def get_MagGrad(gx, gy): + # get gradient maginitude + magnitude = np.sqrt(gx ** 2 + gy ** 2) -d = np.pi / 9 -for i in range(9): - gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i + # get gradient angle + gradient = np.arctan(gy / gx) + + gradient[gradient < 0] = np.pi / 2 + gradient[gradient < 0] + np.pi / 2 + + return magnitude, gradient + + # Gradient histogram + def quantization(gradient): + # prepare quantization table + gradient_quantized = np.zeros_like(gradient, dtype=np.int) + + # quantization base + d = np.pi / 9 + + # quantization + for i in range(9): + gradient_quantized[np.where((gradient >= d * i) & (gradient <= d * (i + 1)))] = i + + return gradient_quantized + + + # get gradient histogram + def gradient_histogram(gradient_quantized, magnitude, N=8): + # get shape + H, W = magnitude.shape + + # get cell num + cell_N_H = H // N + cell_N_W = W // N + histogram = np.zeros((cell_N_H, cell_N_W, 9), dtype=np.float32) + + # each pixel + for y in range(cell_N_H): + for x in range(cell_N_W): + for j in range(N): + for i in range(N): + histogram[y, x, gradient_quantized[y * 4 + j, x * 4 + i]] += magnitude[y * 4 + j, x * 4 + i] + + return histogram + # histogram normalization + def normalization(histogram, C=3, epsilon=1): + cell_N_H, cell_N_W, _ = histogram.shape + ## each histogram + for y in range(cell_N_H): + for x in range(cell_N_W): + #for i in range(9): + histogram[y, x] /= np.sqrt(np.sum(histogram[max(y - 1, 0) : min(y + 2, cell_N_H), + max(x - 1, 0) : min(x + 2, cell_N_W)] ** 2) + epsilon) + + return histogram + + # 1. BGR -> Gray + gray = BGR2GRAY(img) + + # 1. Gray -> Gradient x and y + gx, gy = get_gradXY(gray) + + # 2. get gradient magnitude and angle + magnitude, gradient = get_MagGrad(gx, gy) + + # 3. Quantization + gradient_quantized = quantization(gradient) + + # 4. Gradient histogram + histogram = gradient_histogram(gradient_quantized, magnitude) -N = 8 -HH = H // N -HW = W // N -Hist = np.zeros((HH, HW, 9), dtype=np.float32) -for y in range(HH): - for x in range(HW): - for j in range(N): - for i in range(N): - Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] - -## Normalization -C = 3 -eps = 1 -for y in range(HH): - for x in range(HW): - #for i in range(9): - 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) + # 5. Histogram normalization + histogram = normalization(histogram) + return histogram + + +# Read image +img = cv2.imread("imori.jpg").astype(np.float32) + +# get HOG +histogram = HOG(img) + +# Write result to file for i in range(9): plt.subplot(3,3,i+1) - plt.imshow(Hist[..., i]) + plt.imshow(histogram[..., i]) plt.axis('off') plt.xticks(color="None") plt.yticks(color="None") diff --git a/Question_61_70/answers/answer_69.py b/Question_61_70/answers/answer_69.py index 49ddac84..fff52e7d 100644 --- a/Question_61_70/answers/answer_69.py +++ b/Question_61_70/answers/answer_69.py @@ -2,75 +2,170 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape +# get HOG +def HOG(img): + # Grayscale + def BGR2GRAY(img): + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray + + # Magnitude and gradient + def get_gradXY(gray): + H, W = gray.shape + + # padding before grad + gray = np.pad(gray, (1, 1), 'edge') + + # get grad x + gx = gray[1:H+1, 2:] - gray[1:H+1, :W] + # get grad y + gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] + # replace 0 with + gx[gx == 0] = 1e-6 + + return gx, gy + + # get magnitude and gradient + def get_MagGrad(gx, gy): + # get gradient maginitude + magnitude = np.sqrt(gx ** 2 + gy ** 2) + + # get gradient angle + gradient = np.arctan(gy / gx) + + gradient[gradient < 0] = np.pi / 2 + gradient[gradient < 0] + np.pi / 2 + + return magnitude, gradient -# Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + # Gradient histogram + def quantization(gradient): + # prepare quantization table + gradient_quantized = np.zeros_like(gradient, dtype=np.int) -# Magnitude and gradient -gray = np.pad(gray, (1, 1), 'edge') + # quantization base + d = np.pi / 9 -gx = gray[1:H+1, 2:] - gray[1:H+1, :W] -gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] -gx[gx == 0] = 0.000001 + # quantization + for i in range(9): + gradient_quantized[np.where((gradient >= d * i) & (gradient <= d * (i + 1)))] = i -mag = np.sqrt(gx ** 2 + gy ** 2) -gra = np.arctan(gy / gx) -gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 + return gradient_quantized -# Gradient histogram -gra_n = np.zeros_like(gra, dtype=np.int) -d = np.pi / 9 -for i in range(9): - gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i + # get gradient histogram + def gradient_histogram(gradient_quantized, magnitude, N=8): + # get shape + H, W = magnitude.shape + # get cell num + cell_N_H = H // N + cell_N_W = W // N + histogram = np.zeros((cell_N_H, cell_N_W, 9), dtype=np.float32) + + # each pixel + for y in range(cell_N_H): + for x in range(cell_N_W): + for j in range(N): + for i in range(N): + histogram[y, x, gradient_quantized[y * 4 + j, x * 4 + i]] += magnitude[y * 4 + j, x * 4 + i] + + return histogram + + # histogram normalization + def normalization(histogram, C=3, epsilon=1): + cell_N_H, cell_N_W, _ = histogram.shape + ## each histogram + for y in range(cell_N_H): + for x in range(cell_N_W): + #for i in range(9): + histogram[y, x] /= np.sqrt(np.sum(histogram[max(y - 1, 0) : min(y + 2, cell_N_H), + max(x - 1, 0) : min(x + 2, cell_N_W)] ** 2) + epsilon) + + return histogram + + # 1. BGR -> Gray + gray = BGR2GRAY(img) + + # 1. Gray -> Gradient x and y + gx, gy = get_gradXY(gray) + + # 2. get gradient magnitude and angle + magnitude, gradient = get_MagGrad(gx, gy) + + # 3. Quantization + gradient_quantized = quantization(gradient) + + # 4. Gradient histogram + histogram = gradient_histogram(gradient_quantized, magnitude) -N = 8 -HH = H // N -HW = W // N -Hist = np.zeros((HH, HW, 9), dtype=np.float32) -for y in range(HH): - for x in range(HW): - for j in range(N): - for i in range(N): - Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] - -## Normalization -C = 3 -eps = 1 -for y in range(HH): - for x in range(HW): - #for i in range(9): - 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) - -## Draw -out = gray[1:H+1, 1:W+1].copy().astype(np.uint8) - -for y in range(HH): - for x in range(HW): - cx = x * N + N // 2 - cy = y * N + N // 2 - x1 = cx + N // 2 - 1 - y1 = cy - x2 = cx - N // 2 + 1 - y2 = cy - - h = Hist[y, x] / np.sum(Hist[y, x]) - h /= h.max() + # 5. Histogram normalization + histogram = normalization(histogram) + + return histogram + + +# draw HOG +def draw_HOG(img, histogram): + # Grayscale + def BGR2GRAY(img): + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray + + def draw(gray, histogram, N=8): + # get shape + H, W = gray.shape + cell_N_H, cell_N_W, _ = histogram.shape - for c in range(9): - #angle = (20 * c + 10 - 90) / 180. * np.pi - angle = (20 * c + 10) / 180. * np.pi - rx = int(np.sin(angle) * (x1 - cx) + np.cos(angle) * (y1 - cy) + cx) - ry = int(np.cos(angle) * (x1 - cx) - np.cos(angle) * (y1 - cy) + cy) - lx = int(np.sin(angle) * (x2 - cx) + np.cos(angle) * (y2 - cy) + cx) - ly = int(np.cos(angle) * (x2 - cx) - np.cos(angle) * (y2 - cy) + cy) + ## Draw + out = gray[1 : H + 1, 1 : W + 1].copy().astype(np.uint8) + + for y in range(cell_N_H): + for x in range(cell_N_W): + cx = x * N + N // 2 + cy = y * N + N // 2 + x1 = cx + N // 2 - 1 + y1 = cy + x2 = cx - N // 2 + 1 + y2 = cy + + h = histogram[y, x] / np.sum(histogram[y, x]) + h /= h.max() - c = int(255. * h[c]) - cv2.line(out, (lx, ly), (rx, ry), (c, c, c), thickness=1) + for c in range(9): + #angle = (20 * c + 10 - 90) / 180. * np.pi + # get angle + angle = (20 * c + 10) / 180. * np.pi + rx = int(np.sin(angle) * (x1 - cx) + np.cos(angle) * (y1 - cy) + cx) + ry = int(np.cos(angle) * (x1 - cx) - np.cos(angle) * (y1 - cy) + cy) + lx = int(np.sin(angle) * (x2 - cx) + np.cos(angle) * (y2 - cy) + cx) + ly = int(np.cos(angle) * (x2 - cx) - np.cos(angle) * (y2 - cy) + cy) + + # color is HOG value + c = int(255. * h[c]) + + # draw line + cv2.line(out, (lx, ly), (rx, ry), (c, c, c), thickness=1) + + return out + + + # get gray + gray = BGR2GRAY(img) + + # draw HOG + out = draw(gray, histogram) + + return out + +# Read image +img = cv2.imread("imori.jpg").astype(np.float32) + +# get HOG +histogram = HOG(img) + +# draw HOG +out = draw_HOG(img, histogram) + # Save result cv2.imwrite("out.jpg", out) diff --git a/Question_61_70/answers/answer_70.py b/Question_61_70/answers/answer_70.py index 4c584b4c..0451b8d3 100644 --- a/Question_61_70/answers/answer_70.py +++ b/Question_61_70/answers/answer_70.py @@ -2,35 +2,54 @@ import numpy as np import matplotlib.pyplot as plt +# BGR -> HSV +def BGR2HSV(_img): + img = _img.copy() / 255. + + hsv = np.zeros_like(img, dtype=np.float32) + + # get max and min + max_v = np.max(img, axis=2).copy() + min_v = np.min(img, axis=2).copy() + min_arg = np.argmin(img, axis=2) + + # H + hsv[..., 0][np.where(max_v == min_v)]= 0 + ## if min == B + ind = np.where(min_arg == 0) + hsv[..., 0][ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 + ## if min == R + ind = np.where(min_arg == 2) + hsv[..., 0][ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 + ## if min == G + ind = np.where(min_arg == 1) + hsv[..., 0][ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 + + # S + hsv[..., 1] = max_v.copy() - min_v.copy() + + # V + hsv[..., 2] = max_v.copy() + + return hsv + +# make mask +def get_mask(hsv): + mask = np.zeros_like(hsv[..., 0]) + #mask[np.where((hsv > 180) & (hsv[0] < 260))] = 255 + mask[np.logical_and((hsv[..., 0] > 180), (hsv[..., 0] < 260))] = 255 + return mask + + # Read image -img = cv2.imread("imori.jpg").astype(np.float32) / 255. +img = cv2.imread("imori.jpg").astype(np.float32) # RGB > HSV -out = np.zeros_like(img) - -max_v = np.max(img, axis=2).copy() -min_v = np.min(img, axis=2).copy() -min_arg = np.argmin(img, axis=2) - -H = np.zeros_like(max_v) - -H[np.where(max_v == min_v)] = 0 -## if min == B -ind = np.where(min_arg == 0) -H[ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 -## if min == R -ind = np.where(min_arg == 2) -H[ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 -## if min == G -ind = np.where(min_arg == 1) -H[ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 - -V = max_v.copy() -S = max_v.copy() - min_v.copy() +hsv = BGR2HSV(img) + # color tracking -mask = np.zeros_like(H) -mask[np.where((H>180) & (H<260))] = 255 +mask = get_mask(hsv) out = mask.astype(np.uint8) diff --git a/Question_71_80/answers/answer_71.py b/Question_71_80/answers/answer_71.py index ad0cad89..381e9c3a 100644 --- a/Question_71_80/answers/answer_71.py +++ b/Question_71_80/answers/answer_71.py @@ -2,41 +2,66 @@ import numpy as np import matplotlib.pyplot as plt +# BGR -> HSV +def BGR2HSV(_img): + img = _img.copy() / 255. + + hsv = np.zeros_like(img, dtype=np.float32) + + # get max and min + max_v = np.max(img, axis=2).copy() + min_v = np.min(img, axis=2).copy() + min_arg = np.argmin(img, axis=2) + + # H + hsv[..., 0][np.where(max_v == min_v)]= 0 + ## if min == B + ind = np.where(min_arg == 0) + hsv[..., 0][ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 + ## if min == R + ind = np.where(min_arg == 2) + hsv[..., 0][ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 + ## if min == G + ind = np.where(min_arg == 1) + hsv[..., 0][ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 + + # S + hsv[..., 1] = max_v.copy() - min_v.copy() + + # V + hsv[..., 2] = max_v.copy() + + return hsv + +# make mask +def get_mask(hsv): + mask = np.zeros_like(hsv[..., 0]) + #mask[np.where((hsv > 180) & (hsv[0] < 260))] = 255 + mask[np.logical_and((hsv[..., 0] > 180), (hsv[..., 0] < 260))] = 1 + return mask + +# masking +def masking(img, mask): + mask = 1 - mask + out = img.copy() + # mask [h, w] -> [h, w, channel] + mask = np.tile(mask, [3, 1, 1]).transpose([1, 2, 0]) + out *= mask + + return out + + # Read image -img = cv2.imread("imori.jpg").astype(np.float32) / 255. +img = cv2.imread("imori.jpg").astype(np.float32) # RGB > HSV - -max_v = np.max(img, axis=2).copy() -min_v = np.min(img, axis=2).copy() -min_arg = np.argmin(img, axis=2) - -H = np.zeros_like(max_v) - -H[np.where(max_v == min_v)] = 0 -## if min == B -ind = np.where(min_arg == 0) -H[ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 -## if min == R -ind = np.where(min_arg == 2) -H[ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 -## if min == G -ind = np.where(min_arg == 1) -H[ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 - -V = max_v.copy() -S = max_v.copy() - min_v.copy() +hsv = BGR2HSV(img / 255.) # color tracking -mask = np.zeros_like(H) -mask[np.where((H>180) & (H<260))] = 1 +mask = get_mask(hsv) # masking -mask = 1 - mask -out = img.copy() * 255. - -for c in range(3): - out[..., c] *= mask +out = masking(img, mask) out = out.astype(np.uint8) diff --git a/Question_71_80/answers/answer_72.py b/Question_71_80/answers/answer_72.py index 0b6ae8e0..e8c6823a 100644 --- a/Question_71_80/answers/answer_72.py +++ b/Question_71_80/answers/answer_72.py @@ -2,91 +2,128 @@ import numpy as np import matplotlib.pyplot as plt +# BGR -> HSV +def BGR2HSV(_img): + img = _img.copy() / 255. + + hsv = np.zeros_like(img, dtype=np.float32) + + # get max and min + max_v = np.max(img, axis=2).copy() + min_v = np.min(img, axis=2).copy() + min_arg = np.argmin(img, axis=2) + + # H + hsv[..., 0][np.where(max_v == min_v)]= 0 + ## if min == B + ind = np.where(min_arg == 0) + hsv[..., 0][ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 + ## if min == R + ind = np.where(min_arg == 2) + hsv[..., 0][ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 + ## if min == G + ind = np.where(min_arg == 1) + hsv[..., 0][ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 + + # S + hsv[..., 1] = max_v.copy() - min_v.copy() + + # V + hsv[..., 2] = max_v.copy() + + return hsv + +# make mask +def get_mask(hsv): + mask = np.zeros_like(hsv[..., 0]) + #mask[np.where((hsv > 180) & (hsv[0] < 260))] = 255 + mask[np.logical_and((hsv[..., 0] > 180), (hsv[..., 0] < 260))] = 1 + return mask + +# masking +def masking(img, mask): + mask = 1 - mask + out = img.copy() + # mask [h, w] -> [h, w, channel] + mask = np.tile(mask, [3, 1, 1]).transpose([1, 2, 0]) + out *= mask + + return out + + +# Erosion +def Erode(img, Erode_time=1): + H, W = img.shape + out = img.copy() + + # kernel + MF = np.array(((0, 1, 0), + (1, 0, 1), + (0, 1, 0)), dtype=np.int) + + # each erode + for i in range(Erode_time): + tmp = np.pad(out, (1, 1), 'edge') + # erode + for y in range(1, H + 1): + for x in range(1, W + 1): + if np.sum(MF * tmp[y - 1 : y + 2 , x - 1 : x + 2]) < 1 * 4: + out[y - 1, x - 1] = 0 + + return out + + +# Dilation +def Dilate(img, Dil_time=1): + H, W = img.shape + + # kernel + MF = np.array(((0, 1, 0), + (1, 0, 1), + (0, 1, 0)), dtype=np.int) + + # each dilate time + out = img.copy() + for i in range(Dil_time): + tmp = np.pad(out, (1, 1), 'edge') + for y in range(1, H + 1): + for x in range(1, W + 1): + if np.sum(MF * tmp[y - 1 : y + 2, x - 1 : x + 2]) >= 1: + out[y - 1, x - 1] = 1 + + return out + + +# Opening morphology +def Morphology_Opening(img, time=1): + out = Erode(img, Erode_time=time) + out = Dilate(out, Dil_time=time) + return out + +# Closing morphology +def Morphology_Closing(img, time=1): + out = Dilate(img, Dil_time=time) + out = Erode(out, Erode_time=time) + return out + + # Read image -img = cv2.imread("imori.jpg").astype(np.float32) / 255. +img = cv2.imread("imori.jpg").astype(np.float32) # RGB > HSV - -max_v = np.max(img, axis=2).copy() -min_v = np.min(img, axis=2).copy() -min_arg = np.argmin(img, axis=2) - -H = np.zeros_like(max_v) - -H[np.where(max_v == min_v)] = 0 -## if min == B -ind = np.where(min_arg == 0) -H[ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 -## if min == R -ind = np.where(min_arg == 2) -H[ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 -## if min == G -ind = np.where(min_arg == 1) -H[ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 - -V = max_v.copy() -S = max_v.copy() - min_v.copy() +hsv = BGR2HSV(img / 255.) # color tracking -mask = np.zeros_like(H) -mask[np.where((H>180) & (H<260))] = 255 - -h, w, _ = img.shape - -# Closing -## Morphology filter -MF = np.array(((0, 1, 0), - (1, 0, 1), - (0, 1, 0)), dtype=np.int) - -## Morphology - dilate -Dil_time = 5 - -for i in range(Dil_time): - tmp = np.pad(mask, (1, 1), 'edge') - for y in range(1, h+1): - for x in range(1, w+1): - if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: - mask[y-1, x-1] = 255 -## Morphology - erode -Erode_time = 5 - -for i in range(Erode_time): - tmp = np.pad(mask, (1, 1), 'edge') - for y in range(1, h+1): - for x in range(1, w+1): - if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: - mask[y-1, x-1] = 0 - -# Opening -## Morphology - erode -Erode_time = 5 - -for i in range(Erode_time): - tmp = np.pad(mask, (1, 1), 'edge') - for y in range(1, h+1): - for x in range(1, w+1): - if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: - mask[y-1, x-1] = 0 - -## Morphology - dilate -Dil_time = 5 - -for i in range(Dil_time): - tmp = np.pad(mask, (1, 1), 'edge') - for y in range(1, h+1): - for x in range(1, w+1): - if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: - mask[y-1, x-1] = 255 +mask = get_mask(hsv) -# masking -cv2.imwrite("out_mask.png", mask.astype(np.uint8)) +# closing +mask = Morphology_Closing(mask, time=5) -mask = 1 - mask / 255 -out = img.copy() * 255. +# opening +mask = Morphology_Opening(mask, time=5) -for c in range(3): - out[..., c] *= mask +# masking +out = masking(img, mask) out = out.astype(np.uint8) diff --git a/Question_71_80/answers/answer_73.py b/Question_71_80/answers/answer_73.py index 7401cb3f..d74dc6a5 100644 --- a/Question_71_80/answers/answer_73.py +++ b/Question_71_80/answers/answer_73.py @@ -2,39 +2,66 @@ import numpy as np import matplotlib.pyplot as plt +# Grayscale +def BGR2GRAY(img): + # Grayscale + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray + +# Bi-Linear interpolation +def bl_interpolate(img, ax=1., ay=1.): + if len(img.shape) > 2: + H, W, C = img.shape + else: + H, W = img.shape + C = 1 + + aH = int(ay * H) + aW = int(ax * W) + + # get position of resized image + y = np.arange(aH).repeat(aW).reshape(aW, -1) + x = np.tile(np.arange(aW), (aH, 1)) + + # get position of original position + y = (y / ay) + x = (x / ax) + + ix = np.floor(x).astype(np.int) + iy = np.floor(y).astype(np.int) + + ix = np.minimum(ix, W-2) + iy = np.minimum(iy, H-2) + + # get distance + dx = x - ix + dy = y - iy + + if C > 1: + dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1) + dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1) + + # interpolation + 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] + + out = np.clip(out, 0, 255) + out = out.astype(np.uint8) + + return out + + # Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape +img = cv2.imread("imori.jpg").astype(np.float) -# Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] - -def resize(img, a): - _h, _w = img.shape - h = int(a * _h) - w = int(a * _w) - y = np.arange(h).repeat(w).reshape(w, -1) - x = np.tile(np.arange(w), (h, 1)) - y = (y / a) - x = (x / a) - - ix = np.floor(x).astype(np.int) - iy = np.floor(y).astype(np.int) - ix = np.minimum(ix, _w-2) - iy = np.minimum(iy, _w-2) - - dx = x - ix - dy = y - iy - - 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] - out[out>255] = 255 - - return out - -p = resize(gray, 0.5) -p = resize(p, 2.) - -out = p.astype(np.uint8) +gray = BGR2GRAY(img) + +# Bilinear interpolation +out = bl_interpolate(gray.astype(np.float32), ax=0.5, ay=0.5) + +# Bilinear interpolation +out = bl_interpolate(out, ax=2., ay=2.) + +out = out.astype(np.uint8) # Save result cv2.imshow("result", out) diff --git a/Question_71_80/answers/answer_74.py b/Question_71_80/answers/answer_74.py index 289dff5a..354755f7 100644 --- a/Question_71_80/answers/answer_74.py +++ b/Question_71_80/answers/answer_74.py @@ -2,40 +2,66 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape - # Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] +def BGR2GRAY(img): + # Grayscale + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray + +# Bi-Linear interpolation +def bl_interpolate(img, ax=1., ay=1.): + if len(img.shape) > 2: + H, W, C = img.shape + else: + H, W = img.shape + C = 1 + + aH = int(ay * H) + aW = int(ax * W) + + # get position of resized image + y = np.arange(aH).repeat(aW).reshape(aW, -1) + x = np.tile(np.arange(aW), (aH, 1)) + + # get position of original position + y = (y / ay) + x = (x / ax) + + ix = np.floor(x).astype(np.int) + iy = np.floor(y).astype(np.int) -def resize(img, a): - _h, _w = img.shape - h = int(a * _h) - w = int(a * _w) + ix = np.minimum(ix, W-2) + iy = np.minimum(iy, H-2) - y = np.arange(h).repeat(w).reshape(w, -1) - x = np.tile(np.arange(w), (h, 1)) - y = (y / a) - x = (x / a) + # get distance + dx = x - ix + dy = y - iy - ix = np.floor(x).astype(np.int) - iy = np.floor(y).astype(np.int) - ix = np.minimum(ix, _w-2) - iy = np.minimum(iy, _w-2) + if C > 1: + dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1) + dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1) + + # interpolation + 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] + + out = np.clip(out, 0, 255) + out = out.astype(np.uint8) + + return out + + +# Read image +img = cv2.imread("imori.jpg").astype(np.float) - dx = x - ix - dy = y - iy - - 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] - out[out>255] = 255 +gray = BGR2GRAY(img) - return out +# Bilinear interpolation +out = bl_interpolate(gray.astype(np.float32), ax=0.5, ay=0.5) -p = resize(gray, 0.5) -p = resize(p, 2.) +# Bilinear interpolation +out = bl_interpolate(out, ax=2., ay=2.) -out = np.abs(gray - p) +out = np.abs(out - gray) out = out / out.max() * 255 diff --git a/Question_71_80/answers/answer_75.py b/Question_71_80/answers/answer_75.py index ac71a34e..b07eb8a1 100644 --- a/Question_71_80/answers/answer_75.py +++ b/Question_71_80/answers/answer_75.py @@ -2,51 +2,77 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape - # Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] - -def resize(img, a): - _h, _w = img.shape - h = int(a * _h) - w = int(a * _w) - """ - y = np.arange(h).repeat(w).reshape(w, -1) - x = np.tile(np.arange(w), (h, 1)) - y = np.floor(y / a).astype(np.int) - x = np.floor(x / a).astype(np.int) - y = np.minimum(y, _h-1) - x = np.minimum(x, _w-1) - out = img[y,x] - """ - y = np.arange(h).repeat(w).reshape(w, -1) - x = np.tile(np.arange(w), (h, 1)) - y = (y / a) - x = (x / a) - - ix = np.floor(x).astype(np.int) - iy = np.floor(y).astype(np.int) - ix = np.minimum(ix, _w-2) - iy = np.minimum(iy, _w-2) - - dx = x - ix - dy = y - iy - #dx = np.repeat(np.expand_dims(dx, axis=-1), 3, axis=-1) - #dy = np.repeat(np.expand_dims(dy, axis=-1), 3, axis=-1) - - 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] - out[out>255] = 255 +def BGR2GRAY(img): + # Grayscale + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray + +# Bi-Linear interpolation +def bl_interpolate(img, ax=1., ay=1.): + if len(img.shape) > 2: + H, W, C = img.shape + else: + H, W = img.shape + C = 1 + + aH = int(ay * H) + aW = int(ax * W) + + # get position of resized image + y = np.arange(aH).repeat(aW).reshape(aW, -1) + x = np.tile(np.arange(aW), (aH, 1)) + + # get position of original position + y = (y / ay) + x = (x / ax) + + ix = np.floor(x).astype(np.int) + iy = np.floor(y).astype(np.int) + + ix = np.minimum(ix, W-2) + iy = np.minimum(iy, H-2) + + # get distance + dx = x - ix + dy = y - iy + + if C > 1: + dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1) + dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1) + + # interpolation + 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] + + out = np.clip(out, 0, 255) + out = out.astype(np.uint8) + + return out + +# make image pyramid +def make_pyramid(gray): + # first element + pyramid = [gray] + # each scale + for i in range(1, 6): + # define scale + a = 2. ** i + + # down scale + p = bl_interpolate(gray, ax=1./a, ay=1. / a) + + # add pyramid list + pyramid.append(p) + + return pyramid + +# Read image +img = cv2.imread("imori.jpg").astype(np.float) - return out +gray = BGR2GRAY(img) -pyramid = [gray] -for i in range(1, 6): - a = 2. ** i - p = resize(gray, 1. / a) - pyramid.append(p) +# pyramid +pyramid = make_pyramid(gray) for i in range(6): cv2.imwrite("out_{}.jpg".format(2**i), pyramid[i].astype(np.uint8)) diff --git a/Question_71_80/answers/answer_76.py b/Question_71_80/answers/answer_76.py index afcd7a83..7696b3f6 100644 --- a/Question_71_80/answers/answer_76.py +++ b/Question_71_80/answers/answer_76.py @@ -2,64 +2,106 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape - # Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] - -def resize(img, a): - _h, _w = img.shape - h = int(a * _h) - w = int(a * _w) - """ - y = np.arange(h).repeat(w).reshape(w, -1) - x = np.tile(np.arange(w), (h, 1)) - y = np.floor(y / a).astype(np.int) - x = np.floor(x / a).astype(np.int) - y = np.minimum(y, _h-1) - x = np.minimum(x, _w-1) - out = img[y,x] - """ - y = np.arange(h).repeat(w).reshape(w, -1) - x = np.tile(np.arange(w), (h, 1)) - y = (y / a) - x = (x / a) - - ix = np.floor(x).astype(np.int) - iy = np.floor(y).astype(np.int) - ix = np.minimum(ix, _w-2) - iy = np.minimum(iy, _w-2) - - dx = x - ix - dy = y - iy - #dx = np.repeat(np.expand_dims(dx, axis=-1), 3, axis=-1) - #dy = np.repeat(np.expand_dims(dy, axis=-1), 3, axis=-1) - - 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] - out[out>255] = 255 +def BGR2GRAY(img): + # Grayscale + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray - return out +# Bi-Linear interpolation +def bl_interpolate(img, ax=1., ay=1.): + if len(img.shape) > 2: + H, W, C = img.shape + else: + H, W = img.shape + C = 1 -pyramid = [gray] -for i in range(1, 6): - a = 2. ** i - p = resize(gray, 1. / a) - p = resize(p, a) - pyramid.append(p) - -out = np.zeros((H, W), dtype=np.float32) + aH = int(ay * H) + aW = int(ax * W) + + # get position of resized image + y = np.arange(aH).repeat(aW).reshape(aW, -1) + x = np.tile(np.arange(aW), (aH, 1)) + + # get position of original position + y = (y / ay) + x = (x / ax) + + ix = np.floor(x).astype(np.int) + iy = np.floor(y).astype(np.int) + + ix = np.minimum(ix, W-2) + iy = np.minimum(iy, H-2) + + # get distance + dx = x - ix + dy = y - iy + + if C > 1: + dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1) + dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1) + + # interpolation + 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] + + out = np.clip(out, 0, 255) + out = out.astype(np.uint8) + + return out -out += np.abs(pyramid[0] - pyramid[1]) -out += np.abs(pyramid[0] - pyramid[3]) -out += np.abs(pyramid[0] - pyramid[5]) -out += np.abs(pyramid[1] - pyramid[4]) -out += np.abs(pyramid[2] - pyramid[3]) -out += np.abs(pyramid[3] - pyramid[5]) +# make image pyramid +def make_pyramid(gray): + # first element + pyramid = [gray] + # each scale + for i in range(1, 6): + # define scale + a = 2. ** i + # down scale + p = bl_interpolate(gray, ax=1./a, ay=1. / a) -out = out / out.max() * 255 + # up scale + p = bl_interpolate(p, ax=a, ay=a) + + # add pyramid list + pyramid.append(p.astype(np.float32)) + + return pyramid + +# make saliency map +def saliency_map(pyramid): + # get shape + H, W = pyramid[0].shape + + # prepare out image + out = np.zeros((H, W), dtype=np.float32) + + # add each difference + out += np.abs(pyramid[0] - pyramid[1]) + out += np.abs(pyramid[0] - pyramid[3]) + out += np.abs(pyramid[0] - pyramid[5]) + out += np.abs(pyramid[1] - pyramid[4]) + out += np.abs(pyramid[2] - pyramid[3]) + out += np.abs(pyramid[3] - pyramid[5]) + + # normalization + out = out / out.max() * 255 + + return out + + +# Read image +img = cv2.imread("imori.jpg").astype(np.float) + +# grayscale +gray = BGR2GRAY(img) + +# pyramid +pyramid = make_pyramid(gray) + +# pyramid -> saliency +out = saliency_map(pyramid) out = out.astype(np.uint8) diff --git a/Question_71_80/answers/answer_77.py b/Question_71_80/answers/answer_77.py index c2392591..e62bf8b6 100644 --- a/Question_71_80/answers/answer_77.py +++ b/Question_71_80/answers/answer_77.py @@ -3,32 +3,47 @@ import matplotlib.pyplot as plt # Gabor -K_size = 111 -Sigma = 10 -Gamma = 1.2 -Lambda = 10. -Psi = 0. -angle = 0 +def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): + # get half size + d = K_size // 2 -d = K_size // 2 + # prepare kernel + gabor = np.zeros((K_size, K_size), dtype=np.float32) -gabor = np.zeros((K_size, K_size), dtype=np.float32) + # each value + for y in range(K_size): + for x in range(K_size): + # distance from center + px = x - d + py = y - d -for y in range(K_size): - for x in range(K_size): - px = x - d - py = y - d - theta = angle / 180. * np.pi - _x = np.cos(theta) * px + np.sin(theta) * py - _y = -np.sin(theta) * px + np.cos(theta) * py - gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) + # degree -> radian + theta = angle / 180. * np.pi -gabor /= np.sum(np.abs(gabor)) + # get kernel x + _x = np.cos(theta) * px + np.sin(theta) * py + + # get kernel y + _y = -np.sin(theta) * px + np.cos(theta) * py + + # fill kernel + gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) + + # kernel normalization + gabor /= np.sum(np.abs(gabor)) + + return gabor + + +# get gabor kernel +gabor = Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0) # Visualize +# normalize to [0, 255] out = gabor - np.min(gabor) out /= np.max(out) out *= 255 + out = out.astype(np.uint8) cv2.imwrite("out.jpg", out) cv2.imshow("result", out) diff --git a/Question_71_80/answers/answer_78.py b/Question_71_80/answers/answer_78.py index 24c65de6..643b2b84 100644 --- a/Question_71_80/answers/answer_78.py +++ b/Question_71_80/answers/answer_78.py @@ -4,34 +4,54 @@ # Gabor +def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): + # get half size + d = K_size // 2 -def gabor_f(k=111, s=10, g=1.2, l=10, p=0, A=0): - d = k // 2 + # prepare kernel + gabor = np.zeros((K_size, K_size), dtype=np.float32) - gabor = np.zeros((k, k), dtype=np.float32) - - for y in range(k): - for x in range(k): - px = x - d - py = y - d - theta = A / 180. * np.pi - _x = np.cos(theta) * px + np.sin(theta) * py - _y = -np.sin(theta) * px + np.cos(theta) * py - gabor[y, x] = np.exp(-(_x**2 + g**2 * _y**2) / (2 * s**2)) * np.cos(2*np.pi*_x/l + p) + # each value + for y in range(K_size): + for x in range(K_size): + # distance from center + px = x - d + py = y - d + + # degree -> radian + theta = angle / 180. * np.pi + + # get kernel x + _x = np.cos(theta) * px + np.sin(theta) * py + + # get kernel y + _y = -np.sin(theta) * px + np.cos(theta) * py - gabor /= np.sum(np.abs(gabor)) + # fill kernel + gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) - return gabor + # kernel normalization + gabor /= np.sum(np.abs(gabor)) + return gabor + + +# define each angle As = [0, 45, 90, 135] +# prepare pyplot plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) +# each angle for i, A in enumerate(As): - gabor = gabor_f(A=A) + # get gabor kernel + gabor = Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=A) + + # normalize to [0, 255] out = gabor - np.min(gabor) out /= np.max(out) out *= 255 + out = out.astype(np.uint8) plt.subplot(1, 4, i+1) plt.imshow(out, cmap='gray') diff --git a/Question_71_80/answers/answer_79.py b/Question_71_80/answers/answer_79.py index 6160ff5b..82168972 100644 --- a/Question_71_80/answers/answer_79.py +++ b/Question_71_80/answers/answer_79.py @@ -2,67 +2,96 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape - -# Otsu binary -## Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] -gray = gray.astype(np.uint8) +# Grayscale +def BGR2GRAY(img): + # Grayscale + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray # Gabor -def gabor_f(k=111, s=10, g=1.2, l=10, p=0, A=0): - d = k // 2 +def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): + # get half size + d = K_size // 2 + + # prepare kernel + gabor = np.zeros((K_size, K_size), dtype=np.float32) + + # each value + for y in range(K_size): + for x in range(K_size): + # distance from center + px = x - d + py = y - d - gabor = np.zeros((k, k), dtype=np.float32) - - for y in range(k): - for x in range(k): - px = x - d - py = y - d - theta = A / 180. * np.pi - _x = np.cos(theta) * px + np.sin(theta) * py - _y = -np.sin(theta) * px + np.cos(theta) * py - gabor[y, x] = np.exp(-(_x**2 + g**2 * _y**2) / (2 * s**2)) * np.cos(2*np.pi*_x/l + p) + # degree -> radian + theta = angle / 180. * np.pi - gabor /= np.sum(np.abs(gabor)) + # get kernel x + _x = np.cos(theta) * px + np.sin(theta) * py - return gabor + # get kernel y + _y = -np.sin(theta) * px + np.cos(theta) * py -K_size = 11 -Sigma = 1.5 -Gamma = 1.2 -Lambda = 3. -Psi = 0. + # fill kernel + gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) -gray = np.pad(gray, (K_size//2, K_size//2), 'edge') + # kernel normalization + gabor /= np.sum(np.abs(gabor)) -As = [0, 45, 90, 135] + return gabor -plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) -for i, A in enumerate(As): - gabor = gabor_f(k=K_size, s=Sigma, g=Gamma, l=Lambda, p=Psi, A=A) +def Gabor_filtering(gray, K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): + # get shape + H, W = gray.shape + # padding + gray = np.pad(gray, (K_size//2, K_size//2), 'edge') + + # prepare out image out = np.zeros((H, W), dtype=np.float32) - + + # get gabor filter + gabor = Gabor_filter(K_size=K_size, Sigma=Sigma, Gamma=Gamma, Lambda=Lambda, Psi=0, angle=angle) + + # filtering for y in range(H): for x in range(W): - out[y, x] = np.sum(gray[y:y+K_size, x:x+K_size] * gabor) + out[y, x] = np.sum(gray[y : y + K_size, x : x + K_size] * gabor) - out[out < 0] = 0 - out[out > 255] = 255 - - plt.subplot(1, 4, i+1) - plt.imshow(out, cmap='gray') - plt.axis('off') - plt.title("Angle "+str(A)) + out = np.clip(out, 0, 255) + out = out.astype(np.uint8) -plt.savefig("out.png") -plt.show() + return out - -out = out.astype(np.uint8) +def Gabor_process(img): + # gray scale + gray = BGR2GRAY(img).astype(np.float32) + + # define angle + As = [0, 45, 90, 135] + + # prepare pyplot + plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) + + # each angle + for i, A in enumerate(As): + # gabor filtering + out = Gabor_filtering(gray, K_size=11, Sigma=1.5, Gamma=1.2, Lambda=3, angle=A) + + plt.subplot(1, 4, i+1) + plt.imshow(out, cmap='gray') + plt.axis('off') + plt.title("Angle "+str(A)) + + plt.savefig("out.png") + plt.show() + +# Read image +img = cv2.imread("imori.jpg").astype(np.float32) + +# gabor process +Gabor_process(img) + diff --git a/Question_71_80/answers/answer_80.py b/Question_71_80/answers/answer_80.py index 446b9fa0..54b7e4b5 100644 --- a/Question_71_80/answers/answer_80.py +++ b/Question_71_80/answers/answer_80.py @@ -2,70 +2,105 @@ import numpy as np import matplotlib.pyplot as plt -# Read image -img = cv2.imread("imori.jpg").astype(np.float32) -H, W, C = img.shape - -# Otsu binary -## Grayscale -gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] -gray = gray.astype(np.uint8) +# Grayscale +def BGR2GRAY(img): + # Grayscale + gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] + return gray # Gabor -def gabor_f(k=111, s=10, g=1.2, l=10, p=0, A=0): - d = k // 2 +def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): + # get half size + d = K_size // 2 + + # prepare kernel + gabor = np.zeros((K_size, K_size), dtype=np.float32) + + # each value + for y in range(K_size): + for x in range(K_size): + # distance from center + px = x - d + py = y - d - gabor = np.zeros((k, k), dtype=np.float32) - - for y in range(k): - for x in range(k): - px = x - d - py = y - d - theta = A / 180. * np.pi - _x = np.cos(theta) * px + np.sin(theta) * py - _y = -np.sin(theta) * px + np.cos(theta) * py - gabor[y, x] = np.exp(-(_x**2 + g**2 * _y**2) / (2 * s**2)) * np.cos(2*np.pi*_x/l + p) + # degree -> radian + theta = angle / 180. * np.pi - gabor /= np.sum(np.abs(gabor)) + # get kernel x + _x = np.cos(theta) * px + np.sin(theta) * py - return gabor + # get kernel y + _y = -np.sin(theta) * px + np.cos(theta) * py -K_size = 11 -Sigma = 1.5 -Gamma = 1.2 -Lambda = 3. -Psi = 0. + # fill kernel + gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) -gray = np.pad(gray, (K_size//2, K_size//2), 'edge') + # kernel normalization + gabor /= np.sum(np.abs(gabor)) -As = [0, 45, 90, 135] + return gabor -gs = [] -plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) +def Gabor_filtering(gray, K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): + # get shape + H, W = gray.shape -for i, A in enumerate(As): - gabor = gabor_f(k=K_size, s=Sigma, g=Gamma, l=Lambda, p=Psi, A=A) + # padding + gray = np.pad(gray, (K_size//2, K_size//2), 'edge') + # prepare out image out = np.zeros((H, W), dtype=np.float32) - + + # get gabor filter + gabor = Gabor_filter(K_size=K_size, Sigma=Sigma, Gamma=Gamma, Lambda=Lambda, Psi=0, angle=angle) + + # filtering for y in range(H): for x in range(W): - out[y, x] = np.sum(gray[y:y+K_size, x:x+K_size] * gabor) + out[y, x] = np.sum(gray[y : y + K_size, x : x + K_size] * gabor) + + out = np.clip(out, 0, 255) + out = out.astype(np.uint8) + + return out + + +def Gabor_process(img): + # get shape + H, W, _ = img.shape - out[out < 0] = 0 - out[out > 255] = 255 - - gs.append(out) + # gray scale + gray = BGR2GRAY(img).astype(np.float32) + # define angle + As = [0, 45, 90, 135] + + # prepare pyplot + plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) + + out = np.zeros([H, W], dtype=np.float32) + + # each angle + for i, A in enumerate(As): + # gabor filtering + _out = Gabor_filtering(gray, K_size=11, Sigma=1.5, Gamma=1.2, Lambda=3, angle=A) + + # add gabor filtered image + out += _out + + # scale normalization + out = out / out.max() * 255 + out = out.astype(np.uint8) + + return out + + +# Read image +img = cv2.imread("imori.jpg").astype(np.float32) -out = np.zeros((H, W), dtype=np.float32) -for g in gs: - out += g +# gabor process +out = Gabor_process(img) - -out = out / out.max() * 255 -out = out.astype(np.uint8) cv2.imwrite("out.jpg", out) cv2.imshow("result", out) diff --git a/README.md b/README.md index d87c3852..c9c0fea8 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ PythonとC++の好きな方でやってみてね♡(最近Javascriptも追加 ## Recent +- 2019.9.2 [Python] Q.61~80にAnswerコードをメソッド化 - 2019.8.28 [Python] Q.51~60のAnswerコードをメソッド化 - 2019.8.18 [Python] Q.50までのAnswerコードをメソッド化 - 2019.8.12 [C++]Q.36-40の解答追加