OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 15953|回复: 11

新手求助!关于C#下openCV录制读取视频的问题

[复制链接]
发表于 2015-3-19 18:15:21 | 显示全部楼层 |阅读模式
本帖最后由 lyddd333 于 2015-3-19 18:15 编辑

项目需要加视频图像的目标检测和识别的模块,度娘说openCV是利器,所以这两天开始学习EmguCV(项目是C#的而且本人C++基础太渣),没想到刚开始就在视频读取上卡住了,希望各位高手看到支个招,不甚感激!!
环境:win 8.1+Visual Studio 2012(C#)+EmguCV 2.4.10.1940

用网上的一个录制读取视频的例子做测试,结果总是出错:
1. 录制视频时报错:Unable to create VideoWriter. Make sure you have the specific codec installed
2. 播放视频时报错:Unable to create capture from X://XXX.XXX(视频文件路径)

看错误提示的内容貌似是编解码器的问题。这两天前后下载安装了终极解码、ffdshow、Gspot(查看缺少的codec)和VirtualDub(视频格式转换),各种设置都是按照网上的教程来的,但是都没有解决问题。录制视频保存的是avi格式,播放视频尝试了rmvb格式和MP4格式,都没有成功过,目前只有avi格式是可以的,但是项目要求的视频的格式是不确定的,所以其他格式也希望能正常播放。这些视频在我的电脑上都是可以正常播放的,但是到程序里就是不行,用matlab里的函数试过了,也不能播放,所以应该还是程序找不到正确的编解码器的问题。请问各位高手,这个问题要怎么解决呢?

程序是按照别人发在网上的例子来写的(http://blog.sina.com.cn/s/blog_b793c1190101aqgq.html),怕超字数,贴在下一楼。


回复

使用道具 举报

 楼主| 发表于 2015-3-19 18:16:06 | 显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. //添加的头文件
  10. using Emgu.CV;
  11. using Emgu.CV.UI;
  12. using Emgu.CV.Structure;
  13. using Emgu.Util;

  14. namespace MyEmgu
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private Capture capture;
  19. Image frame;
  20. VideoWriter videowriter;//
  21. int VideoFps;//视频帧率
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Record_Click(object sender, EventArgs e)
  27. {
  28. if (Record.Text=="录制")
  29. {//打开摄像头并开始录制
  30. try
  31. {
  32. SaveFileDialog saveFileDialog = new SaveFileDialog();
  33. saveFileDialog.Filter = "AVI|*.avi";
  34. saveFileDialog.AddExtension = true;
  35. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  36. {
  37. capture = new Capture();
  38. videowriter = new VideoWriter(saveFileDialog.FileName, //文件名
  39. CvInvoke.CV_FOURCC('M', 'J', 'P', 'G'), //编码格式
  40. 25, //帧率
  41. (int)CvInvoke.cvGetCaptureProperty(capture, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH), //视频宽度
  42. (int)CvInvoke.cvGetCaptureProperty(capture, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT), //视频高度
  43. true);//彩色
  44. Application.Idle += new EventHandler(ProcessFrame);
  45. Record.Text = "停止";
  46. Play.Enabled = false;
  47. }
  48. }
  49. catch (NullReferenceException excpt)
  50. {
  51. MessageBox.Show(excpt.Message);
  52. }
  53. }
  54. else
  55. {//关闭摄像头,停止录制
  56. capture.Dispose();
  57. Application.Idle -= new EventHandler(ProcessFrame);
  58. videowriter.Dispose();
  59. Record.Text = "录制";
  60. Play.Enabled = true;
  61. }
  62. }
  63. private void Play_Click(object sender, EventArgs e)
  64. {
  65. if (Play.Text == "播放")
  66. {//开启播放模式
  67. OpenFileDialog openFileDialog = new OpenFileDialog();
  68. openFileDialog.Filter = "AVI文件|*.avi|RMVB文件|*.rmvb|WMV文件|*.wmv|MKV文件|*.mkv|所有文件|*.*";
  69. if (openFileDialog.ShowDialog() == DialogResult.OK)
  70. {
  71. Application.Idle += new EventHandler(ProcessFrame);
  72. capture = new Capture(openFileDialog.FileName);
  73. VideoFps = (int)CvInvoke.cvGetCaptureProperty(capture,
  74. Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);
  75. Play.Text = "停止";
  76. VideoBox.Image = null;
  77. Record.Enabled = false;
  78. }
  79. }
  80. else
  81. {
  82. capture.Dispose();
  83. Application.Idle -= new EventHandler(ProcessFrame);
  84. Play.Text = "播放";
  85. Record.Enabled = true;
  86. }
  87. }
  88. private void ProcessFrame(object sender, EventArgs arg)
  89. {
  90. if (Play.Text != "播放")//正在播放视频
  91. {
  92. frame = capture.QueryFrame();
  93. if (frame != null)
  94. {
  95. //为使播放顺畅,添加以下延时
  96. System.Threading.Thread.Sleep((int)(1000.0 / VideoFps - 5));
  97. VideoBox.Image = frame;
  98. }
  99. else
  100. {
  101. Play.Text = "播放";
  102. Record.Enabled = true;
  103. VideoBox.Image = null;
  104. }
  105. }
  106. else if (Record.Text != "录制")//正在录制
  107. {
  108. try
  109. {
  110. frame = capture.QueryFrame();
  111. VideoBox.Image = frame;
  112. videowriter.WriteFrame(frame);
  113. }
  114. catch
  115. {
  116. System.Diagnostics.Process.Start(Application.ExecutablePath);
  117. System.Diagnostics.Process.GetCurrentProcess().Kill();
  118. }
  119. }
  120. }
  121. }
  122. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-21 11:02:36 | 显示全部楼层
看完于老师的入门教程后发现录制视频不成功是codec不正确的问题,换一个就对了。
但是现在又出现了新的问题,就是播放和停止的时候都会出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”,设断点之后发现是运行到“Application.Idle -= new EventHandler(ProcessFrame);”这一句时出现的问题,求问各位大牛,出现这个问题一般是什么原因呢?是我本身内存的问题还是程序的问题呢?
回复 支持 反对

使用道具 举报

发表于 2015-4-7 18:41:08 | 显示全部楼层
private void ProcessFrame(object sender, EventArgs arg)
{
if (Play.Text != "播放")//正在播放视频
{
frame = capture.QueryFrame();改成RetrieveBgrFrame()试一下,
回复 支持 反对

使用道具 举报

发表于 2015-4-17 14:21:40 | 显示全部楼层
qtfan 发表于 2015-4-7 18:41
private void ProcessFrame(object sender, EventArgs arg)
{
if (Play.Text != "播放")//正在播放视频

第一个问题改下videowriter
videowriter = new VideoWriter(saveFileDialog.FileName, //文件名
                        25, //帧率
                        capture.Width, //视频宽度
                        capture.Height, //视频高度
                        true);//彩色
第二个问题
frame = capture.QueryFrame();改成RetrieveBgrFrame() 这个录制可以,但是播放不行,会是frame为null,直接把capture.Dispose();这句屏蔽就可以了,我的是可以了,还不知道有什么其它的后果,(本人小白,刚开始学OpenCV)
回复 支持 反对

使用道具 举报

发表于 2015-10-28 10:09:34 | 显示全部楼层
异常:已引发: "无法加载 DLL“opencv_core231”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。" (System.DllNotFoundException)
引发了一个 System.DllNotFoundException: "无法加载 DLL“opencv_core231”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。"
时间: 2015/10/28 9:59:55
线程: <无名称>[5316]
用的配置是VS2013和emguCV2.9,播放视频会出现这个情况,肿么办呀
回复 支持 反对

使用道具 举报

发表于 2015-11-5 10:36:08 | 显示全部楼层
zazelily 发表于 2015-10-28 10:09
异常:已引发: "无法加载 DLL“opencv_core231”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。" (S ...

已经解决
回复 支持 反对

使用道具 举报

发表于 2015-12-4 11:27:39 | 显示全部楼层

什么原因,可以把解决方法描述一下 吗?
回复 支持 反对

使用道具 举报

发表于 2015-12-26 10:52:35 | 显示全部楼层
:@:@
回复 支持 反对

使用道具 举报

发表于 2016-5-8 16:11:48 | 显示全部楼层
楼主~我也遇到一样的问题了,你最后是怎么解决的,求赐教啊!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-4-26 05:52 , Processed in 0.010085 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表