opencv+pyQt5实现图片阈值编辑器/寻色块阈值利器


Posted in Python onNovember 13, 2020

定位色块常用到hsv色彩空间下的颜色阈值,笔者曾经用openmv时,其IDE有自带一个阈值编辑器,使用起来非常方便,现在在linux上跑cv算法,需要类似的功能,因此自己写了一个阈值编辑器。 目前暂时只支持HSV色彩空间下的阈值编辑。

代码:

mian.py:

from threshold_ui import Ui_Widget
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QImage,QPixmap,QFont
from PyQt5 import QtWidgets
import sys
import cv2
import numpy as np

class Threshold_Value_Edit(QtWidgets.QWidget,Ui_Widget):
  def __init__(self): #UI初始化
    super(Threshold_Value_Edit,self).__init__()
    self.setupUi(self)
    self.setWindowTitle("阈值编辑器")
    img_path = r'D:\Pictures\screenshot.png'
    ft=QFont()
    ft.setPointSize(12)

    self.horizontalSlider.setMaximum(255)
    self.horizontalSlider_2.setMaximum(255)
    self.horizontalSlider_3.setMaximum(255)
    self.horizontalSlider_4.setMaximum(255)
    self.horizontalSlider_5.setMaximum(255)
    self.horizontalSlider_6.setMaximum(255)

    self.horizontalSlider.setValue(255)
    self.horizontalSlider_3.setValue(255)
    self.horizontalSlider_5.setValue(255)
    self.label.setText("H最大值:"+str(self.horizontalSlider.value()))
    self.label_2.setText("H最小值:"+str(self.horizontalSlider_2.value()))
    self.label_3.setText("S最大值:"+str(self.horizontalSlider_3.value()))
    self.label_4.setText("S最小值:"+str(self.horizontalSlider_4.value()))
    self.label_5.setText("V最大值:"+str(self.horizontalSlider_5.value()))
    self.label_6.setText("V最小值:"+str(self.horizontalSlider_6.value()))
    self.label.setFont(ft)
    self.label_2.setFont(ft)
    self.label_3.setFont(ft)
    self.label_4.setFont(ft)
    self.label_5.setFont(ft)
    self.label_6.setFont(ft)
    self.lineEdit.setText("HSVmin: HSVmax:")
    self.lineEdit.setFont(ft)
    
    self.img = cv2.imread(img_path)
    self.label_image_show(self.img)

    self.horizontalSlider.valueChanged[int].connect(self.horizontalSlider1_changeValue)
    self.horizontalSlider_2.valueChanged[int].connect(self.horizontalSlider2_changeValue)
    self.horizontalSlider_3.valueChanged[int].connect(self.horizontalSlider3_changeValue)
    self.horizontalSlider_4.valueChanged[int].connect(self.horizontalSlider4_changeValue)
    self.horizontalSlider_5.valueChanged[int].connect(self.horizontalSlider5_changeValue)
    self.horizontalSlider_6.valueChanged[int].connect(self.horizontalSlider6_changeValue)

  def label_image_show(self,img):
    if(len(img.shape)==2):
      img = cv2.merge([img,img,img])

    height, width, bytesPerComponent = img.shape  #返回的是图像的行数,列数,色彩通道数
    bytesPerLine = 3 * width  #每行的字节数    
    cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
    QImg = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888)     
    pixmap = QPixmap.fromImage(QImg)
    self.label_7.setPixmap(pixmap)
    #print(img.shape[0],img.shape[1])
    self.label_7.update()




  def horizontalSlider1_changeValue(self): #更新画板和滑条前的label
    self.label.setText("H最大值:"+str(self.horizontalSlider.value()))
    self.change_lineedit_value()
    self.HSV_img_change()
  def horizontalSlider2_changeValue(self):
    self.label_2.setText("H最小值:"+str(self.horizontalSlider_2.value()))
    self.change_lineedit_value()
    self.HSV_img_change()
  def horizontalSlider3_changeValue(self):
    self.label_3.setText("S最大值:"+str(self.horizontalSlider_3.value()))
    self.change_lineedit_value()
    self.HSV_img_change()
  def horizontalSlider4_changeValue(self):
    self.label_4.setText("S最小值:"+str(self.horizontalSlider_4.value()))
    self.change_lineedit_value()
    self.HSV_img_change()
  def horizontalSlider5_changeValue(self):
    self.label_5.setText("V最大值:"+str(self.horizontalSlider_5.value()))
    self.change_lineedit_value()
    self.HSV_img_change()
  def horizontalSlider6_changeValue(self):
    self.label_6.setText("V最小值:"+str(self.horizontalSlider_6.value()))
    self.change_lineedit_value()
    self.HSV_img_change()


  def change_lineedit_value(self):
    self.lineEdit.setText("HSVmin:["+str(self.horizontalSlider_2.value())+","+str(self.horizontalSlider_4.value())+","+str(self.horizontalSlider_6.value())+"] HSVmax:["+str(self.horizontalSlider.value())+","+str(self.horizontalSlider_3.value())+","+str(int(self.horizontalSlider_5.value()))+"]")

  def HSV_img_change(self):
    
    hsv_min = np.array([self.horizontalSlider_2.value(),self.horizontalSlider_4.value(),self.horizontalSlider_6.value()])
    hsv_max = np.array([self.horizontalSlider.value(),self.horizontalSlider_3.value(),self.horizontalSlider_5.value()])
    hsv = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV)         # 转化成HSV图像
    erode_hsv = cv2.erode(hsv, None, iterations=8)          # 腐蚀 细的变粗
    inRange_hsv = cv2.inRange(erode_hsv,hsv_min,hsv_max)
    self.label_image_show(inRange_hsv)
    print("hsv_min:")
    print(hsv_min)
    print("hsv_max:")
    print(hsv_max)    
    


