找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6238|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

OpenCV學(xué)習(xí)—視頻文件處理 圖像變換

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
呃,網(wǎng)上的學(xué)習(xí)筆記好亂@@
還是自己邊學(xué)邊寫吧。不然幾個(gè)月后又忘記怎么弄了。

OpenCV如何在VS2010下配置的方法及OpenCV的介紹就不寫了。
如有需要,再寫如何配置的筆記。




第一個(gè)練習(xí)——【打開圖片】


鍵入代碼:

#include "stdafx.h"
#include "cv.h"
#include
#include


int _tmain(int argc, _TCHAR* argv[])
{
        IplImage *img = cvLoadImage("funny-pictures.jpg");
        cvNamedWindow("Image:",1);
        cvShowImage("Image:",img);

        cvWaitKey();
        cvDestroyWindow("Image:");
        cvReleaseImage(&img);

        return 0;

}

程序運(yùn)行后顯示如下:





代碼解析:
#include "stdafx.h"
#include "cv.h"
#include
#include
引用不再解釋,不太理解可詳見c++ premier。稍后轉(zhuǎn)錄過來(lái)。

int _tmain(int argc, _TCHAR* argv[])
在main函數(shù)中定義一個(gè)argc用于讀取輸入?yún)?shù)個(gè)數(shù),argv[]數(shù)組用于存放輸入的參數(shù)。
如輸入test  E:jay.jpg,argc讀取參數(shù)個(gè)數(shù)為2,argv[0]為test,而argv[1]中的E:jay.jpg讀出為E:\jay.jpg。這個(gè)具體原因待整理。更多詳見http://www.opencv.org.cn/index.p ... v%E8%AF%B4%E6%98%8E

IplImage *img = cvLoadImage("E:lena.jpg");
此行代碼是將圖像加載到內(nèi)存,cvLoadImage()函數(shù)通過文件名確定被加載文件的格式并自動(dòng)分配圖像所需內(nèi)存。cvLoadImage()函數(shù)可以打開大部分常用圖像格式,如BMP,JPEG,JPG,PNG等。該函數(shù)執(zhí)行完后會(huì)返回一個(gè)指針,該指針指向描述圖像文件數(shù)據(jù)結(jié)構(gòu)IplImage分配的內(nèi)存。E:lena.jpg為指定圖像所在地址,也可直接為lena.jpg,此處為程序默認(rèn)儲(chǔ)存地址([VS2010默認(rèn)項(xiàng)目文件夾]項(xiàng)目文件夾項(xiàng)目文件夾  下)內(nèi)的圖片。lena.jpg原為大家圖像都很熟悉的草帽女,但是我測(cè)試的時(shí)候沒有去找就直接在e盤下重命名了張圖片。

cvNamedWindow("Image:",1);
cvNamedWindow()函數(shù)將在屏幕上創(chuàng)建一個(gè)窗口用于顯示圖像。函數(shù)中第一個(gè)參數(shù)為窗口命名為"Image:",第二個(gè)參數(shù)為定義窗口屬性,默認(rèn)值為0,表示窗口大小不會(huì)因圖像的大小而改變,圖像將根據(jù)窗口大小進(jìn)行變化充滿窗口。為1或CV_WINDOW_AUTOSIZE時(shí),窗口將根據(jù)圖像的實(shí)際大小自動(dòng)變化適應(yīng)圖像。

cvShowImage("Image:",img);
cvShowImage()函數(shù)通過第一個(gè)參數(shù)確定在已創(chuàng)建的哪個(gè)窗口中顯示圖像,且該函數(shù)被調(diào)用時(shí)窗口將被重繪并將圖像顯示到窗口中。

cvWaitKey();
函數(shù)功能為使程序暫停,等待觸發(fā)按鍵。函數(shù)中參數(shù)為正值時(shí),程序?qū)和T撜麛?shù)值個(gè)毫秒后繼續(xù)執(zhí)行程序,沒有按下按鍵也會(huì)如此。設(shè)置為0或者負(fù)數(shù)將一直等待用戶觸發(fā)按鍵。

