Skip to content

Commit

Permalink
aedd
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyoyo-yo committed Sep 1, 2019
1 parent de8a396 commit 3830576
Show file tree
Hide file tree
Showing 22 changed files with 1,489 additions and 838 deletions.
6 changes: 3 additions & 3 deletions Question_41_50/answers/answer_49.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 46 additions & 30 deletions Question_61_70/answers/answer_61.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
81 changes: 49 additions & 32 deletions Question_61_70/answers/answer_62.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
92 changes: 54 additions & 38 deletions Question_61_70/answers/answer_63.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 3830576

Please sign in to comment.