OpenCV RGB到灰色 [英] OpenCV RGB to gray

查看:197
本文介绍了OpenCV RGB到灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个视频监控项目。

我看不到从RGB到灰色的转换;我得到一个黑色的窗口为灰色。

你能帮助我的问题吗? (代码附加)

此外,如何获得当前帧和上一帧之间的区别?

非常感谢。
Ilan

I am doing a project for video monitoring.
I can't see the conversion from RGB to gray; I get a black window for the gray.
Could you please help me with the problem? (code attached)
Also, how can I get the difference between current frame and previous frame?
Thanks a lot. Ilan

#include "stdafx.h"
#include <stdio.h>  // For printf
#include <cv.h>  
#include <cxcore.h>  
#include <highgui.h>      

int main()  
{  


    int key = 0; 



    CvCapture* capture = cvCaptureFromAVI( "macroblock.mpg" );  
    IplImage* frame = cvQueryFrame( capture );
    IplImage* gray = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
    cvCvtColor(frame, gray, CV_RGB2GRAY);

        if ( !capture ) 

    {  
        fprintf( stderr, "Cannot open AVI!\n" );  
        return 1;  
        }
          int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );

           cvNamedWindow( "video", CV_WINDOW_AUTOSIZE ); 
         cvNamedWindow( "grayvideo", CV_WINDOW_AUTOSIZE ); 

        while( key != 'x' )  
    {  
        frame = cvQueryFrame( capture );




    if(key==27 )break;
     cvShowImage( "video",frame );
     cvShowImage( "grayvideo",gray );


           key = cvWaitKey( 1000 / fps );  
    }  
           cvDestroyWindow( "video" );  
    cvReleaseCapture( &capture );  

    return 0;  
}  


推荐答案

从颜色到灰度。如果你在开头做一次,它不会自动转换。所以你需要添加

You need to convert each frame from color to gray scale. It does not get converted automatically if you do it once in the beginning. So you need to add

cvCvtColor(frame, gray, CV_BGR2GRAY);

c>。

这篇关于OpenCV RGB到灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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