python pyqt5学习笔记 2020.9.29(2)(图片半透明叠加,以此为基础实现半透明涂鸦标注和橡皮擦)

25 篇文章 2 订阅

图片叠加

找一张图片:
在这里插入图片描述
根据图片大小,创建一张相同大小的纯黑色的图片。

import cv2.cv2 as cv2
import numpy as np

img1 = cv2.imread("C:/Users/Administrator/Desktop/1/1.jpg")
img3 = np.zeros((img1.shape), dtype=np.uint8)
cv2.imwrite("C:/Users/Administrator/Desktop/2/3.jpg", img3)

使用画图工具打开,话两个有色矩形上去:
在这里插入图片描述

然后使用addWeighted将两张图片融合到一起:

import cv2.cv2 as cv2
import numpy as np

img1 = cv2.imread("C:/Users/Administrator/Desktop/1/1.jpg")
img2 = cv2.imread("C:/Users/Administrator/Desktop/2/3.jpg")
#img3 = np.zeros((img1.shape), dtype=np.uint8)
#cv2.imwrite("C:/Users/Administrator/Desktop/2/3.jpg", img3)
img3 = cv2.addWeighted(img1, 1, img2, 0.9, 1)
cv2.imwrite("C:/Users/Administrator/Desktop/2/4.jpg", img3)
cv2.imshow("ss",img3)
cv2.waitKey(0)

在这里插入图片描述
其中cv2.addWeighted(img1, 1, img2, 0.9, 1)
表示img1* 1+ img2* 0.9+ 1

应用于标注软件

有了以上基础,就可以解决我写的涂鸦标注软件中的,涂鸦不透明的问题了,只要再新建一个纯黑色的画布,然后将纯黑色的画布与原图进行addWeighted加权,再显示到label中,就实现了半透明涂鸦式标注。
新增代码

#读取图片时,加载黑色画布:
self.black_board = np.zeros((self.cur_img.shape), dtype=np.uint8)       #黑色画布
#鼠标移动时,把原图上画圆画直线的代码去掉,增加在黑色画布上的代码
cv2.circle(self.black_board, (int((self.x1-5)/self.ui.lflabel1.width()*self.cur_lab_shape[1]), int((self.y1-100)/self.ui.lflabel1.height()*self.cur_lab_shape[0])), self.radius*self.pen_times, self.pen_color(), -1)
cv2.line(self.black_board, self.start_point, (int((self.x1-5)/self.ui.lflabel1.width()*self.cur_lab_shape[1]), int((self.y1-100)/self.ui.lflabel1.height()*self.cur_lab_shape[0])), self.pen_color(), 2*self.pen_times*self.radius, 4)
#黑色幕布加权到原图上
img_add = cv2.addWeighted(self.cur_img, 1, self.black_board, 0.9, 0)

然后显示出来就行。

在这里插入图片描述

橡皮擦

有意思的是,采用黑色画布来使标注半透明时,如果画笔是黑色,就可以将原先标注的擦掉了,但是由于我的程序出口是输出一个白色幕布的包含标注的图像,白色幕布上就会被涂上黑色。所以我加个判断,把黑色作为橡皮,如果涂的黑色,那白色幕布上就涂上白色就行了。
在这里插入图片描述
得到的白色幕布:
在这里插入图片描述
做一下修改:

#修改橡皮为黑色画笔
def set_rubber(self):            #设置橡皮擦
    pic=QtGui.QPixmap("pic/rubberlabel.png")
    pic = pic.scaled(25,25)
    self.ui.lflabel1.setCursor(QtGui.QCursor(pic))
    self.pen_clor = "black"
    self.cur_wid = "pen"

#读取到是黑色画笔时,就将白色画布的画笔改为白色画笔
if self.pen_clor == "black":
    cv2.circle(self.draw_board, (int((self.x1-5)/self.ui.lflabel1.width()*self.cur_lab_shape[1]), int((self.y1-100)/self.ui.lflabel1.height()*self.cur_lab_shape[0])), self.radius*self.pen_times, (255,255,255), -1)
else:
    cv2.circle(self.draw_board, (int((self.x1-5)/self.ui.lflabel1.width()*self.cur_lab_shape[1]), int((self.y1-100)/self.ui.lflabel1.height()*self.cur_lab_shape[0])), self.radius*self.pen_times, self.pen_color(), -1)                           
                                                

用橡皮擦一下:
在这里插入图片描述
生成的标注图:
在这里插入图片描述
解决!

至此,软件中的标注功能就结束了,接下来有时间就会开始做深度学习模型训练部分

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个简单的示例代码,使用 PyQT5 和 MySQL 实现注册和登录功能: ```python import mysql.connector from PyQt5 import QtWidgets, uic from PyQt5.QtWidgets import QMessageBox # 连接 MySQL 数据库 mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) # 注册函数 def register(username, password): mycursor = mydb.cursor() sql = "INSERT INTO users (username, password) VALUES (%s, %s)" val = (username, password) mycursor.execute(sql, val) mydb.commit() QMessageBox.information(None, "Registration Successful", "You have successfully registered!") # 登录函数 def login(username, password): mycursor = mydb.cursor() sql = "SELECT * FROM users WHERE username = %s AND password = %s" val = (username, password) mycursor.execute(sql, val) result = mycursor.fetchone() if result: QMessageBox.information(None, "Login Successful", "You have successfully logged in!") else: QMessageBox.warning(None, "Login Failed", "Invalid username or password.") # 创建 GUI app = QtWidgets.QApplication([]) dlg = uic.loadUi("login.ui") # 注册按钮点击事件 def register_clicked(): username = dlg.register_username.text() password = dlg.register_password.text() register(username, password) # 登录按钮点击事件 def login_clicked(): username = dlg.login_username.text() password = dlg.login_password.text() login(username, password) dlg.register_button.clicked.connect(register_clicked) dlg.login_button.clicked.connect(login_clicked) dlg.show() app.exec() ``` 需要注意的是,上述代码中的 `mydatabase` 应该替换为你自己的数据库名称,而 `yourusername` 和 `yourpassword` 则应该替换为你的 MySQL 用户名和密码。此外,还需要在 MySQL 中创建一个名为 `users` 的表,包含 `username` 和 `password` 两个字段。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值