[OpenCV] Install openCV in Qt Creator

Learn openCV.pdf


qmake: link with opencv (Key Point)

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = displayimage
TEMPLATE = app

INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui

SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>  //add

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cv::Mat inputImage = cv::imread("/home/unsw/Pictures/me/lolo.JPG");  //add
    cv::imshow("Display Image", inputImage);  //add
}

MainWindow::~MainWindow()
{
    delete ui;
}

 


ch2_ex2_1: 打开并显示图片

#include "highgui.h"

int main( int argc, char** argv )
{
    // IplImage* img = cvLoadImage( argv[1] );
    IplImage* img = cvLoadImage( "/home/unsw/Pictures/me/lolo.JPG",
                                 CV_WINDOW_AUTOSIZE);

    cvNamedWindow("lolo", CV_WINDOW_FREERATIO);
    cvShowImage("lolo", img );
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow("lolo");
}

 


ch2_ex2_2: 打开并显示视频

int main( int argc, char** argv ) {
    cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
    //CvCapture* capture = cvCaptureFromAVI( argv[1] ); // either one will work
    CvCapture* capture = cvCreateFileCapture( "/home/unsw/test.avi" ); // 放每一帧的buf
    IplImage* frame;
    while(1) {
        frame = cvQueryFrame( capture );
        if( !frame ) break;
        cvShowImage( "Example2", frame );  //不需要手动释放,capture的释放即顺便将其释放
        char c = cvWaitKey(33);        //33ms, 30frames per second.
        if( c == 27 ) break;
    }
    cvReleaseCapture( &capture );
    cvDestroyWindow( "Example2" );
}

 


ch2_ex2_3: 打开并显示视频 + Control Bar

可以使用QT的图形界面控制,非重点here。

 


ch2_ex2_4: 平滑处理图片

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

void example2_4( IplImage* image )
{
    // Create some windows to show the input
    // 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,0,0 );
    cvSmooth( out, out, CV_GAUSSIAN, 5,5,0,0);
    
    // 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 )
{
  IplImage* img = cvLoadImage( argv[1] );
  cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
  cvShowImage("Example1", img );
  example2_4( img );
//  cvWaitKey(0);
  cvReleaseImage( &img );
  cvDestroyWindow("Example1");
}

 


 ch2_ex2_8: 灰度化(Gray) --> 缩小图片(Pyr) --> 线条化(Canny)

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

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 )
{
  cvNamedWindow("Example Gray",  CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Pyr",   CV_WINDOW_AUTOSIZE );
  cvNamedWindow("Example Canny", CV_WINDOW_AUTOSIZE );
  IplImage* img_rgb = cvLoadImage( "/home/unsw/lolo.jpg" );
  IplImage* out;

  out = cvCreateImage( cvSize( img_rgb->width,img_rgb->height ), img_rgb->depth, 1);
  cvCvtColor(img_rgb, out ,CV_BGR2GRAY);
  cvShowImage("Example Gray", out );
out = doPyrDown( out ); out = doPyrDown( out ); cvShowImage("Example Pyr", out );
out = doCanny( out, 10, 100, 3 ); cvShowImage("Example Canny", out ); cvWaitKey(0); cvReleaseImage( &out); cvDestroyWindow("Example Gray"); cvDestroyWindow("Example Pyr"); cvDestroyWindow("Example Canny"); }

 


 ch2_ex2_9: Camera: Preview & Capture

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

int main( int argc, char** argv ) {
    cvNamedWindow( "Example2_9", CV_WINDOW_AUTOSIZE );
    CvCapture* capture;
if (argc==1) { capture = cvCreateCameraCapture( 0 ); } else { capture = cvCreateFileCapture( argv[1] ); } assert( capture != NULL ); IplImage* frame; while(1) { frame = cvQueryFrame( capture ); if( !frame ) break; cvShowImage( "Example2_9", frame ); char c = cvWaitKey(40); if( c == 27 ) break; } cvReleaseCapture( &capture ); cvDestroyWindow( "Example2_9" ); }

 


 ch2_ex2_10: 写入视频文件

#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <iostream>

using namespace std;

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

//#define NOWRITE 1;   //Turn this on (removed the first comment out "//" if you can't write on linux

main( int argc, char* argv[] ) {
cvNamedWindow(
"Example2_10", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Log_Polar", CV_WINDOW_AUTOSIZE ); CvCapture* capture = cvCreateFileCapture( "/home/unsw/test.avi" ); if (!capture){ return -1; } IplImage* bgr_frame; double fps = cvGetCaptureProperty ( capture, CV_CAP_PROP_FPS ); printf("fps=%d\n",(int)fps); CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) ); printf("frame (w, h) = (%d, %d)\n",size.width,size.height); #ifndef NOWRITE CvVideoWriter* writer = cvCreateVideoWriter( // On linux Will only work if you've installed ffmpeg development files correctly, "/home/unsw/test_out.avi", // otherwise segmentation fault. Windows probably better. CV_FOURCC('D','X','5','0'), fps, size ); #endif IplImage* logpolar_frame = cvCreateImage( size, IPL_DEPTH_8U, 3 ); IplImage* gray_frame = cvCreateImage( size, IPL_DEPTH_8U, 1 ); while( (bgr_frame=cvQueryFrame(capture)) != NULL ) { cvShowImage( "Example2_10", bgr_frame ); cvConvertImage( //We never make use of this gray image bgr_frame, gray_frame, CV_RGB2GRAY ); cvLogPolar( bgr_frame, logpolar_frame, //This is just a fun conversion the mimic's the human visual system cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS ); cvShowImage( "Log_Polar", logpolar_frame ); //Sigh, on linux, depending on your ffmpeg, this often won't work ... #ifndef NOWRITE cvWriteToAVI( writer, logpolar_frame ); #endif char c = cvWaitKey(10); if( c == 27 ) break; } #ifndef NOWRITE cvReleaseVideoWriter( &writer ); #endif cvReleaseImage( &gray_frame ); cvReleaseImage( &logpolar_frame ); cvReleaseCapture( &capture ); }

 

posted @ 2016-07-06 21:33  郝壹贰叁  阅读(581)  评论(0编辑  收藏  举报