if __name__ == '__main__':
  app = QtWidgets.QApplication(sys.argv)
  w = Threshold_Value_Edit()
  w.__init__()
  w.show()
  sys.exit(app.exec_())

threshold_ui.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'widget.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Widget(object):
  def setupUi(self, Widget):
    Widget.setObjectName("Widget")
    Widget.resize(942, 247)
    self.horizontalLayout = QtWidgets.QHBoxLayout(Widget)
    self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
    self.horizontalLayout.setSpacing(6)
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.label_7 = QtWidgets.QLabel(Widget)
    self.label_7.setObjectName("label_7")
    self.horizontalLayout.addWidget(self.label_7)
    self.formLayout = QtWidgets.QFormLayout()
    self.formLayout.setSpacing(6)
    self.formLayout.setObjectName("formLayout")
    self.label = QtWidgets.QLabel(Widget)
    self.label.setObjectName("label")
    self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
    self.horizontalSlider = QtWidgets.QSlider(Widget)
    self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider.setObjectName("horizontalSlider")
    self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.horizontalSlider)
    self.label_2 = QtWidgets.QLabel(Widget)
    self.label_2.setObjectName("label_2")
    self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
    self.horizontalSlider_2 = QtWidgets.QSlider(Widget)
    self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider_2.setObjectName("horizontalSlider_2")
    self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.horizontalSlider_2)
    self.label_3 = QtWidgets.QLabel(Widget)
    self.label_3.setObjectName("label_3")
    self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
    self.horizontalSlider_3 = QtWidgets.QSlider(Widget)
    self.horizontalSlider_3.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider_3.setObjectName("horizontalSlider_3")
    self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.horizontalSlider_3)
    self.label_4 = QtWidgets.QLabel(Widget)
    self.label_4.setObjectName("label_4")
    self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_4)
    self.horizontalSlider_4 = QtWidgets.QSlider(Widget)
    self.horizontalSlider_4.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider_4.setObjectName("horizontalSlider_4")
    self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.horizontalSlider_4)
    self.label_5 = QtWidgets.QLabel(Widget)
    self.label_5.setObjectName("label_5")
    self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_5)
    self.horizontalSlider_5 = QtWidgets.QSlider(Widget)
    self.horizontalSlider_5.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider_5.setObjectName("horizontalSlider_5")
    self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.horizontalSlider_5)
    self.label_6 = QtWidgets.QLabel(Widget)
    self.label_6.setObjectName("label_6")
    self.formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.label_6)
    self.horizontalSlider_6 = QtWidgets.QSlider(Widget)
    self.horizontalSlider_6.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider_6.setObjectName("horizontalSlider_6")
    self.formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.horizontalSlider_6)
    self.lineEdit = QtWidgets.QLineEdit(Widget)
    self.lineEdit.setObjectName("lineEdit")
    self.formLayout.setWidget(6, QtWidgets.QFormLayout.SpanningRole, self.lineEdit)
    self.horizontalLayout.addLayout(self.formLayout)

    self.retranslateUi(Widget)
    QtCore.QMetaObject.connectSlotsByName(Widget)

  def retranslateUi(self, Widget):
    _translate = QtCore.QCoreApplication.translate
    Widget.setWindowTitle(_translate("Widget", "Widget"))
    self.label_7.setText(_translate("Widget", "TextLabel"))
    self.label.setText(_translate("Widget", "H最大值"))
    self.label_2.setText(_translate("Widget", "H最小值"))
    self.label_3.setText(_translate("Widget", "S最大值"))
    self.label_4.setText(_translate("Widget", "S最小值"))
    self.label_5.setText(_translate("Widget", "V最大值"))
    self.label_6.setText(_translate("Widget", "V最小值"))

