基于Python实现 传感器的随机布置 传感网覆盖仿真

代码演示

import tkinter as tk
import random
import win32gui
import cv2
import time
import math
from PIL import Image, ImageGrab

class Window:
    def __init__(self, master):
        self.root = master
        self.createpage()
        self.run()

    def createpage(self):
        self.root.geometry('910x510')
        # 设置窗口是否可以变化长/宽,False不可变,True可变,默认为True
        self.root.resizable(width=False, height=False)

        tk.LabelFrame(self.root, text='|参数|', fg='black', padx=10, pady=10,
                      font='Verdana 10 bold').place(x=10, y=10, height=180, width=290)
        tk.Label(self.root, text='传感器数量:', fg='black').place(x=15, y=45)
        tk.Label(self.root, text='传感器探测半径:', fg='black').place(x=15, y=115)
        # 创建一个输入框,以输入内容
        self.inputnum_text = tk.StringVar()
        self.inputnum = tk.Entry(self.root, textvariable=self.inputnum_text)
        self.inputnum.place(x=120, y=50)
        
        self.inputradius_text = tk.StringVar()
        self.inputradius = tk.Entry(
            self.root, textvariable=self.inputradius_text)
        self.inputradius.place(x=120, y=120)

        tk.LabelFrame(self.root, text='|结果|', fg='black', padx=10, pady=10,
                      font='Verdana 10 bold').place(x=10, y=215, height=180, width=290)
        tk.Label(self.root, text='理论覆盖率(%):', fg='black').place(x=15, y=250)
        tk.Label(self.root, text='实际覆盖率(%):', fg='black').place(x=15, y=320)

        self.theoryoutput = tk.Text(self.root, width=20, height=1)
        self.theoryoutput.place(x=120, y=255)  # 先创建对象再放置位置,否则会报错

        self.actualoutput = tk.Text(self.root, height=1, width=20)
        self.actualoutput.place(x=120, y=325)

        self.cv = tk.Canvas(self.root, bg='white', height=500, width=600)
        self.cv.place(x=300)
        # 创建按钮
        tk.Button(self.root, text='传感器布置', command=self.circle_create).place(
            x=100, y=400, width=100)
        tk.Button(self.root, text='计算', command=self.Calculation_Area).place(
            x=100, y=460, width=100)

    def Wipe_Data(self):
        # 清空文本框内的内容
        self.theoryoutput.delete(0.0, 'end')
        self.actualoutput.delete(0.0, 'end')

    def CaptureScreen(self):
        # print('执行截图函数')
        HWND = win32gui.GetFocus()  # 获取当前窗口句柄
        self.rect = win32gui.GetWindowRect(HWND)  # 获取当前窗口坐标
        x = self.rect[0] + 300
        x1 = x+self.cv.winfo_width()
        y = self.rect[1]
        y1 = y+self.cv.winfo_height()
        image = ImageGrab.grab((x, y, x1, y1))
        image.save("second.jpeg", 'jpeg')  # 前面一个参数是保存路径,后面一个参数是保存格式

    def Calculation_Area(self):
        self.CaptureScreen()
        # print('执行计算函数')
        self.Wipe_Data()
        img = cv2.imread("second.jpeg")  # 图片读取
        pictue_size = img.shape
        picture_height = pictue_size[0]
        picture_width = pictue_size[1]
        # 输出长和宽
        # print(picture_height,picture_width)
        All_area = 304416.0
        Proportion_area_white = 0
        Proportion_area_blue = 0
        for a in range(picture_height):
            for b in range(picture_width):
                if img[a, b].all() > 0:
                    Proportion_area_white = Proportion_area_white + 1

        Proportion_area_blue = float(All_area - Proportion_area_white)
        # 计算实际输出
        # print(Proportion_area_blue)
        Occupancy_actual = Proportion_area_blue / All_area
        Occupancy_actual = ('%.12f' % Occupancy_actual)
        Occupancy_actual = float(Occupancy_actual)
        Occupancy_actual = Occupancy_actual * 100
        # 计算理论输出
        # 𝑅 = 1 − 𝑒^−𝑛𝜋𝑟2/𝐴 理论值计算公式
        i = float(self.sensor_num * math.pi *
                  self.sensor_radius * self.sensor_radius)
        Occupancy_theory = (1 - math.exp((i * -1.0)/All_area))*100
        # 将得到的值输入到文本框里
        self.theoryoutput.insert('end', Occupancy_theory)
        self.actualoutput.insert('end', Occupancy_actual)

    def circle_create(self):
        # print('执行绘图函数')
        # 清空画图的内容
        self.cv.delete(tk.ALL)
        # 获取在参数里输入的值
        self.sensor_num = self.inputnum.get()
        self.sensor_radius = self.inputradius.get()
        # 转int
        self.sensor_num = int(self.sensor_num)
        self.sensor_radius = int(self.sensor_radius)
        # 做循环开始绘图
        for num in range(1, (self.sensor_num + 1)):
            circle_center_x = random.randint(10, 590)
            circle_center_y = random.randint(10, 490)
            self.cv.create_oval((circle_center_x-self.sensor_radius, circle_center_y-self.sensor_radius, 
                                circle_center_x+self.sensor_radius, circle_center_y+self.sensor_radius),
                                outline='blue',
                                fill='blue'
                                )

    def run(self):
        try:
            self.root.mainloop()
        except Exception as e:
            print("*** exception:\n".format(e))


