将RGB视频转换为灰度

将RGB视频转换为灰度

我有这个程序应将RGB视频转换为灰度:

CvCapture* capture = 0;
capture = cvCreateFileCapture( "sample.avi" );
if(!capture)
{
    return -1;
}
IplImage *bgr_frame=cvQueryFrame(capture);//Init the video read
double fps = cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);

CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter *writer = cvCreateVideoWriter("izlaz.avi",CV_FOURCC_DEFAULT,fps,size);

IplImage *grayScaleImage = cvCreateImage(size ,IPL_DEPTH_8U,1);



while( (bgr_frame=cvQueryFrame(capture)) != NULL )
{

    cvCvtColor(bgr_frame, grayScaleImage, CV_BGR2GRAY);
    cvWriteFrame( writer, grayScaleImage );


}

cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );

事实并非如此。谁能帮我修改它以使其正常工作?谢谢 :)

最佳答案

我认为您需要在您的编解码器的fourcc之间进行选择,CV_FOURCC_DEFAULT对我不起作用。我尝试了CV_FOURCC('P','I','M,'1')和CV_FOURCC('M','J','P','G')。他们俩都为我工作。

这是我尝试的示例代码。

#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;

int main(int argc, char*argv[])
{

    char *my_file = "C:\\vid_an2\\Wildlife.wmv";

    std::cout<<"Video File "<<my_file<<std::endl;

    cv::VideoCapture input_video;

    if(input_video.open(my_file))
    {
       std::cout<<"Video file open "<<std::endl;
    }
    else
    {
        std::cout<<"Not able to Video file open "<<std::endl;
    }
    int fps = input_video.get(CV_CAP_PROP_FPS);
    int frameCount = input_video.get(CV_CAP_PROP_FRAME_COUNT);
    double fheight = input_video.get(CV_CAP_PROP_FRAME_HEIGHT);
    double fwidth = input_video.get(CV_CAP_PROP_FRAME_WIDTH);
    CvSize fsize;
    fsize.width = fwidth;
    fsize.height = fheight;

    CvVideoWriter *new_writer = cvCreateVideoWriter("brg_file.avi",CV_FOURCC('M','J','P','G'), fps,fsize, 0);
    std::cout<<"Video Frame Rate "<<fps<<std::endl;
    std::cout<<"Video Frame Count "<<frameCount<<std::endl;
    Mat cap_img;
    Mat gry_img;
    IplImage new_img;
    while(input_video.grab())
   {
        if(input_video.retrieve(cap_img))
        {
                cvtColor(cap_img, gry_img, CV_RGB2GRAY);
                new_img = gry_img.operator IplImage();
                int ret = cvWriteFrame(new_writer, (const IplImage*)&new_img);
                std::cout<<"Wrote Frame "<<ret<<std::endl;

        }
    }
    cvReleaseVideoWriter(&new_writer);

    return 0;
}

关于opencv - 在OpenCV中将RGB视频转换为灰度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16266011/

10-10 18:28