【BadApple】提取矢量化边界

BadApple提取矢量化边界使用Python,OpenCV处理,通过
cap = cv.VideoCapture(fileName)
while True:
hasFrame, frame = cap.read()
if not hasFrame:
break
提取视频中每帧图像,
并进行两步处理:
- 提取边缘
# 转二值图像
ret, img_threshold = cv.threshold(frame, 128, 255, cv.THRESH_BINARY)
# 使用canny提取边界
img_canny = cv.Canny(img_threshold, 128, 255)
- 第一步提取的边界信息为二维数组表示的图像,其中高亮度值为边界。这一步将第一步提取的边界信息转换为矢量数据,采用的方法为:搜索
这里采用了四个方向的搜索(adjacent),导致产生很多碎线,应当采用八方向,纯属失误
# 广搜
def Bfs(img, w, h, i, j):
ret = [[i, j]]
queue = [[i, j]]
img[i, j] = 0
adjacent = [[0, 1], [0, -1], [1, 0],[-1, 0]] # 四向搜索
while (len(queue) > 0):
cnt = queue.pop()
for k in range(len(adjacent)):
x = cnt[0] + adjacent[k][0]
y = cnt[1] + adjacent[k][1]
if (x < 0 or x >= w or y < 0 and y >= h):
continue
if (img[x, y] == 255):
img[x, y] = 0
queue.append([x, y])
ret.append([x, y])
return ret
# 矢量化边界
def Ve****************(img, w, h):
ret = []
for i in range(w):
for j in range(h):
if (img[i, j] == 255):
# BFS 搜索边界
line = Bfs(img, w, h, i, j)
ret.append(line)
return ret
此时已提取出矢量化的边界信息,不过数据量相当大,可以使用曲线抽稀算法再处理一步(有损压缩)