将视频从彩色转换为灰度 [英] convert video from colour to grayscale

查看:47
本文介绍了将视频从彩色转换为灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读入彩色视频并将输出作为灰度视频.下面是我运行得很好但输出是我的相同颜色视频的代码.有人能找出问题所在吗?

I would like to read in a colour video and have the output as a grayscale video. Below is the code that I have which runs just fine but the output is my same colour video. Would anyone be able to identify the problem?

//Convert a video to grayscale
//argv[1]: input video file
//argv[2]: name of new output file

#include "cv.h"
#include "highgui.h"

int main( int argc, char* argv ) {
CvCapture* capture = cvCaptureFromAVI("C:\\walking\\lady walking.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(
    "Output video",//filename for new file
    CV_FOURCC('M','J','P','G'),//video codec to compress video stream
    fps,
    size
    );
IplImage* logpolar_frame = cvCreateImage(
    size,
    IPL_DEPTH_8U,
    3
    );
while( (bgr_frame=cvQueryFrame(capture)) != NULL ) {
    cvLogPolar( bgr_frame, logpolar_frame,
        cvPoint2D32f(bgr_frame->width/2,
        bgr_frame->height/2),
        40,
        CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
    cvWriteFrame( writer, logpolar_frame );
}
cvReleaseVideoWriter( &writer );
cvReleaseImage( &logpolar_frame );
cvReleaseCapture( &capture );

IplImage*image;
CvCapture*recorder = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
cvNamedWindow("Output video",1);
while(1)    {
    image=cvQueryFrame(recorder);   
    if(image==NULL)
    {
        break;
    }
    cvShowImage("Output video",image);
    char c= cvWaitKey(30);
    if(c==27)
    {
        break;
    }

}
cvReleaseCapture( &capture );
cvDestroyWindow( "Output video" );
return 0;
}

推荐答案

在我看来,您的问题只是您在输出视频"窗口中显示的视频实际上是您的lady walk.avi"输入视频,而不是您输出的视频.

Looks to me as if your problem is simply that the video you're displaying in the "Output video" window is actually your "lady walking.avi" input video, not the video you output.

这篇关于将视频从彩色转换为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