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())
相关推荐
(ღ星辰ღ)9 分钟前
js应用opencv
开发语言·javascript·opencv
忧陌60621 分钟前
Day22打卡-复习
python
Sylvan Ding1 小时前
PyTorch Lightning实战 - 训练 MNIST 数据集
人工智能·pytorch·python·lightning
水银嘻嘻2 小时前
web 自动化之 Unittest 应用:报告&装饰器&断言
前端·python·自动化
攻城狮7号2 小时前
Python爬虫第20节-使用 Selenium 爬取小米商城空调商品
开发语言·数据库·爬虫·python·selenium
虚空之月&&轮舞者4 小时前
Python与矢量网络分析仪3671E:自动化测试(Vscode)
网络·vscode·python·射频工程
李昊哲小课4 小时前
tensorflow-cpu
大数据·人工智能·python·深度学习·数据分析·tensorflow
小彭律师9 小时前
数字化工厂中央控制室驾驶舱系统架构文档
python
old_power10 小时前
【Python】PDF文件处理(PyPDF2、borb、fitz)
python·pdf
测试开发Kevin11 小时前
从投入产出、效率、上手难易度等角度综合对比 pytest 和 unittest 框架
python·pytest