vc++_2008和vc++_2010安裝方法可參考:http://www.opencv.org.cn/index.p ... E8%A3%85OpenCV2.3.1
vs2010配置VC++目錄可參考:http://blog.csdn.net/zhangyafengcpp/article/details/6847821

視頻文件處理:
第一段測(cè)試代碼
      
    #include "stdafx.h"  
    #include   
    #include   
    //#include   
    #include   
    #include   
    #include
    using namespace std;
    int main()
    {
       IplImage *frame = NULL;
       CvCapture *capture = NULL;
      capture = cvCaptureFromAVI("D:\123\1.AVI");
        frame = cvQueryFrame(capture);
       cvNamedWindow("frame");
       while(frame){
          cvShowImage("frame", frame);
          cvWaitKey(20);
          cout << "Frame Grabbed." << endl;
          frame = cvQueryFrame(capture);
       }
       return 0;
    }
運(yùn)行后界面顯示如下

前面的不再做過多解析@@

其實(shí)我也想不起來(lái)了,四天沒碰。。浪費(fèi)時(shí)間啥也沒記住。。

cvCaptureFromAVI()通過參數(shù)讀入AVI文件及其所有信息并返回一個(gè)CvCapture指針,對(duì)人AVI視頻時(shí)參數(shù)為該指針的cvQueryFrame()將為圖像分配內(nèi)存,與圖像顯示不同,不需要cvLoadImage為圖像分配內(nèi)存。CvCapture被釋放后,每一幀圖像的內(nèi)存空間也將被釋放。
cout函數(shù)讓圖像播放時(shí)將不斷輸出  Frame Grabbed.



第二段測(cè)試代碼
      
    #include "stdafx.h"  
    #include   
    #include   
    #include   
    #include   
    #include   
    #include
    // 使用標(biāo)準(zhǔn)命名空間  
        using namespace std;  
  
        CvCapture* g_capture1 = NULL;  

    int main(int argc, char** argv )  
        {  
                // 建立播放窗口  
                 cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );
                 // 捕捉視頻文件
                 argv[1]="D:\123\1.avi";
                 g_capture1 = cvCreateFileCapture( argv[1] );
                 // 開始播放并保存視頻  
                 IplImage* frame1;   
                 while(1)  
                 {  
                  // 獲取、顯示源文件的幀畫面  
                  frame1 = cvQueryFrame( g_capture1 );  
                  if( !frame1 ) break;  
                  cvShowImage( "Video Test 1", frame1 );  
                  // 若按下 ESC 鍵,則退出程序  
                  char c = cvWaitKey(33);  
                  if( c==27 ) break;  
                 }  
                 // 釋放內(nèi)存,關(guān)閉窗口  
                 cvReleaseCapture( &g_capture1 );   
                 cvDestroyWindow( "Video Test 1" );  
  
                 return 0;  
        }  






