opencv与pyqt6结合例子

文章目录

显示图像

创建一个 PyQt 应用程序,该应用程序能够:

1.使用 OpenCV 加载一张图像。

2.在 PyQt 的窗口中显示这张图像。

3.提供四个按钮(QPushButton):

  • 一个用于将图像转换为灰度图
  • 一个用于将图像恢复为原始彩色图
  • 一个用于将图像进行翻转
  • 一个用于将图像进行旋转
    4.当用户点击按钮时,相应地更新窗口中显示的图像。

代码

导入所需要的库

python 复制代码
import sys
import cv2
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel
from PyQt6.QtGui import QImage, QPixmap

创建窗口类

python 复制代码
class ImageProcessor(QMainWindow):
    def __init__(self):
        super().__init__()
        self.image = cv2.imread('imge.jpg')  # 加载图像
        self.gray_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)  # 转换为灰度图
		#  创建ui
        self.initUI()
    def initUI(self):
        self.setWindowTitle('Image Processor')
        self.setGeometry(100, 100, 800, 600)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout()
        self.central_widget.setLayout(self.layout)

        self.image_label = QLabel()
        self.layout.addWidget(self.image_label)

        self.gray_button = QPushButton('转换为灰度图')
        self.gray_button.clicked.connect(self.convertToGray)
        self.layout.addWidget(self.gray_button)

        self.color_button = QPushButton('恢复为原始彩色图')
        self.color_button.clicked.connect(self.convertToColor)
        self.layout.addWidget(self.color_button)

        self.flip_button = QPushButton('翻转图像')
        self.flip_button.clicked.connect(self.flipImage)
        self.layout.addWidget(self.flip_button)

        self.rotate_button = QPushButton('旋转图像')
        self.rotate_button.clicked.connect(self.rotateImage)
        self.layout.addWidget(self.rotate_button)

        self.updateImageLabel()

    def updateImageLabel(self):
        height, width = self.image.shape[:2]
        bytes_per_line = 3 * width
        q_image = QImage(self.image.data, width, height, bytes_per_line, QImage.Format.Format_RGB888)
        pixmap = QPixmap.fromImage(q_image)
        self.image_label.setPixmap(pixmap)

    def convertToGray(self):
        self.image = self.gray_image
        self.updateImageLabel()

    def convertToColor(self):
        self.image = cv2.cvtColor(self.gray_image, cv2.COLOR_GRAY2BGR)
        self.updateImageLabel()

    def flipImage(self):
        self.image = cv2.flip(self.image, 1)  # 水平翻转
        self.updateImageLabel()

    def rotateImage(self):
        self.image = cv2.rotate(self.image, cv2.ROTATE_90_CLOCKWISE)  # 顺时针旋转90度
        self.updateImageLabel()

调整亮度和对比度

创建一个 PyQt 应用程序,该应用程序能够:

1.使用 OpenCV 加载一张彩色图像,并在 PyQt 的窗口中显示它。

2.提供一个滑动条(QSlider),允许用户调整图像的亮度。

3.当用户调整滑动条时,实时更新窗口中显示的图像亮度。

4.添加另一个滑动条(QSlider),允许用户调整图像的对比度。

5.当用户调整滚动条时,实时更新窗口中显示的图像对比度。

6.提供一个按钮(QPushButton),允许用户将图像保存为新的文件。

7.当用户点击保存按钮时,将调整后的图像保存到指定的路径,OpenCV中使用cv2.imwrite()来保存图片。

代码

python 复制代码
import sys
import cv2
from PyQt6.QtWidgets import QApplication, QMainWindow, QSlider, QVBoxLayout, QWidget, QPushButton, QLabel
from PyQt6.QtGui import QImage, QPixmap
from PyQt6.QtCore import Qt

class ImageProcessor(QMainWindow):
    def __init__(self):
        super().__init__()

        self.image = cv2.imread('imge.jpg')  # 加载图像
        self.brightness = 0
        self.contrast = 1

        self.initUI()

    def initUI(self):
        self.setWindowTitle('Image Processor')
        self.setGeometry(100, 100, 800, 600)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout()
        self.central_widget.setLayout(self.layout)

        self.image_label = QLabel()
        self.layout.addWidget(self.image_label)

        self.brightness_slider = QSlider()
        self.brightness_slider.setOrientation(Qt.Orientation.Vertical)
        self.brightness_slider.setMinimum(-100)
        self.brightness_slider.setMaximum(100)
        self.brightness_slider.setValue(0)
        self.brightness_slider.valueChanged.connect(self.updateBrightness)
        self.layout.addWidget(self.brightness_slider)

        self.contrast_slider = QSlider()
        self.contrast_slider.setOrientation(Qt.Orientation.Vertical)
        self.contrast_slider.setMinimum(1)
        self.contrast_slider.setMaximum(10)
        self.contrast_slider.setValue(1)
        self.contrast_slider.valueChanged.connect(self.updateContrast)
        self.layout.addWidget(self.contrast_slider)

        self.save_button = QPushButton('保存图像')
        self.save_button.clicked.connect(self.saveImage)
        self.layout.addWidget(self.save_button)

        self.updateImageLabel()

    def updateImageLabel(self):
        # 调整图像亮度和对比度
        adjusted_image = cv2.convertScaleAbs(self.image, alpha=self.contrast, beta=self.brightness)
        adjusted_image = cv2.cvtColor(adjusted_image, cv2.COLOR_BGR2RGB)

        # 将 OpenCV 图像转换为 QImage
        height, width, channel = adjusted_image.shape
        bytes_per_line = 3 * width
        q_image = QImage(adjusted_image.data, width, height, bytes_per_line, QImage.Format.Format_RGB888)

        # 将 QImage 转换为 QPixmap
        pixmap = QPixmap.fromImage(q_image)

        # 在 QLabel 上显示 QPixmap
        self.image_label.setPixmap(pixmap)

    def updateBrightness(self, value):
        self.brightness = value
        self.updateImageLabel()

    def updateContrast(self, value):
        self.contrast = value
        self.updateImageLabel()

    def saveImage(self):
        # 保存调整后的图像
        adjusted_image = cv2.convertScaleAbs(self.image, alpha=self.contrast, beta=self.brightness)
        cv2.imwrite('adjusted_image.jpg', adjusted_image)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageProcessor()
    window.show()
    sys.exit(app.exec())

