闭合曲线内涂色code(辅助制作Ground truth)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import cv2
import numpy as np
from matplotlib import pyplot as plt

path = 'C:\\Users\\Ashraf\\Desktop\\ground\\'

def red_bw2d(image):
img = image
r, c, _ = img.shape
for i in range(r):
for j in range(c):
if img[i, j][2] > img[i, j][0] and img[i, j][2] > img[i, j][1]:
img[i, j][0] = 255
img[i,j][1] = 255
img[i,j][2] = 255
else:
img[i, j][0] = 0
img[i, j][1] = 0
img[i, j][2] = 0
img = img[:, :, 1]
_, bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

return bin

def green_bw2d(image):
img = image
r, c, _ = img.shape
for i in range(r):
for j in range(c):
if img[i, j][1] > img[i, j][0] and img[i, j][1] > img[i, j][2]:
img[i, j][0] = 255
img[i,j][1] = 255
img[i,j][2] = 255
else:
img[i, j][0] = 0
img[i, j][1] = 0
img[i, j][2] = 0
img = img[:, :, 1]
_, bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

return bin

def blue_bw2d(image):
img = image
r, c, _ = img.shape
for i in range(r):
for j in range(c):
if img[i, j][0] > img[i, j][1] and img[i, j][0] > img[i, j][2]:
img[i, j][0] = 255
img[i,j][1] = 255
img[i,j][2] = 255
else:
img[i, j][0] = 0
img[i, j][1] = 0
img[i, j][2] = 0
img = img[:, :, 1]
_, bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

return bin

def denoise(image):
img = image
img = cv2.medianBlur(img, 3)
return img

def fill(image, name):
point = (0,0)
src = image.copy()
src = np.invert(src)
connectivity = 4
flags = connectivity
flags |= cv2.FLOODFILL_FIXED_RANGE
cv2.floodFill(src, None, point, (0, 0, 0), (100,) * 3, (100,) * 3, flags)
cv2.imwrite(name, src)

def function(img, name):
img = denoise(img)
img = red_bw2d(img)
img = denoise(img)
fill(img, name)

# Main
img = cv2.imread('./3.png', cv2.IMREAD_COLOR)
name = './test.jpg'
function(img, name)
坚持原创分享,您的支持将鼓励我继续创作!