下面段函數(shù)我電腦上運(yùn)行不出結(jié)果。。待解決
    // test2_video.cpp : Defines the entry point for the console application.  
    //  
      
    #include "stdafx.h"  
    #include   
    #include   
    //#include   
    #include   
    #include   
      
    // 使用標(biāo)準(zhǔn)命名空間  
    using namespace std;  
      
    // 初始化進(jìn)度條的位置  
    int g_slider_position1 = 0;  
    int g_slider_position2 = 0;  
      
    CvCapture* g_capture1 = NULL;  
    CvCapture* g_capture2 = NULL;  
      
    // 定義回調(diào)函數(shù)用于播放進(jìn)度的控制   
    void onTrackbarSlide1( int pos1 )  
    {  
     cvSetCaptureProperty( g_capture1, CV_CAP_PROP_POS_FRAMES, pos1 );  
    }  
    void onTrackbarSlide2( int pos2 )  
    {  
     cvSetCaptureProperty( g_capture2, CV_CAP_PROP_POS_FRAMES, pos2 );  
    }  
      
      
    int main(int argc, char** argv )  
    {  
     // 建立播放窗口  
     cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );  
     cvNamedWindow( "Video Test 2", CV_WINDOW_AUTOSIZE );  
      
     // 捕捉視頻文件  
     g_capture1 = cvCreateFileCapture( argv[1] );  
     g_capture2 = cvCreateFileCapture( argv[2] );  
      
     // 讀取、顯示視頻文件的幀數(shù)  
     int frames1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FRAME_COUNT );  
     cout << "frames1 = " << frames1 << endl;  
     // 建立進(jìn)度條  
     if( frames1 != 0 )  
      cvCreateTrackbar(   
       "Position",   
       "Video Test 1",   
       &g_slider_position1,   
       frames1,   
       onTrackbarSlide1  
      );  
      
     int frames2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FRAME_COUNT );  
     cout << "frames2 = " << frames2 << endl;  
     if( frames2 != 0 )  
      cvCreateTrackbar(   
       "Position",   
       "Video Test 2",   
       &g_slider_position2,   
       frames2,   
       onTrackbarSlide2   
      );  
      
    // 讀取視頻文件信息  
     double fps1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FPS );  
     double fps2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FPS );  
     CvSize size1 = cvSize(   
      (int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_WIDTH),  
      (int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_HEIGHT));  
     CvSize size2 = cvSize(   
      (int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_WIDTH),  
      (int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_HEIGHT));  
      
    // 創(chuàng)建 VideoWriter   
     CvVideoWriter* wrVideo1 = cvCreateVideoWriter(argv[3], CV_FOURCC('M','J','P','G'), fps1, size1);  
     CvVideoWriter* wrVideo2 = cvCreateVideoWriter(argv[4], CV_FOURCC('M','J','P','G'), fps2, size2);  
      
     int frs = 0;  
      
     // 開始播放并保存視頻  
     IplImage* frame1;  
     IplImage* frame2;  
      
     while( frs < frames1 && frs < frames2 )  
     {  
      
      // 獲取、顯示源文件的幀畫面  
      frame1 = cvQueryFrame( g_capture1 );  
      if( !frame1 ) break;  
      cvShowImage( "Video Test 1", frame1 );  
      
      frame2 = cvQueryFrame( g_capture2 );  
      if( !frame2 ) break;  
      cvShowImage( "Video Test 2", frame2 );  
      
      // 保存:將當(dāng)前幀寫入到目標(biāo)視頻文件  
      
      cvWriteFrame( wrVideo1, frame1 );  
      cvWriteFrame( wrVideo2, frame2 );  
        
      // 若按下 ESC 鍵,則退出程序  
      char c = cvWaitKey(33);  
      if( c==27 ) break;  
     }  
     // 釋放內(nèi)存,關(guān)閉窗口  
     cvReleaseCapture( &g_capture1 );  
     cvReleaseCapture( &g_capture2 );  
     cvReleaseVideoWriter( &wrVideo1 );  
     cvReleaseVideoWriter( &wrVideo2 );  
     cvDestroyWindow( "Video Test 1" );  
     cvDestroyWindow( "Video Test 2" );  
      
     return 0;  
    }  
呃,先貼過來(lái),因?yàn)樘嘁獪y(cè)試的,邊測(cè)試邊寫
----------------------------------------

    #include "stdafx.h"  
    #include "cv.h"
    #include "highgui.h"
    #include
    #include
    int main( int argc, char** argv )
    {
        IplImage* img = 0;
      
        int nFrames = 50;
        CvCapture* capture = 0;
       CvVideoWriter *writer = 0;
       int isColor = 1;
        int fps = 25; // or 30
        int frameW = 640; //
        int frameH = 480; //
      
        if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
            capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
            if( !capture )
        {
            fprintf(stderr,"Could not initialize capturing...");
            return -1;
        }
           writer=cvCreateVideoWriter("D:\out.avi",CV_FOURCC('X','V','I','D'),
        fps,cvSize(frameW,frameH),isColor);
    //存儲(chǔ)視頻文件 CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
                for(int i=0;i<nframes;i++)
                {
                        cvGrabFrame(capture); // 抓取幀
                        img=cvRetrieveFrame(capture); // 恢復(fù)圖像
                        cvWriteFrame(writer,img); // 將幀添加入視頻文件
                        //顯示所抓視頻
                        cvNamedWindow("Live", CV_WINDOW_AUTOSIZE);//創(chuàng)建窗口
                        cvShowImage("Live", img);//顯示所抓視頻
                        cvWaitKey(50); // wait 20 ms
                }
                cvReleaseVideoWriter(&writer);
    return 0;
    }

