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();
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() {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );  //不需要手动释放,capture的释放即顺便将其释放
char c = cvWaitKey();        //33ms, 30frames per second.
if( c == ) 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, ); // Do the smoothing
//
cvSmooth( image, out, CV_GAUSSIAN, ,5,0,0 );
cvSmooth( out, out, CV_GAUSSIAN, ,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( );
cvDestroyWindow("Example2_4-in" );
cvDestroyWindow("Example2_4-out" ); } int main( int argc, char** argv )
{
IplImage* img = cvLoadImage( argv[] );
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,
);
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% == && in->height% == ); IplImage* out = cvCreateImage(
cvSize( in->width/, in->height/ ),
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, );
cvCvtColor(img_rgb, out ,CV_BGR2GRAY);
cvShowImage("Example Gray", out );
out = doPyrDown( out );
out = doPyrDown( out );
cvShowImage("Example Pyr", out );
out = doCanny( out, , , );
cvShowImage("Example Canny", out ); cvWaitKey();
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==) {
capture = cvCreateCameraCapture( );
} else {
capture = cvCreateFileCapture( argv[] );
}
assert( capture != NULL ); IplImage* frame;
while() {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2_9", frame );
char c = cvWaitKey(4);
if( c == ) 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 -;
}
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','',''),
fps,
size
);
#endif
IplImage* logpolar_frame = cvCreateImage(
size,
IPL_DEPTH_8U, ); IplImage* gray_frame = cvCreateImage(
size,
IPL_DEPTH_8U, ); 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/,
bgr_frame->height/),
,
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();
if( c == ) break;
}
#ifndef NOWRITE
cvReleaseVideoWriter( &writer );
#endif
cvReleaseImage( &gray_frame );
cvReleaseImage( &logpolar_frame );
cvReleaseCapture( &capture );
}

[OpenCV] Install openCV in Qt Creator的更多相关文章

  1. [OpenCV] Install OpenCV 3.3 with DNN

    OpenCV 3.3 Aug 3, 2017 OpenCV 3.3 has been released with greatly improved Deep Learning module and l ...

  2. [OpenCV] Install OpenCV 3.4 with DNN

    目标定位 一.开始全面支持 Tensorflow OpenCV3.4 新功能 当前最新进展OpenCV 3.4 dev:https://github.com/opencv/opencv/tree/ma ...

  3. Install OpenCV 3.0 and Python 2.7+ on OSX

    http://www.pyimagesearch.com/2015/06/15/install-OpenCV-3-0-and-Python-2-7-on-osx/ As I mentioned las ...

  4. Install OpenCV 3.0 and Python 2.7+ on Ubuntu

    为了防止原文消失或者被墙,转载留个底,最好还是去看原贴,因为随着版本变化,原贴是有人维护升级的 http://www.pyimagesearch.com/2015/06/22/install-Open ...

  5. QT creator+OpenCV2.4.2+MinGW 在windows下开发环境配置

    由于项目开发的原因,需要配置QT creator+OpenCV2.4.2+MinGW开发环境,现对配置方法做如下总结: 1.  下载必备软件 QT SDK for Open Source C++ de ...

  6. QT creator中使用opencv

    最近要用到opencv做图像方面的东西,网上很多是用VS加opencv,但自己对VS不怎么喜欢,想用QT Creator.在网上搜索了很多资料,终于花了一天的时间,在QT Creator上能使用ope ...

  7. OpenCV编译以及QT Creator配置

    OpenCV编译以及QT Creator配置 在进行编译前,需下载以下工具和源码: CMake ---- 用于编译: 下载地址; https://cmake.org/ 安装在D:\Program Fi ...

  8. Windows下OpenCV 3.1.0 在 Qt Creator 4.0.2 (Qt 5.7.0 MinGW) 中的开发环境配置

    2017-2-23 Update: 修改并添加了部分细节 最近正在学习OpenCV ,为毕业设计做准备.Windows版本的OpenCV都默认提供对VS的支持,其在VS中的配置比较简单,网上也有大批教 ...

  9. QT creator中使用opencv采集摄像头信息

    之前在QT creator上成功编译了opencv,由于课题需要,需要采集摄像头的信息.故搜集了网上的一些资料,依葫芦画瓢的照着做了一下,终于简单的成功采集了信息. 打开QTcreator,新建一个w ...

随机推荐

  1. android: 使用前台服务

    9.5.1    使用前台服务 服务几乎都是在后台运行的,一直以来它都是默默地做着辛苦的工作.但是服务的系统 优先级还是比较低的,当系统出现内存不足的情况时,就有可能会回收掉正在后台运行的服 务.如果 ...

  2. Java开源框架推荐(全)

    Build Tool Tools which handle the buildcycle of an application. Apache Maven - Declarative build and ...

  3. file_get_contents高級用法

    首先解決file_get_contents的超時問題,在超時返回錯誤後就象js中的settimeout那樣進行一次嘗試,錯誤超過3次或者5次後就確認為無法連線伺服器而徹底放棄.這裡就簡單介紹兩種解決方 ...

  4. Bitbucket Repository size limits

    Repository size limits By Justen Stepka, Product Manager on May 30, 2014 In order to improve and mai ...

  5. Speech两种使用方法

    COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...

  6. Swift入门篇-循环语句

    今天早上一起来所有新闻都是报道荷兰5-1战胜西班牙,我一看没有搞错吧,顿时想都如果中国队vs荷兰队也不至于会输的怎么惨吧,难道是荷兰队开挂了,于是我看了一下昨天比赛的视频直播,还真是新闻报道的那样,顿 ...

  7. 内部通信服务Factory(WCF)

    WCF,很好,却又麻烦,很多时候不想用WCF的原因就是:用这个真麻烦... 麻烦的地方,比如: 一堆一堆的服务配置,散落在一个一个的folder下,更新系统时容易出错 客户端除了要知道WCF Cont ...

  8. scikit-learn主要模块和基本使用方法

    从网上看到一篇总结的很不错的sklearn使用文档,备份勿忘. 引言 对于一些开始搞机器学习算法有害怕下手的小朋友,该如何快速入门,这让人挺挣扎的.在从事数据科学的人中,最常用的工具就是R和Pytho ...

  9. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  10. 获取枚举类型的描述description

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...