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 相关文章推荐
php使用递归与迭代实现快速排序示例
Jan 23 Python
连接Python程序与MySQL的教程
Apr 29 Python
介绍Python的Urllib库的一些高级用法
Apr 30 Python
Python基于select实现的socket服务器
Apr 13 Python
python连接数据库的方法
Oct 19 Python
Python爬虫之网页图片抓取的方法
Jul 16 Python
Python中一般处理中文的几种方法
Mar 06 Python
对python 中class与变量的使用方法详解
Jun 26 Python
python实现车牌识别的示例代码
Aug 05 Python
在Tensorflow中实现leakyRelu操作详解(高效)
Jun 30 Python
Python如何利用Har文件进行遍历指定字典替换提交的数据详解
Nov 05 Python
Python制作一个随机抽奖小工具的实现
Jul 07 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
百事可乐也出咖啡了 双倍咖啡因双倍快乐
2021/03/03 咖啡文化
PHP 读取和编写 XML
2014/11/19 PHP
PHP使用NuSOAP调用Web服务的方法
2015/07/18 PHP
PHP使用curl模拟post上传及接收文件的方法
2016/03/04 PHP
thinkPHP5.1框架路由::get、post请求简单用法示例
2019/05/06 PHP
PHP程序员简单的开展服务治理架构操作详解(一)
2020/05/14 PHP
获取css样式表内样式的js函数currentStyle(IE),defaultView(FF)
2011/02/14 Javascript
简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
2013/10/22 Javascript
jquery $.fn $.fx是什么意思有什么用
2013/11/04 Javascript
JS实现定时页面弹出类似QQ新闻的提示框
2013/11/07 Javascript
Javascript冒泡排序算法详解
2014/12/03 Javascript
JavaScript 事件对象介绍
2015/04/13 Javascript
JQuery datepicker 用法详解
2015/12/25 Javascript
javascript执行环境及作用域详解
2016/05/05 Javascript
BootStrap 智能表单实战系列(十)自动完成组件的支持
2016/06/13 Javascript
Bootstrap 最常用的JS插件系列总结(图片轮播、标签切换等)
2016/07/14 Javascript
javascrpt密码强度校验函数详解
2020/03/18 Javascript
vue scroll滚动判断的实现(是否滚动到底部、滚动方向、滚动节流、获取滚动区域dom元素)
2020/06/11 Javascript
JavaScript快速调试的两个技巧
2020/11/04 Javascript
Python中关键字is与==的区别简述
2014/07/31 Python
Python的socket模块源码中的一些实现要点分析
2016/06/06 Python
python3中set(集合)的语法总结分享
2017/03/24 Python
浅谈Pandas中map, applymap and apply的区别
2018/04/10 Python
python多进程(加入进程池)操作常见案例
2019/10/21 Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧(自定义信号与槽)详解与实例
2020/03/06 Python
tensorflow转换ckpt为savermodel模型的实现
2020/05/25 Python
python 动态渲染 mysql 配置文件的示例
2020/11/20 Python
python爬虫scrapy图书分类实例讲解
2020/11/23 Python
YOINS官网:时尚女装网上购物
2017/03/17 全球购物
英国豪华家具和经典家居饰品购物网站:OKA
2020/06/05 全球购物
Windows和Linux动态库应用异同
2016/07/28 面试题
2014第二批党的群众路线教育实践活动对照检查材料思想汇报
2014/09/18 职场文书
大学生迟到检讨书500字
2014/10/17 职场文书
保安辞职信范文
2015/02/28 职场文书
活动宣传稿范文
2015/07/23 职场文书
《社戏》教学反思
2016/02/22 职场文书