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())
相关推荐
Suc_zhan2 分钟前
实验二 如何将随机森林算法应用于激酶抑制剂分类任务
python·算法·随机森林·机器学习
Wwwilling6 分钟前
docker中怎么把docker中的python package转成conda,目前还没有conda
python·docker·conda
ak啊7 分钟前
Python文件/文件夹复制功能
python
ak啊8 分钟前
Python 操作 Linux 命令行
python
AI是这个时代的魔法15 分钟前
Using Dyck Path to solve a leetcode puzzle
python·算法·leetcode
__淡墨青衫__1 小时前
Django之旅:第六节--mysql数据库操作增删改查(二)
后端·python·django
无影无踪的青蛙1 小时前
[Python] 贪心算法简单版
开发语言·python·贪心算法
aiweker1 小时前
Python PDF解析利器:pdfplumber | AI应用开发
python·pdf
Fuction.1 小时前
聚类注意点
人工智能·python·机器学习·kmeans·聚类
xc10111 小时前
树莓派5从零开发至脱机脚本运行教程——6.实战篇
人工智能·opencv·计算机视觉