錄制avi視頻

#include "cv.h"
#include "highgui.h"
#include
#include
int main( int argc, char** argv )
{
    IplImage* img = 0;
   
    int nFrames = 500;
    CvCapture* capture = 0;
   CvVideoWriter *writer = 0;
   int isColor = 1;
    int fps = 25; // or 30
    int frameW = 640; //
    int frameH = 480; //
   
    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
      if( !capture )
    {
        fprintf(stderr,"Could not initialize capturing...");
        return -1;
    }
    writer=cvCreateVideoWriter("D:\out.mp4",CV_FOURCC('D','I','V','X'),
       fps,cvSize(frameW,frameH),isColor);
//存儲(chǔ)視頻文件CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
for(int i=0;i<nframes;i++)
{
cvGrabFrame(capture); // 抓取幀
img=cvRetrieveFrame(capture); // 恢復(fù)圖像
cvWriteFrame(writer,img); // 將幀添加入視頻文件
//顯示所抓視頻
cvNamedWindow("Live", CV_WINDOW_AUTOSIZE);//創(chuàng)建窗口
cvShowImage("Live", img);//顯示所抓視頻
cvWaitKey(20); // wait 20 ms
}
cvReleaseVideoWriter(&writer);
return 0;
}

錄制mpga視頻

------------------------------------------------

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

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) //消除console窗口
//此處稍后寫學(xué)習(xí)筆記講解


//初始化進(jìn)度條的位置
int g_slider_position=0;
CvCapture* g_capture=NULL;



//進(jìn)度條的回調(diào)函數(shù),播放進(jìn)度控制
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);//設(shè)置視頻
}



int main(int argc, char* argv[])
{
//創(chuàng)建播放窗口
    cvNamedWindow("Window Name", CV_WINDOW_AUTOSIZE);
//捕獲視頻
    g_capture=cvCreateFileCapture("D:\out1.avi");
//獲取視頻的幀數(shù)
int frames = (int) cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
//建立進(jìn)度條
if(frames!=0)
{
    cvCreateTrackbar("Trackbar Name","Window Name",&g_slider_position,frames,onTrackbarSlide);
}
//捕獲、播放視頻
    IplImage* frame;
    while(1)
{
    frame=cvQueryFrame(g_capture);
    if( !frame ) break;
//獲取視頻播放位置
int trapos=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES);
//設(shè)置進(jìn)度條位置,使其和視頻播放同步
cvSetTrackbarPos("Trackbar Name","Window Name", trapos);
//播放視頻
    cvShowImage("Window Name",frame);
//等待按鍵
    char c=cvWaitKey(33);
    if(c==27) break;
}
//釋放資源
    cvReleaseCapture(&g_capture);
    cvDestroyWindow( "Window Name");

return 0;
}

添加進(jìn)度條成功


呃。由于是上一條錄制視頻的時(shí)候錄制的自己

所以播放也就播放自己了。。截圖截個(gè)搓搓的頭發(fā)吧


int g_slider_position=0;
CvCapture* g_capture=NULL;
為進(jìn)度條添加全局變量。而回調(diào)函數(shù)需要使用CvCapture對(duì)象,故將它定義為全局變量。為增加程序可讀性,前加g_方便修改者尋找。

