美文网首页
OPENCV入门试水

OPENCV入门试水

作者: 看风景的人_21744 | 来源:发表于2017-12-12 21:43 被阅读0次

#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main( int argc, char** argv ) {
Mat img = imread( argv[1], -1 );
if( img.empty() ) return -1;
namedWindow( "Example1", cv::WINDOW_AUTOSIZE );
imshow( "Example1", img );
waitKey( 0 );
destroyWindow( "Example1" );
}
  • imread():第二个参数是flag。flag>0返回三通道图像,flag=0返回单通道图像,flag<0原图像。
  • namedWindow( "Example1", cv::WINDOW_AUTOSIZE )默认就是这个(不能调整大小)。可以写成namedWindow( "Example1")。其他选择:WINDOW_NORMALWINDOW_AUTOSIZEWINDOW_FREERATIOWINDOW_KEEPRATIO
  • destroyWindow( "Example1" ):小程序就不必了,因为Mat会自己销毁。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
int main( int argc, char** argv ) {
cv::namedWindow( "Example3", cv::WINDOW_AUTOSIZE );
cv::VideoCapture cap;
cap.open( string(argv[1]) );
cv::Mat frame;
for(;;) {
cap >> frame;
if( frame.empty() ) break;
cv::imshow( "Example3", frame );
if( cv::waitKey(33) >= 0 ) break;
}
// Ran out of film
return 0;
}
  • cv::waitKey(33) >= 0:It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.如果没有按下键盘,则33ms后返回-1。否则返回按键值(大于1)。

Adding a trackbar slider to the basic viewer window for moving around within the video file

#include"opencv2/opencv.hpp"
#include<iostream>
#include<fstream>

using namespace std;
using namespace cv;

int g_slider_position = 0;
int g_run = 1, g_dontset = 0; //start out in single step mode
cv::VideoCapture g_cap;

void onTrackbarSlide( int pos, void *) {
    g_cap.set(1, pos ); //g_cap.set( cv::CAP_PROP_POS_FRAMES, pos );
    if( !g_dontset )
        g_run = 1;
    g_dontset = 0;

}

int main() {

    cv::namedWindow( "Example2_4", cv::WINDOW_AUTOSIZE );
    g_cap.open("/home/lsy/test.mp4");
    int frames = (int) g_cap.get(7);//cv::CAP_PROP_FRAME_COUNT
    int tmpw = (int) g_cap.get(3);//cv::CAP_PROP_FRAME_WIDTH
    int tmph = (int) g_cap.get(4);//cv::CAP_PROP_FRAME_HEIGHT

    cout << "Video has " << frames << " frames of dimensions("
         << tmpw << ", " << tmph << ")." << endl;

    cv::createTrackbar("Position", "Example2_4", &g_slider_position, frames,
                onTrackbarSlide);

    cv::Mat frame;
    for(;;) {

        if( g_run != 0 ) {

            g_cap >> frame;
            if(frame.empty()) break;
            int current_pos = (int)g_cap.get(1);//cv::CAP_PROP_POS_FRAMES
            g_dontset = 1;
            cv::setTrackbarPos("Position", "Example2_4", current_pos);
            cv::imshow( "Example2_4", frame );
            g_run-=1;

        }

        char c = (char) cv::waitKey(10);
        if( c == 's' ) // single step
            {g_run = 1; cout << "Single step, run = " << g_run << endl;}
        if( c == 'r' ) // run mode
            {g_run = -1; cout << "Run mode, run = " << g_run <<endl;}
        if( c == 27 )
            break;

    }
return(0);

}

  • cv::createTrackbar("Position", "Example2_4", &g_slider_position, frames, onTrackbarSlide);:bar的名字、窗口名、滑条值、滑条最大值、相对应的函数( Foo(int,void*))
  • setTrackbarPos("Position", "Example2_4", current_pos);:定位滑条位置
  • 吐槽:cv::CAP_PROP_POS_FRAMES这些我不能用,只能用数值代替

Loading and then smoothing an image before it is displayed on the screen

#include <opencv2/opencv.hpp>

using namespace cv;
int main(){

    cv::namedWindow( "Example2_5-in", cv::WINDOW_AUTOSIZE );
    cv::namedWindow( "Example2_5-out", cv::WINDOW_AUTOSIZE );

    Mat img = imread("/home/lsy/girl.jpg");
    cv::imshow( "Example2_5-in",  img);

    Mat out;
    GaussianBlur( img, out, cv::Size(5,5), 3, 3);
    imshow( "Example2_5-out", out );
    waitKey( 0 );

}
  • GaussianBlur( img, out, cv::Size(5,5), 3, 3):src、dst、大小、x方向标准差、y方向标准差

Using cv::pyrDown() to create a new image that is half the width and
height of the input image

  • pyrDown( img,out ):模糊然后采样。

The Canny edge detector writes its output to a single-channel (grayscale)
image

  • cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY):最后是类型flag

Getting and setting pixels

  • cv::Vec3b intensity = img_rgb.at< cv::Vec3b >(y, x)
    uchar blue = intensity[0]:不同函数的坐标(x,y)意义不同

相关文章

网友评论

      本文标题:OPENCV入门试水

      本文链接:https://www.haomeiwen.com/subject/kvzkixtx.html