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())
相关推荐
伊一大数据&人工智能学习日志21 分钟前
自然语言处理NLP 04案例——苏宁易购优质评论与差评分析
人工智能·python·机器学习·自然语言处理·数据挖掘
刀客12326 分钟前
python3+TensorFlow 2.x(六)自编码器
人工智能·python·tensorflow
微刻时光27 分钟前
影刀RPA中级证书-Excel进阶-开票清单
经验分享·python·低代码·rpa·影刀·影刀证书·影刀实战
一朵小花43 分钟前
Python中with的用法
python
m0_748232391 小时前
基于OpenCV和Python的人脸识别系统_django
python·opencv·django
dme.2 小时前
Python爬虫selenium验证-中文识别点选+图片验证码案例
爬虫·python
东方-教育技术博主2 小时前
wps中zotero插件消失,解决每次都需要重新开问题
python
镰圈量化2 小时前
当电脑上有几个python版本Vscode选择特定版本python
开发语言·vscode·python
宇努力学习2 小时前
如何本地部署seepseek
python·ai·ollama·deepseek
橙狮科技2 小时前
使用 GPTQ 进行 4 位 LLM 量化
人工智能·python·语言模型