图像变换

创建一个 PyQt 应用程序,该应用程序能够:

1.使用 OpenCV 加载一张图像。

2.在 PyQt 的窗口中显示这张图像。

3.提供一个下拉列表(QComboBox),对图像做(模糊、锐化、边缘检测)处理:

  • 模糊------使用cv2.GaussianBlur()实现
  • 锐化------使用cv2.Laplacian()、cv2.Sobel()实现
  • 边缘检测------使用cv2.Canny()实现
    4.当用户点击下拉列表选项时,相应地更新窗口中显示的图像。
    5.提供一个按钮,当用户点击按钮时,能保存调整后的图像。

代码

python 复制代码
import sys
import cv2
from PyQt6.QtWidgets import QApplication, QMainWindow, QComboBox, QVBoxLayout, QWidget, QPushButton, QLabel
from PyQt6.QtGui import QImage, QPixmap

class ImageProcessor(QMainWindow):
    def __init__(self):
        super().__init__()

        self.image = cv2.imread('imge.jpg')  # 加载图像

        self.initUI()

    def initUI(self):
        self.setWindowTitle('Image Processor')
        self.setGeometry(100, 100, 800, 600)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout()
        self.central_widget.setLayout(self.layout)

        self.image_label = QLabel()
        self.layout.addWidget(self.image_label)

        self.processing_combobox = QComboBox()
        self.processing_combobox.addItems(['模糊', '锐化', '边缘检测'])
        self.processing_combobox.currentIndexChanged.connect(self.updateImage)
        self.layout.addWidget(self.processing_combobox)

        self.save_button = QPushButton('保存图像')
        self.save_button.clicked.connect(self.saveImage)
        self.layout.addWidget(self.save_button)

        self.updateImageLabel()

    def updateImageLabel(self):
        # 根据下拉列表选项处理图像
        processing_option = self.processing_combobox.currentText()
        if processing_option == '模糊':
            processed_image = cv2.GaussianBlur(self.image, (5, 5), 0)
        elif processing_option == '锐化':
            processed_image = cv2.Laplacian(self.image, cv2.CV_64F)
            processed_image = cv2.convertScaleAbs(processed_image)
        elif processing_option == '边缘检测':
            processed_image = cv2.Canny(self.image, 100, 200)

        # 将 OpenCV 图像转换为 QImage
        processed_image=cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
        height, width, channel = processed_image.shape
        bytes_per_line = 3 * width
        q_image = QImage(processed_image.data, width, height, bytes_per_line, QImage.Format.Format_RGB888)

        # 将 QImage 转换为 QPixmap
        pixmap = QPixmap.fromImage(q_image)

        # 在 QLabel 上显示 QPixmap
        self.image_label.setPixmap(pixmap)

    def updateImage(self, index):
        self.updateImageLabel()

    def saveImage(self):
        # 保存调整后的图像
        processing_option = self.processing_combobox.currentText()
        if processing_option == '模糊':
            processed_image = cv2.GaussianBlur(self.image, (5, 5), 0)
        elif processing_option == '锐化':
            processed_image = cv2.Laplacian(self.image, cv2.CV_64F)
            processed_image = cv2.convertScaleAbs(processed_image)
        elif processing_option == '边缘检测':
            processed_image = cv2.Canny(self.image, 100, 200)

        cv2.imwrite('processed_image.jpg', processed_image)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageProcessor()
    window.show()
    sys.exit(app.exec())
相关推荐
lskisme2 分钟前
springboot maven导入本地jar包
开发语言·python·pycharm
开心-开心急了22 分钟前
pyside6实现win10自动切换主题
开发语言·python·pyqt·pyside
mortimer1 小时前
一键实现人声伴奏分离:基于 `uv`, `FFmpeg` 和 `audio-separator` 的高效解决方案
python·ffmpeg·音视频开发
Sunhen_Qiletian1 小时前
Python 类继承详解:深度学习神经网络架构的构建艺术
python·深度学习·神经网络
俊俊谢2 小时前
OpenCV环境配置(QT 6.6.1 MSVC2019 64bit + OpenCV – 4.12.0)
qt·opencv·msvc
程序员大雄学编程2 小时前
用Python来学微积分34-定积分的基本性质及其应用
开发语言·python·数学·微积分
却道天凉_好个秋2 小时前
OpenCV(十六):椭圆的绘制
opencv·计算机视觉
Q_Q5110082852 小时前
python+django/flask的莱元元电商数据分析系统_电商销量预测
spring boot·python·django·flask·node.js·php
林一百二十八2 小时前
Python实现手写数字识别
开发语言·python
Q26433650233 小时前
【有源码】基于Hadoop+Spark的起点小说网大数据可视化分析系统-基于Python大数据生态的网络文学数据挖掘与可视化系统
大数据·hadoop·python·信息可视化·数据分析·spark·毕业设计