void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);//設(shè)置視頻
}
定義一個(gè)在進(jìn)度條被拖動(dòng)時(shí)調(diào)用的回調(diào)函數(shù)。拖動(dòng)條位置會(huì)被作為一個(gè)32位的整數(shù)以參數(shù)形式傳入。
CV_CAP_PROP_POS_FRAMES表示我們以幀數(shù)來(lái)設(shè)置讀入位置,F(xiàn)RAMES若用AVI_RATIO代替表示通過視頻長(zhǎng)度比例來(lái)設(shè)置讀入位置。最后,新的進(jìn)度條位置作為參數(shù)傳入。
int frames = (int) cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
需要從CvCapture結(jié)構(gòu)查詢數(shù)據(jù)時(shí),可使用cvGetCaptureProperty函數(shù),我們希望獲得視頻文件的總幀數(shù)以對(duì)進(jìn)度條進(jìn)行設(shè)置。
if(frames!=0)
{
    cvCreateTrackbar("Trackbar Name","Window Name",&g_slider_position,frames,onTrackbarSlide);
}
借助cvCreateTrackbar()創(chuàng)建進(jìn)度條,可設(shè)置進(jìn)度條名稱和所屬窗口。將一個(gè)變量綁定到進(jìn)度條來(lái)表示其進(jìn)度條最大值及一個(gè)回調(diào)函數(shù)(不需要回調(diào)函數(shù)的時(shí)候?yàn)榭,進(jìn)度條被拖動(dòng)時(shí)觸發(fā))。cvCreateTrackbar()返回幀數(shù)為0時(shí),進(jìn)度條不會(huì)被創(chuàng)建。因?yàn)橛行┚幋a方式無(wú)法獲得幀數(shù),這時(shí)候只能播放而無(wú)法獲得進(jìn)度條。
------------------------------------------------------

圖像變換:
#include "stdafx.h"
#include "cv.h"
#include
#include



void example2_4( IplImage* image )

{
    // Create some windows to show the inpu
    // and output images in.
    //
    cvNamedWindow( "Example2_4-in", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "Example2_4-out", CV_WINDOW_AUTOSIZE );   
    // Create a window to show our input image
    //
    cvShowImage( "Example2_4-in", image );   
    // Create an image to hold the smoothed output
    IplImage* out = cvCreateImage(
       cvGetSize(image),
        IPL_DEPTH_8U,
        3
    );
    // Do the smoothing
    //
    cvSmooth( image, out, CV_GAUSSIAN, 5,5 );
    cvSmooth( out, out, CV_GAUSSIAN, 5, 5);  
    // Show the smoothed image in the output window
    //
    cvShowImage( "Example2_4-out", out );
    // Be tidy
    //
    cvReleaseImage( &out );
    // Wait for the user to hit a key, then clean up the windows
    //
    cvWaitKey( 0 );
    cvDestroyWindow("Example2_4-in" );
    cvDestroyWindow("Example2_4-out" );
}



int main( int argc, char** argv )
{
  argv[1]="E:lena.jpg";
  IplImage* img = cvLoadImage( argv[1] );
  cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example1", img );
  example2_4( img );
  cvWaitKey(0);
  cvReleaseImage( &img );
  cvDestroyWindow("Example1");
}


cvShowImage()和學(xué)習(xí)筆記之初試中無(wú)異,cvCreateImage()來(lái)為新的幀分配空間且只分配一幀圖像的空間,再次調(diào)用時(shí)覆蓋前一次的數(shù)據(jù)(這樣每次調(diào)用返回的指針是一樣的)。
cvGetsize(image)獲得CvSize結(jié)構(gòu),第一個(gè)參數(shù)說(shuō)明里當(dāng)前圖像的結(jié)構(gòu)大學(xué)結(jié)構(gòu),第二個(gè)參數(shù)告訴了我們各通道每個(gè)像素點(diǎn)的數(shù)據(jù)類型,最后一個(gè)參數(shù)說(shuō)明了通道的總數(shù)。程序中圖像通道是3個(gè)(每個(gè)通道為8位)。圖像大小同image。
該例程為平滑處理函數(shù),通過使用每個(gè)像素周圍3*3區(qū)域進(jìn)行高斯平滑處理。

#include "stdafx.h"
#include "cv.h"
#include
#include


IplImage* doPyrDown(
  IplImage* in,
  int       filter = IPL_GAUSSIAN_5x5)

{
   // Best to make sure input image is divisible by two.
    assert( in->width%2 == 0 && in->height%2 == 0 );
   IplImage* out = cvCreateImage(
        cvSize( in->width/2, in->height/2 ),
        in->depth,
        in->nChannels
    );
    cvPyrDown( in, out );
    return( out );
};

