欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python-图像边缘算法-动态显示过程

程序员文章站 2022-03-24 15:57:56
...
import os
import matplotlib.pyplot as plt
import cv2
import numpy as np

def get_EuclideanDistance(x,y):
    myx = np.array(x)
    myy = np.array(y)
    return np.sqrt(np.sum((myx-myy)*(myx-myy)))

dir = os.listdir('./train')

if __name__ == '__main__':

    print('loading %s ...'%dir[1])
    print('working')
    image1 = cv2.imread('./train/'+dir[1])
    w = image1.shape[1]
    h = image1.shape[0]
    image2 = np.zeros((h,w,3), np.uint8)

    black = np.array([0,0,0])
    white = np.array([255,255,255])
    centercolor = np.array([125,125,125])
    #此处为关键,打开图片框
    plt.ion()
    i = 0
    for y in range(0,h-1):
        for x in range(0,w-1):

            down = image1[y+1,x,:]
            right = image1[y,x+1,:]

            here = image1[y,x,:]
            here2 = here
            right2 = right
            down2 = down
            if get_EuclideanDistance(here2,down2) > 16 and get_EuclideanDistance(here2,right2) > 16 :
                image2[y,x,:]=black
            elif get_EuclideanDistance(here2,down2) <=16 and get_EuclideanDistance(here2,right2)<=16:
                image2[y,x,:] = white
            else:
                image2[y,x,:] = centercolor

            i += 1
            #每2000此运算显示一次图片,频率太高影响运算速度
            if i %2000 == 0:
                plt.imshow(image2)
            #这里每次显示0.1秒
                plt.pause(0.1)
print("finish")
plt.show()