【BadApple】提取矢量化邊界

BadApple提取矢量化邊界使用Python,OpenCV處理,通過
cap = cv.VideoCapture(fileName)
while True:
? ? ? ? hasFrame, frame = cap.read()
? ? ? ? if not hasFrame:
? ? ? ? ? ? break
提取視頻中每幀圖像,
并進行兩步處理:
- 提取邊緣
# 轉(zhuǎn)二值圖像
? ? ? ? ret, img_threshold = cv.threshold(frame, 128, 255, cv.THRESH_BINARY)
? ? ? ? # 使用canny提取邊界
? ? ? ? img_canny = cv.Canny(img_threshold, 128, 255)
- 第一步提取的邊界信息為二維數(shù)組表示的圖像,其中高亮度值為邊界。這一步將第一步提取的邊界信息轉(zhuǎn)換為矢量數(shù)據(jù),采用的方法為:搜索
這里采用了四個方向的搜索(adjacent),導(dǎo)致產(chǎn)生很多碎線,應(yīng)當采用八方向,純屬失誤
# 廣搜
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
此時已提取出矢量化的邊界信息,不過數(shù)據(jù)量相當大,可以使用曲線抽稀算法再處理一步(有損壓縮)