import cv2 import numpy as np import PIL.ImageDraw SCALE = 0.65156853729882650681169151675877 # m/px def add_chinese_text(img, text, position, textColor, textSize): img = PIL.Image.fromarray(img) draw = PIL.ImageDraw.Draw(img) fontStyle = PIL.ImageFont.truetype('simsun.ttc', textSize, encoding='utf-8') draw.text(position, text, textColor, font=fontStyle, stroke_width=1) return np.asarray(img) def main(): # 读取verts with open('verts.txt', 'r', encoding='utf8') as f: verts = f.readlines() verts = list(map(lambda x: x.split(), verts)) verts = list(map(lambda x: [x[0], int(x[1]), int(x[2])], verts)) print(f'{verts=}') # 读取edges with open('edges.txt', 'r', encoding='utf8') as f: edges = f.readlines() edges = list(map(lambda x: x.split(), edges)) edges = list(map(lambda x: [int(x[0]), int(x[1])], edges)) print(f'{edges=}') # 显示地图 im = cv2.imread('map.png') for edge in edges: v1 = verts[edge[0]] # 边关联的点1 v2 = verts[edge[1]] # 边关联的点2 weight = int((((v1[1] - v2[1]) ** 2 + (v1[2] - v2[2]) ** 2) ** 0.5) * SCALE) # 权重为两点欧氏距离 cv2.line(im, (v1[1], v1[2]), (v2[1], v2[2]), (255, 0, 0), 5) # 绘制边的直线 im = add_chinese_text(im, str(weight), ((v1[1] + v2[1]) // 2, (v1[2] + v2[2]) // 2), (255, 0, 255), 30) # 绘制边的权重 for i, vert in enumerate(verts): cv2.circle(im, (vert[1], vert[2]), 20, (255, 0, 0), 2) # 绘制顶点圆圈 im = add_chinese_text(im, str(i), (vert[1] - 15, vert[2] - 15), (255, 0, 255), 30) # 绘制顶点圈中序号数字 im = add_chinese_text(im, vert[0], (vert[1] - 40, vert[2] + 25), (255, 0, 255), 30) # 绘制顶点下方文字 cv2.namedWindow('map', cv2.WINDOW_KEEPRATIO) cv2.imshow('map', im) cv2.waitKey() if __name__ == '__main__': main()
时间: 2023-02-06 20:41:25 浏览: 171
这段代码导入了三个模块:
1. cv2:这是一个用于计算机视觉的库,可以用来处理图像和视频数据。
2. numpy:这是一个用于科学计算的库,主要用于处理数组和矩阵的运算。
3. PIL.ImageDraw:这是 Python Imaging Library (PIL) 中的一个模块,用于在图像上绘图。
SCALE 这个常量的值是一个浮点数,代表一个缩放比例。
相关问题
校园地图服务系统的设计与实现中导入的下列模块import cv2 import numpy as np import PIL.ImageDraw作出300字简述
cv2是一个用于计算机视觉的 Python 库,它提供了大量的图像处理和计算机视觉方法。
numpy 是一个用于 Python 的数学计算库,它提供了大量的数组和矩阵运算方法。
PIL.ImageDraw 是 Python 图像处理库 Pillow 的一个模块,提供了用于在图像上绘制文本、线条、圆形等基本图形的方法。
这些模块都可能会在校园地图服务系统的设计与实现中使用。例如,可能会使用 cv2 来识别图像中的建筑物、道路、景点等关键元素;使用 numpy 来处理地图数据,例如路径规划或者地图变换;使用 PIL.ImageDraw 来在地图上绘制文本、图标等信息,帮助用户理解地图。
import tkinter as tk from tkinter import filedialog from PIL import ImageTk, Image # 创建窗口 window = tk.Tk() window.title("宝石预测") window.geometry("400x400") # 加载模型参数 para_state_dict = paddle.load("MyCNN") model = MyCNN() model.set_state_dict(para_state_dict) model.eval() # 加载标签字典 label_dict = train_parameters['label_dict'] # 创建预测函数 def predict(): # 获取待预测图片路径 img_path = filedialog.askopenfilename() img = Image.open(img_path) # 预处理图片 img = img.resize((224, 224), Image.BILINEAR) img = np.array(img).astype('float32') img = img.transpose((2, 0, 1)) img = img / 255 img = np.array([img]) # 进行预测 img = paddle.to_tensor(img) out = model(img) label = np.argmax(out.numpy()) result = label_dict[str(label)] # 显示预测结果 result_label.config(text="预测结果:{}".format(result)) # 显示待预测图片 img = ImageTk.PhotoImage(Image.open(img_path).resize((200, 200))) img_label.config(image=img) img_label.image = img # 创建选择图片按钮 select_button = tk.Button(window, text="选择图片", command=predict) select_button.pack(pady=20) # 创建待预测图片区域 img_label = tk.Label(window) img_label.pack() # 创建预测结果区域 result_label = tk.Label(window, font=("Helvetica", 16)) result_label.pack(pady=20) # 进入消息循环 window.mainloop()在这段代码的功能中加入使用cv2对选择的图片进行降噪,之后再对上传图片进行识别
好的,你可以使用 OpenCV 库对选择的图片进行降噪,然后将处理后的图片传给预测函数进行识别。下面是修改后的代码:
```
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
import cv2
import numpy as np
# 创建窗口
window = tk.Tk()
window.title("宝石预测")
window.geometry("400x400")
# 加载模型参数
para_state_dict = paddle.load("MyCNN")
model = MyCNN()
model.set_state_dict(para_state_dict)
model.eval()
# 加载标签字典
label_dict = train_parameters['label_dict']
# 创建预测函数
def predict():
# 获取待预测图片路径
img_path = filedialog.askopenfilename()
# 降噪处理
img = cv2.imread(img_path)
img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
# 预处理图片
img = cv2.resize(img, (224, 224))
img = img.transpose((2, 0, 1))
img = img / 255
img = np.array([img])
# 进行预测
img = paddle.to_tensor(img)
out = model(img)
label = np.argmax(out.numpy())
result = label_dict[str(label)]
# 显示预测结果
result_label.config(text="预测结果:{}".format(result))
# 显示待预测图片
img = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)).resize((200, 200)))
img_label.config(image=img)
img_label.image = img
# 创建选择图片按钮
select_button = tk.Button(window, text="选择图片", command=predict)
select_button.pack(pady=20)
# 创建待预测图片区域
img_label = tk.Label(window)
img_label.pack()
# 创建预测结果区域
result_label = tk.Label(window, font=("Helvetica", 16))
result_label.pack(pady=20)
# 进入消息循环
window.mainloop()
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)