def main():
    window = tk.Tk()
    window.title('SensorHomework Design By HJK')
    Window(window).run()


if __name__ == '__main__':
    main()

输出截图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

原理

1.读取数据,并在画布上显示
2.绘图完成后,使用截图功能,进行截图
3.使用python中的Openv模块对截图进行识别,并计算出面积

  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
第一章 无线传感器络概述 6概述 61.1 NS-2 61.2 OPNET 61.3 SensorSim 71.4 EmStar 71.5 GloMoSim 71.6 TOSSIM 71.7 PowerTOSSIM 8第二章 OMNET++简介 9概述 92.1 OMNeT++框架 92.1.1 OMNeT++组成 92.1.2 OMNeT++结构 102.2 OMNeT++的安装 112.3 OMNeT++语法 122.3.1 NED语言 122.3.1.1 NED总概述 122.3.1.2 Ned描述的组件 132.3.1.3函数 152.3.2 简单模块 172.3.2.1 OMNET++中离散事件 172.3.2.2 包传输模型 172.3.2.3定义简单模块 182.3.2.4 简单模块中的主要成员函数 202.3.3 消息 212.3.3.1 cMessage类 212.3.3.2 消息定义 212.3.3.3 消息的收发 222.3.4 模块参数、门及连接的访问 232.3.4.1消息参数的访问 232.3.4.2门和连接的访问 242.3.4.3门的传输状态 262.3.3.4连接的状态 262.4 仿真过程 272.5 配置文件omnetpp.ini 282.6 结果分析工具 292.6.1 矢量描绘工具Plove 292.6.2 标量工具Scalar 2927、结束语 30第三章 物理层仿真(信道) 323.1 UWB的基础知识 323.1.1 UWB信号的应用背景 323.1.2 UWB信号的定义 323.1.3 UWB的脉冲生成方式(高斯脉冲,非高斯脉冲) 343.1.4 UWB的调制方式 343.1.5 用功率控制多址接入方法来进行链路的建立控制 363.2 用OMNeT++对UWB进行仿真 373.2.1 算法仿真的概述 373.2.2 算法的具体流程 393.2.3 算法的主要代码 413.2.4 仿真结果分析 583.2.5 应用前景 58参考文献 59第四章 MAC层仿真 60概述 604.1 无线传感器络MAC层特性及分类 604.1.1 无线信道特性 604.1.2 MAC 设计特性分析 614.1.3 无线传感器络典型MAC协议的分类 614.2 基于随机竞争的MAC协议 624.2.1 S-MAC协议[12] 624.2.2 T-MAC协议 644.2.3 AC-MAC协议 654.3 基于时分复用的MAC协议 654.3.1 D-MAC协议 654.3.2 TRAMA协议 664.3.3 AI-LMAC协议 664.4 其他类型的MAC协议 674.4.1 SMACS/EAR协议 674.4.2 基于CDMA技术的MAC协议 674.4.3 DCC-MAC 684.5 基于OMNeT++的MAC层协议仿真 694.5.1 S-MAC协议的仿真 694.5.2 S-MAC协议流程图 704.5.3 S-MAC协议的分析 714.6 小结 86参考文献 86第五章 络层仿真 88概述 885.1 无线传感器络路由协议研究 885.1.1 无线传感器络协议分类 885.1.2无线传感器络中平面路由 905.1.3无线传感器络中层次化路由 915.1.4 经典算法的OMNET仿真 935.2 无线传感器络路由协议研究的发展趋势 1045.3 无线传感器络层路由协议与OMNET++仿真 1045.3.1 无线传感器络层路由与OMNET++仿真的基本概念[19] 1045.3.1.1 传感器络的体系结构 1055.3.1.1.1 传感节点的物理结构 1055.3.1.1.2 传感器络的体系结构与络模型 1065.3.2 传感器络层路由协议的基本概念 1065.3.2.1 络通信模式[28] 1065.3.2.1.1 单播: 10

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值