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. [苏飞开发助手V1.0测试版]官方教程与升级报告

           [苏飞开发助手V1.0测试版]官方教程与升级报告导读部分----------------------------------------------------------------- ...

  2. 关于PF_RING/Intel 82599/透明VPN的一些事

    接近崩溃的边缘,今天这篇文章构思地点在医院,小小又生病了,宁可吊瓶不吃药,带了笔记本却无法上网,我什么都不能干,想了解一些东西,只能用3G,不敢 开热点,因为没人给我报销流量,本周末我只有一天时间,因 ...

  3. XML 和 List 互转类

    XML 和 List 互转类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. xml转换之

    1.XStream public static <T> T toBean(String xmlStr, Class<T> cls) { XStream xstream = ne ...

  5. SVN(TortoiseSVN)提交时忽略bin跟obj目录

    SVN(TortoiseSVN)提交时忽略bin和obj目录 一般协作开发情况下,有意思无意将bin和obj目录添加到版本管理中是很烦人的事儿,在VS中不断地编译程序集和提交将带来版本暴增问题.如果你 ...

  6. AngularJS datepicker 和 datatimepicker

    本文内容 项目结构 AngularJS datepicker AngularJS+jQueryUI datetimepicker 本文介绍 AngualrJS datetimepicker 控件.说明 ...

  7. Entity Framework Core 实现读写分离

    在之前的版本中我们可用构造函数实现,其实现在的版本也一样,之前来构造连接字符串,现在相似,构造DbContextOptions<T> 代码如下: public SContext(Maste ...

  8. ubuntu 休眠之后网络间接失败 can not connect to network after suspend (wake up)

    ubuntu for laptop系统在系统休眠后wakeup 之后,网络连接失败, 有线网络无法连接, 无线wifi无法连接, 只能重启后才能恢复, 此时可以采用以下方法处理: 1. 在/etc/p ...

  9. jedis:exception is java.lang.VerifyError: Bad type on operand stack

    项目中需要用到缓存,经过比较后,选择了redis,客户端使用jedis连接,也使用到了spring提供的spring-data-redis.配置正确后启动tomcat,发现如下异常: ======== ...

  10. Cubieboard2裸机开发之(二)板载LED交替闪烁

    前言 电路原理在文章http://www.cnblogs.com/lknlfy/p/3583806.html中已经说明,两个LED的原理图是一样的.要使两个LED交替闪烁,只需要在点亮蓝色LED,熄灭 ...