Python + OpenCV颜色空间(Color Space)

Python + OpenCV颜色空间(Color Space)


  • seuchenrui@126.com

众所周知,视频和图像领域中存在好几种颜色空间,比如RGB、HSV、YCbCr和灰色。本文将通过OpenCV来演示这些颜色空间之间的差别,以图像的形式直接展现出这些颜色空间之间的差别。

代码如下

    import cv2

    image = cv2.imread('1.jpg')
    cv2.imshow("Original", image)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.imshow("Gray", gray)
    cv2.imwrite("1_gray.jpg", gray)

    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    cv2.imshow("HSV", hsv)
    cv2.imwrite("1_hsv.jpg&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 PyQt 结合 OpenCV 进行录制保存和播放视频,可以按照以下步骤进行: 1. 安装 PyQt 和 OpenCV 库: ``` pip install PyQt5 pip install opencv-python ``` 2. 创建一个 PyQt 窗口,用于显示视频: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel from PyQt5.QtGui import QImage, QPixmap class VideoPlayer(QWidget): def __init__(self): super().__init__() self.label = QLabel(self) self.setGeometry(100, 100, 640, 480) self.show() ``` 3. 初始化 OpenCV 的摄像头,并将视频帧显示在 PyQt 窗口中: ```python import cv2 class VideoPlayer(QWidget): def __init__(self): super().__init__() self.label = QLabel(self) self.setGeometry(100, 100, 640, 480) self.show() self.cap = cv2.VideoCapture(0) self.timer = QTimer(self) self.timer.timeout.connect(self.update_frame) self.timer.start(50) def update_frame(self): ret, frame = self.cap.read() if ret: image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = image.shape bytes_per_line = ch * w convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888) p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio) self.label.setPixmap(QPixmap.fromImage(p)) ``` 4. 添加录制和保存视频的功能: ```python class VideoPlayer(QWidget): def __init__(self): super().__init__() self.label = QLabel(self) self.setGeometry(100, 100, 640, 480) self.show() self.cap = cv2.VideoCapture(0) self.timer = QTimer(self) self.timer.timeout.connect(self.update_frame) self.timer.start(50) self.recording = False self.video_writer = None def update_frame(self): ret, frame = self.cap.read() if ret: if self.recording: if self.video_writer is None: fourcc = cv2.VideoWriter_fourcc(*'MJPG') self.video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) self.video_writer.write(frame) image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = image.shape bytes_per_line = ch * w convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888) p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio) self.label.setPixmap(QPixmap.fromImage(p)) def keyPressEvent(self, event): if event.key() == Qt.Key_Space: self.recording = not self.recording if not self.recording and self.video_writer is not None: self.video_writer.release() self.video_writer = None ``` 5. 添加播放已保存视频的功能: ```python class VideoPlayer(QWidget): def __init__(self): super().__init__() self.label = QLabel(self) self.setGeometry(100, 100, 640, 480) self.show() self.cap = cv2.VideoCapture(0) self.timer = QTimer(self) self.timer.timeout.connect(self.update_frame) self.timer.start(50) self.recording = False self.video_writer = None self.playing = False self.video_capture = None def update_frame(self): ret, frame = self.cap.read() if ret: if self.recording: if self.video_writer is None: fourcc = cv2.VideoWriter_fourcc(*'MJPG') self.video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) self.video_writer.write(frame) image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = image.shape bytes_per_line = ch * w convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888) p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio) self.label.setPixmap(QPixmap.fromImage(p)) def keyPressEvent(self, event): if event.key() == Qt.Key_Space: self.recording = not self.recording if not self.recording and self.video_writer is not None: self.video_writer.release() self.video_writer = None elif event.key() == Qt.Key_P: self.playing = not self.playing if self.playing: self.video_capture = cv2.VideoCapture('output.avi') self.timer.stop() self.timer.timeout.connect(self.update_saved_frame) self.timer.start(1000//30) else: self.timer.stop() self.timer.timeout.connect(self.update_frame) self.timer.start(50) def update_saved_frame(self): ret, frame = self.video_capture.read() if ret: image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = image.shape bytes_per_line = ch * w convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888) p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio) self.label.setPixmap(QPixmap.fromImage(p)) ``` 完整的代码如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值