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())
相关推荐
_一条咸鱼_42 分钟前
Python 数据类型之可变与不可变类型详解(十)
人工智能·python·面试
_一条咸鱼_42 分钟前
Python 入门之基本运算符(六)
python·深度学习·面试
_一条咸鱼_1 小时前
Python 流程控制之 for 循环(九)
人工智能·python·面试
_一条咸鱼_1 小时前
Python 语法入门之流程控制 if 判断(七)
人工智能·python·面试
_一条咸鱼_1 小时前
Python 流程控制之 while 循环(八)
人工智能·python·面试
HtwHUAT1 小时前
实验四 Java图形界面与事件处理
开发语言·前端·python
Tech Synapse1 小时前
基于Surprise和Flask构建个性化电影推荐系统:从算法到全栈实现
python·算法·flask·协同过滤算法
麦麦大数据1 小时前
vue+flask+CNN电影推荐系统
pytorch·python·cnn·flask·scikit-learn·电影推荐
腾飞开源1 小时前
02_Flask是什么?
python·flask·python web开发·flask快速入门教程·人人都能学·小白看得懂学得会·跟我学编程
終不似少年遊*1 小时前
国产之光DeepSeek架构理解与应用分析04
人工智能·python·深度学习·算法·大模型·ds