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

本文通过计算像素点与其相邻像素点之间的欧氏距离来实现边缘检测,并使用Python和OpenCV库进行图像处理。该方法通过设定阈值区分边缘和非边缘区域,最终实现了对输入图像的简化表示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值