pra*_*ala 5 c++ opencv image-processing video-processing
我正在使用以下代码从文件中读取视频,应用canny edge算法并将修改后的视频写入文件.代码编译和运行完美.但是,视频不是写的!我完全糊涂了.请告诉我错误是什么.该文件根本没有创建!操作系统:Ubuntu 12.10
写入输出文件的代码
打开输出文件
bool setOutput(const std::string &filename, int codec=0, double framerate=0.0, bool isColor=true) {
outputFile= filename;
extension.clear();
if (framerate==0.0)
framerate= getFrameRate(); // same as input
char c[4];
// use same codec as input
if (codec==0) {
codec= getCodec(c);
}
// Open output video
return writer.open(outputFile, // filename
codec, // codec to be used
framerate, // frame rate of the video
getFrameSize(), // frame size
isColor); // color video?
}
Run Code Online (Sandbox Code Playgroud)
写帧
void writeNextFrame (Mat& frame)
{
writer.write (frame);
}
Run Code Online (Sandbox Code Playgroud)
并且有一个单独的run方法来执行这些
每当我在我的应用程序中遇到奇怪的行为时,我会写一个简短的,自包含的,正确的(可编译的)示例来帮助我理解正在发生的事情.
我编写了下面的代码来说明你应该做什么.值得注意的是,它在我的Mac OS X上完美运行:
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
// Load input video
cv::VideoCapture input_cap("Wildlife.avi");
if (!input_cap.isOpened())
{
std::cout << "!!! Input video could not be opened" << std::endl;
return -1;
}
// Setup output video
cv::VideoWriter output_cap("output.avi",
input_cap.get(CV_CAP_PROP_FOURCC),
input_cap.get(CV_CAP_PROP_FPS),
cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH), input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));
if (!output_cap.isOpened())
{
std::cout << "!!! Output video could not be opened" << std::endl;
return -1;
}
// Loop to read frames from the input capture and write it to the output capture
cv::Mat frame;
while (true)
{
if (!input_cap.read(frame))
break;
output_cap.write(frame);
}
// Release capture interfaces
input_cap.release();
output_cap.release();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用FFmpeg检查输入文件reveal(ffmpeg -i Wildlife.avi):
Input #0, avi, from 'Wildlife.avi':
Metadata:
ISFT : Lavf52.13.0
Duration: 00:00:07.13, start: 0.000000, bitrate: 2401 kb/s
Stream #0.0: Video: msmpeg4v2, yuv420p, 1280x720, PAR 1:1 DAR 16:9, 29.97 tbr, 29.97 tbn, 29.97 tbc
Stream #0.1: Audio: mp3, 44100 Hz, 2 channels, s16, 96 kb/s
Run Code Online (Sandbox Code Playgroud)
和输出:
Input #0, avi, from 'output.avi':
Metadata:
ISFT : Lavf52.61.0
Duration: 00:00:07.10, start: 0.000000, bitrate: 3896 kb/s
Stream #0.0: Video: msmpeg4v2, yuv420p, 1280x720, 29.97 tbr, 29.97 tbn, 29.97 tbc
Run Code Online (Sandbox Code Playgroud)
因此,两个文件之间唯一重要的变化是OpenCV生成的输出没有音频流,这是正确的行为,因为OpenCV不处理音频.
确保您的用户具有在运行应用程序的目录中读/写/执行的适当权限.此外,我在代码中添加的调试可能会帮助您找到与输入/输出捕获相关的问题.