第二份代码保存为threshold_ui.py,第一份可保存为任意文件名.py,放到同一目录下,运行第一份代码即可,img_path需修改为目标图片的绝对路径。

程序界面截图:

opencv+pyQt5实现图片阈值编辑器/寻色块阈值利器

阈值选取:

opencv+pyQt5实现图片阈值编辑器/寻色块阈值利器

到此这篇关于opencv+pyQt5实现图片阈值编辑器/寻色块阈值利器的文章就介绍到这了,更多相关pyQt5 图片阈值编辑器内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python中使用sys模板和logging模块获取行号和函数名的方法
Apr 15 Python
python实现批量下载新浪博客的方法
Jun 15 Python
Python中的urllib模块使用详解
Jul 07 Python
python开发中range()函数用法实例分析
Nov 12 Python
Python中的字符串类型基本知识学习教程
Feb 04 Python
Python Requests安装与简单运用
Apr 07 Python
Windows下Python3.6安装第三方模块的方法
Nov 22 Python
Python lambda表达式用法实例分析
Dec 25 Python
python解析含有重复key的json方法
Jan 22 Python
对Python3 pyc 文件的使用详解
Feb 16 Python
Python发送邮件的实例代码讲解
Oct 16 Python
关于Python字符编码与二进制不得不说的一些事
Oct 04 Python
Python图像识别+KNN求解数独的实现
Nov 13 #Python
Django正则URL匹配实现流程解析
Nov 13 #Python
Django框架请求生命周期实现原理
Nov 13 #Python
python在地图上画比例的实例详解
Nov 13 #Python
python语言实现贪吃蛇游戏
Nov 13 #Python
Python使用struct处理二进制(pack和unpack用法)
Nov 12 #Python
python切割图片的示例
Nov 12 #Python
You might like
从零开始的异世界生活:第二季延期后,B站上架了第二部剧场版
2020/05/06 日漫
Yii CGridView用法实例详解
2016/07/12 PHP
PHP对称加密函数实现数据的加密解密
2016/10/27 PHP
PHP实现json_decode不转义中文的方法
2017/05/20 PHP
laravel dingo API返回自定义错误信息的实例
2019/09/29 PHP
解决Laravel 使用insert插入数据,字段created_at为0000的问题
2019/10/11 PHP
THINKPHP5分页数据对象处理过程解析
2020/10/28 PHP
javascript 二进制运算技巧解析
2012/11/27 Javascript
利用JavaScript检测CPU使用率自己写的
2014/03/22 Javascript
jquery中push()的用法(数组添加元素)
2014/11/25 Javascript
JavaScript实现通过select标签跳转网页的方法
2016/09/29 Javascript
Kendo Grid editing 自定义验证报错提示的解决方法
2016/11/18 Javascript
React-router 4 按需加载的实现方式及原理详解
2017/05/25 Javascript
详解基于webpack搭建react运行环境
2017/06/01 Javascript
jQuery实现的页面遮罩层功能示例【测试可用】
2017/10/14 jQuery
Vue父组件调用子组件事件方法
2018/02/23 Javascript
JS实现显示当前日期的实例代码
2018/07/03 Javascript
Postman的下载及安装教程详解
2018/10/16 Javascript
小程序实现授权登陆的解决方案
2018/12/02 Javascript
详解mpvue中使用vant时需要注意的onChange事件的坑
2019/05/16 Javascript
[01:01:43]EG vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.24
2018/08/25 DOTA
Python多线程结合队列下载百度音乐的方法
2015/07/27 Python
Django数据库表反向生成实例解析
2018/02/06 Python
Python最小二乘法矩阵
2019/01/02 Python
python里运用私有属性和方法总结
2019/07/08 Python
ML神器:sklearn的快速使用及入门
2019/07/11 Python
Form表单及django的form表单的补充
2019/07/25 Python
django多文件上传,form提交,多对多外键保存的实例
2019/08/06 Python
Python接口测试结果集实现封装比较
2020/05/01 Python
CSS3弹性盒模型开发笔记(三)
2016/04/26 HTML / CSS
CSS3的transition和animation的用法实例介绍
2014/08/20 HTML / CSS
美国著名的婴儿学步鞋老品牌:Robeez
2016/08/20 全球购物
大学毕业通用个人的求职信
2013/12/08 职场文书
校园文明倡议书
2014/05/16 职场文书
入党积极分子半年考察意见
2015/06/02 职场文书
企业愿景口号
2015/12/25 职场文书