opencv上的直方图

时间:2012-04-07 16:18:20

标签: opencv

嘿,我试图制作一个显示帧减法的直方图,代码正在运行,但我得到了灰色的窗口没有结果。 命令窗口中的消息是: 编译器没有对齐堆栈变量。 Libavcodec被错误编译 并且可能非常慢或崩溃。这不是libavcodec中的错误, 但在编译器中。您可以尝试使用gcc> = 4.2重新编译。 不要向FFmpeg开发人员报告崩溃情况。 OpenCV错误:断言失败(images [j] .channels()== 1)未知函数, 文件........ \ ocv \ opencv \ src \ cv \ cvhistogram.cpp,第137行

这是有人有想法的代码吗?感谢您的帮助......

int main()  
{  


    int key = 0; 




     CvCapture* capture = cvCaptureFromAVI( "macroblock.mpg" ); 
     IplImage* frame = cvQueryFrame( capture );
     IplImage* currframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
     IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
   IplImage* imgHistogram = 0;
       CvHistogram* hist;



        if ( !capture ) 

    {  
        fprintf( stderr, "Cannot open AVI!\n" );  
        return 1;  
        }

      int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );

      cvNamedWindow( "dest", CV_WINDOW_AUTOSIZE );
      cvNamedWindow( "imgHistogram", CV_WINDOW_AUTOSIZE );

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



              cvSub(frame,currframe,destframe);

               int bins = 256;
            int hsize[] = {bins};
            float max_value = 0, min_value = 0;
            float value;
            int normalized;
             float xranges[] = {0, 256};
            float* ranges[] = {xranges};
          IplImage* planes[] = {destframe};

    hist = cvCreateHist(1, hsize, CV_HIST_ARRAY, ranges,1);
    cvCalcHist(planes, hist, 0, NULL);
    cvGetMinMaxHistValue(hist, &min_value, &max_value);
  //  printf("Minimum Histogram Value: %f, Maximum Histogram Value: %f\n", min_value, max_value);
  imgHistogram = cvCreateImage(cvSize(bins, 50),IPL_DEPTH_8U,3);
    cvRectangle(imgHistogram, cvPoint(0,0), cvPoint(256,50), CV_RGB(255,255,255),-1);

      for(int i=0; i < bins; i++){
        value = cvQueryHistValue_1D(hist, i);
        normalized = cvRound(value*50/max_value);
        cvLine(imgHistogram,cvPoint(i,50), cvPoint(i,50-normalized), CV_RGB(0,0,0));

      }



              if(key==27 )break;
              cvShowImage( "dest",destframe);
              cvShowImage( "imgHistogram",imgHistogram);

               key = cvWaitKey( 1000 / 10 );
               }  
           cvDestroyWindow( "dest" );
           cvReleaseCapture( &capture );
           return 0;
}

1 个答案:

答案 0 :(得分:4)

由于您试图显示一维直方图,因此直方图平面需要为灰度。因此,您需要先将结果图像从cvSub()转换为灰度。尝试

IplImage *gray = NULL;
gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

while(key != 'x') {
    ...
    cvSub(frame, currframe, destframe);
    cvCvtColor(destframe, gray, CV_BGR2GRAY);
    ...
    IplImage* planes[] = {gray};
    ..
}

让我知道它是否适合你。