原创

: 直线扫描法,填充(闭合?)区域

直线扫描法,填充(闭合?)区域

写着玩的,不知道正经的直线扫描法是不是这样的。
效果不好,图像像素的情况不像是想象的那么简单。
使用横向和纵向的线条找出闭合的区域
结果为 横向图像的闭合 与 纵向图像的闭合 结果的 交集。
不放图片。

结果有线条感,可能在更加简单的图形中能用。图像中有复杂实体的情况几乎没法用。

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
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python 
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import numpy as np
from PIL import Image
import os
import cv2


# 划线法
def fulling(img):
img1 = img.copy()
img2 = img.copy()

# 横向
flag = False
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img[i][j] == 255:
flag = not flag
while img[i][j] == 255:
j += 1
if j >= img.shape[1]:
break
if j >= img.shape[1]:
break
if flag == True:
img1[i][j] = 255
flag = False
cv2.imshow("1", img1)
# 纵向
flag = False
for i in range(img.shape[1]):
for j in range(img.shape[0]):
if img[j][i] == 255:
flag = not flag
while img[j][i] == 255:
j += 1
if j >= img.shape[0]:
break
if j >= img.shape[0]:
break
if flag == True:
img2[j][i] = 255
flag = False
cv2.imshow("2", img2)
img = img1 & img2
# cv2.imshow("3", img)
cv2.waitKey(0)
return img


# [ 0000000010000000001000000 ]
def one_picture(src):
# breakpoint_connect_path = r'./pictures/output_canong3_canonxt_sub_05.tif'
# save_path=r''
# breakpoint_connect_mask = Image.open(breakpoint_connect_path)
if src.split() == 3:
src = src.split()[0]
img = np.asarray(src)
print(img)
img = fulling(img)
cv2.imwrite("pictures/pic.png", img)


def all_picture_full(input, output):
src_path = input
save_path = output
for index, item in enumerate(os.listdir(src_path)):
_src_path = os.path.join(src_path, item)
_save_path = os.path.join(save_path, item)
pred_mask = Image.open(_src_path)
if pred_mask.split() == 3: # 若为3rgb图像 则会读取为灰度
pred_mask = pred_mask.split()[0]
pred_mask = np.array(pred_mask)

_mask_after_full = fulling(pred_mask) # 关键函数

_mask_after_full = Image.fromarray(_mask_after_full.astype(np.uint8))
_mask_after_full.save(_save_path)
print("{}/{}".format(index + 1, len(os.listdir(src_path))))
print(item)


if __name__ == '__main__':
# breakpoint_full_path = r'./pictures/output_canong3_canonxt_sub_05.tif'
# breakpoint_full_mask = Image.open(breakpoint_full_path)
# one_picture(breakpoint_full_mask) # 单张图片测试
src_path = r'C:\Users\brighten\Desktop\0324_两阶段_0306模型,只监督条带区域,带8张图\columb\result2'
save_path = r'C:\Users\brighten\Desktop\0324_两阶段_0306模型,只监督条带区域,带8张图\columb\result3'
all_picture_full(src_path, save_path) # 文件夹内所有图片