python 连通区域检测_Python批量检测连通性

#!/usr/bin/env python3

#author:zhouxia

#date:2016-08-05

import socket

def checkip(ipaddr,port):

try:

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

sock.settimeout(3)

sock.connect((ipaddr,port))

return True

except socket.error as e:

return False

finally:

sock.close()

if __name__ == '__main__':

file = open("ip_list.txt")

checkinfo = open("check_info.txt",'w+')

line = file.readline()

while line:

if line == "":

continue

iplist = line.split(' ')

ipaddr = iplist[0]

port = int(iplist[1])

status = checkip(ipaddr,port)

if status == True:

info = '%s %s is OK' % (ipaddr, port)+'\n'

checkinfo.write(info)

else:

info = '%s %s is Fail' % (ipaddr, port)+'\n'

checkinfo.write(info)

line = file.readline()

file.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Python实现连通区域合并的示例代码: ```python from collections import deque def merge_connected_regions(image): # 获取图像的宽度和高度 height, width = image.shape[:2] # 定义一个二维数组,用于记录每个像素是否已经被访问过 visited = [[False for _ in range(width)] for _ in range(height)] # 定义一个列表,用于存储所有连通区域的像素坐标 connected_regions = [] # 遍历图像的每个像素 for y in range(height): for x in range(width): # 如果当前像素已经被访问过,或者是黑色像素,则跳过 if visited[y][x] or image[y][x] == 0: continue # 定义一个队列,用于存储当前连通区域的像素坐标 queue = deque([(x, y)]) # 定义一个列表,用于存储当前连通区域的像素坐标 connected_region = [] # 循环遍历队列中的像素 while queue: # 取出队列中的第一个像素 x, y = queue.popleft() # 如果当前像素已经被访问过,则跳过 if visited[y][x]: continue # 将当前像素标记为已访问 visited[y][x] = True # 将当前像素添加到当前连通区域中 connected_region.append((x, y)) # 将当前像素的上下左右四个相邻像素加入队列 if x > 0 and not visited[y][x - 1] and image[y][x - 1] == 255: queue.append((x - 1, y)) if x < width - 1 and not visited[y][x + 1] and image[y][x + 1] == 255: queue.append((x + 1, y)) if y > 0 and not visited[y - 1][x] and image[y - 1][x] == 255: queue.append((x, y - 1)) if y < height - 1 and not visited[y + 1][x] and image[y + 1][x] == 255: queue.append((x, y + 1)) # 将当前连通区域添加到连通区域列表中 connected_regions.append(connected_region) # 返回所有连通区域的像素坐标列表 return connected_regions # 示例代码的使用方法 import numpy as np from skimage import io # 读取图像 image = io.imread('test.png', as_gray=True) # 将图像二值化 image = (image < 0.5).astype(np.uint8) * 255 # 合并连通区域 connected_regions = merge_connected_regions(image) # 输出连通区域的数量 print(len(connected_regions)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值