int main( int argc, char** argv )
{
  argv[1]="E:lena.jpg";
  IplImage* img = cvLoadImage( argv[1] );
  IplImage* img2 = cvCreateImage(
cvSize( img->width/2,img->height/2 ),
img->depth, img->nChannels);
  cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example1", img );
  img2 = doPyrDown( img );
  cvShowImage("Example2", img2 );
  cvWaitKey(0);
  cvReleaseImage( &img );
  cvReleaseImage( &img2 );
  cvDestroyWindow("Example1");
  cvDestroyWindow("Example2");
}


#include "stdafx.h"
#include "cv.h"
#include
#include



IplImage* doCanny(
    IplImage* in,
    double    lowThresh,
    double    highThresh,
    double    aperture)
{
    if (in->nChannels != 1)
        return(0);
// Canny only handles gray scale images
    IplImage* out = cvCreateImage(
        cvGetSize( in ),
        in->depth,
//        IPL_DEPTH_8U,   
        1);
    cvCanny( in, out, lowThresh, highThresh, aperture );
        return( out );
};

int main( int argc, char** argv )
{
  argv[1]="E:lena.jpg";
  IplImage* img_rgb = cvLoadImage( argv[1] );
  IplImage* img_gry = cvCreateImage(
cvSize( img_rgb->width,img_rgb->height ),
img_rgb->depth,
1);
  cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
  cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example Gray", img_gry );
  IplImage* img_cny = doCanny( img_gry, 10, 100, 3 );
  cvShowImage("Example Canny", img_cny );
  cvWaitKey(0);
  cvReleaseImage( &img_rgb);
  cvReleaseImage( &img_gry);
  cvReleaseImage( &img_cny);
  cvDestroyWindow("Example Gray");
  cvDestroyWindow("Example Canny");
}


#include "stdafx.h"
#include "cv.h"
#include
#include


IplImage* doCanny(
    IplImage* in,
    double    lowThresh,
    double    highThresh,
    double    aperture)
{
    IplImage* out = cvCreateImage(
        cvGetSize( in ),
        in->depth,
         //IPL_DEPTH_8U,  
        1);
    cvCanny( in, out, lowThresh, highThresh, aperture );
    return( out );
};

IplImage* doPyrDown(
  IplImage* in,
  int       filter = IPL_GAUSSIAN_5x5)
{

    // Best to make sure input image is divisible by two.
    //  assert( in->width%2 == 0 && in->height%2 == 0 );
    IplImage* out = cvCreateImage(
        cvSize( in->width/2, in->height/2 ),
        in->depth,
        in->nChannels
    );
    cvPyrDown( in, out );
    return( out );
};


int main( int argc, char** argv )
{
  IplImage* img_rgb  = cvLoadImage("E:\lena.jpg");
  IplImage* img_gry  = cvCreateImage( cvSize( img_rgb->width,img_rgb->height ), img_rgb->depth, 1);
  cvCvtColor(img_rgb, img_gry ,CV_BGR2GRAY);
  IplImage* img_pyr  = doPyrDown( img_gry, IPL_GAUSSIAN_5x5 );
  IplImage* img_pyr2 = doPyrDown( img_pyr, IPL_GAUSSIAN_5x5 );
  IplImage* img_cny  = doCanny( img_pyr2, 10, 100, 3 );
  cvNamedWindow("Example Gray", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Pyr", CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example Gray", img_gry );
  cvShowImage("Example Pyr", img_pyr2 );
  cvShowImage("Example Canny", img_cny );
  cvWaitKey(0);
  cvReleaseImage( &img_rgb);
  cvReleaseImage( &img_gry);
  cvReleaseImage( &img_pyr);
  cvReleaseImage( &img_pyr2);
  cvReleaseImage( &img_cny);
  cvDestroyWindow("Example Gray");
  cvDestroyWindow("Example Pyr");
  cvDestroyWindow("Example Canny");
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:162229 發(fā)表于 2017-3-17 21:30 | 只看該作者
雖然看不懂,但是感覺好厲害的樣子。!
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表