无法在Python中使用OpenCV编写视频

时间:2017-03-06 08:32:08

标签: python opencv ros openkinect

我正在尝试将实时图像写入Kinect在Ubuntu中获取的视频文件中。请参阅下面的示例代码:

***admin_panel.jsp***

<h1>Admin Panel</h1>
<h3>${Name}</h3>
<h3>${Email}</h3>
<h3>${Phone}</h3>
<h3>${textarea}</h3>

代码运行顺畅,终端没有显示任何错误。但是,它不会生成任何视频文件,即使我使用#!/usr/bin/env python import roslib roslib.load_manifest('ros_package_name') import rospy import cv2 from sensor_msgs.msg import Image from cv_bridge import CvBridge, CvBridgeError class image_converter(object): def __init__(self, topic_name, file_name, fps=20): self.fps = fps self.file_name = file_name self.bridge = CvBridge() self.image_sub = rospy.Subscriber(topic_name, Image, self.callback) self.video_writer = None def callback(self,data): try: cv_image = self.bridge.imgmsg_to_cv2(data, 'bgr8') except CvBridgeError as e: print e if self.video_writer is None: rows, cols, _ = cv_image.shape self.video_writer = cv2.VideoWriter(self.file_name, -1, self.fps, (cols, rows)) self.video_writer.write(cv_image) def clean_shutdown(self): if self.video_writer is not None: self.video_writer.release() print 'Saving video file', self.file_name def __del__(self): self.clean_shutdown() def main(): ic = image_converter('/kinect2/qhd/image_color_rect','video.avi') rospy.init_node('save_video', anonymous=True) rospy.on_shutdown(ic.clean_shutdown) rospy.spin() 终止它。

1 个答案:

答案 0 :(得分:1)

使用编解码器保存视频。 Motion Jpeg几乎总是有效!

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
self.video_writer = cv2.VideoWriter(file_name, fourcc, fps, frame_size)

然后是故事的其余部分。