先上效果圖,這里顯示有點(diǎn)色差, 實(shí)際數(shù)值是純色的, 而不是混色的.
這個(gè)算法最大的特點(diǎn)是保留原始像素的數(shù)值, 而不是把邊框統(tǒng)一變成白色.
實(shí)現(xiàn)的算法也超級(jí)簡(jiǎn)單. 就是有點(diǎn)慢. 考慮到我這個(gè)應(yīng)用場(chǎng)景對(duì)性能要求不高, 比人快就行. 人工是它的幾百倍. 所以也就無(wú)所謂啦.
測(cè)試結(jié)果一張1080*1920的圖用時(shí)3秒, 如果換成c語(yǔ)言估計(jì)0.5秒左右.
算法原理, 每次4個(gè)田子形像素逐行掃描. 發(fā)現(xiàn)4個(gè)像素不一致的就輸出到結(jié)果圖上. 否則就是輸出0.
代碼如下.
#
# demo.py
# 識(shí)別單張圖片
#
import argparse
import os
import numpy as np
import time
from modeling.deeplab import *
from dataloaders import custom_transforms as tr
from PIL import Image
from torchvision import transforms
from dataloaders.utils import *
from torchvision.utils import make_grid, save_image,to_image
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
def main():
im = Image.open("test_border.png")
npimg = np.array(im) # 這個(gè)圖片是1維的索引圖.
# chwimg = npimg.transpose(2,0,1) # HWC 變成 CHW 格式的矩陣
print(npimg.shape)
h,w,c = npimg.shape
src = np.sum(npimg,axis=2) # 這里測(cè)試用, 先把3通道的合成了一個(gè)通道的, 實(shí)際使用的時(shí)候也是1通道的.
print(src.shape)
borderimg = np.zeros(src.shape) #默認(rèn)都輸出了0 后面就不用輸出0了.
# 修補(bǔ)bug, 解決邊框線(xiàn)會(huì)丟失的問(wèn)題.
borderimg[0,:]=src[0,:]
borderimg[:,0]=src[:,0]
borderimg[-1,:]=src[-1,:]
borderimg[:,-1]=src[:,-1]
t1= time.time()
for x in range(0,h-1,1):
for y in range(0,w-1,1):
# point = src[x,y]
# if(point>0):
# print(point)
if not (src[x,y] == src[x+1,y] == src[x,y+1] == src[x+1,y+1]): # 發(fā)現(xiàn)4個(gè)像素不一致的就輸出到結(jié)果圖上.
borderimg[x,y] = src[x,y]
borderimg[x+1,y] = src[x+1,y]
borderimg[x,y+1] = src[x,y+1]
borderimg[x+1,y+1] = src[x+1,y+1]
t2= time.time()
print("耗時(shí)",t2-t1)
plt.figure()
plt.title('display')
plt.imshow(src)
plt.show( )
plt.imshow(borderimg)
plt.show( )
print("start test get image border ...")
if __name__ == "__main__":
main()
else:
main()