[OpenCV] Samples 12: laplace

Posted 机器学习水很深

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[OpenCV] Samples 12: laplace相关的知识,希望对你有一定的参考价值。

先模糊再laplace,也可以替换为sobel等。

变换效果后录成视频,挺好玩。

 

 

#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <opencv2/core/utility.hpp>

#include <ctype.h>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

static void help()
{
    cout <<
            "\\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\\n"
            "It captures from the camera of your choice: 0, 1, ... default 0\\n"
            "Call:\\n"
            "./laplace -c=<camera #, default 0> -p=<index of the frame to be decoded/captured next>\\n" << endl;
}

enum {GAUSSIAN, BLUR, MEDIAN};

int sigma = 3;
int smoothType = GAUSSIAN;

int main( int argc, char** argv )
{
    VideoCapture cap;
    cv::CommandLineParser parser(argc, argv, "{help h | | }{ c | 0 | }{ p | | }");
    if ( parser.has("help") )
    {
        help();
        return 0;
    }

    if( parser.get<string>("c").size() == 1 && isdigit(parser.get<string>("c")[0]) )
    {
        cout << "debug 1:" << parser.get<int>("c") << endl;
        cap.open(parser.get<int>("c"));
    }
    else
    {
        cout << "debug 2:" << parser.get<string>("c") << endl;
        cap.open(parser.get<string>("c"));
    }

    if( cap.isOpened() )
        cout << "Video " << parser.get<string>("c") <<
            ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) <<
            ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) <<
            ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl;
    // Jeff --> "nframes == -1"
    // try to return more frames?

    if( parser.has("p") )
    {
        int pos = parser.get<int>("p");
        if (!parser.check())
        {
            parser.printErrors();
            return -1;
        }
        cout << "seeking to frame #" << pos << endl;
        cap.set(CAP_PROP_POS_FRAMES, pos);
    }

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\\n";
        return -1;
    }

    namedWindow( "Laplacian", 0 );
    createTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );

    CvSize size1 = cvSize(
                (int)cap.get(CAP_PROP_FRAME_WIDTH),
                (int)cap.get(CAP_PROP_FRAME_HEIGHT));
    CvVideoWriter* wrVideo1 = cvCreateVideoWriter("./my.mp4", CV_FOURCC(\'P\',\'I\',\'M\',\'1\'), 32, size1);
    // CV_FOURCC:
    // CV_FOURCC(\'P\',\'I\',\'M\',\'1\')    MPEG-1 codec
    // CV_FOURCC(\'M\',\'J\',\'P\',\'G\')    motion-jpeg codec (does not work well)
    // CV_FOURCC(\'M\', \'P\', \'4\', \'2\') MPEG-4.2 codec
    // CV_FOURCC(\'D\', \'I\', \'V\', \'3\') MPEG-4.3 codec
    // CV_FOURCC(\'D\', \'I\', \'V\', \'X\') MPEG-4 codec
    // CV_FOURCC(\'U\', \'2\', \'6\', \'3\') H263 codec
    // CV_FOURCC(\'I\', \'2\', \'6\', \'3\') H263I codec
    // CV_FOURCC(\'F\', \'L\', \'V\', \'1\') FLV1 codec




    Mat smoothed, laplace, result;

    for(;;)
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;
        /*
         * Step 1: 高斯,均值, 中值
         */
        int ksize = (sigma*5)|1;
        if(smoothType == GAUSSIAN)
            GaussianBlur(frame, smoothed, Size(ksize, ksize), sigma, sigma);
        else if(smoothType == BLUR)
            blur(frame, smoothed, Size(ksize, ksize));
        else
            medianBlur(frame, smoothed, ksize);

        /*
         * Step 2: extract edges.
         *     In: smoothed.
         *    Out: laplace.
         */
        Laplacian(smoothed, laplace, CV_16S, 5);
        convertScaleAbs(laplace, result, (sigma+1)*0.25);
        imshow("Laplacian", result);

        /*
         * Jeff --> We may try any other Algorithm here,
         * such as sobel.


        int c = waitKey(30);
        if( c == \' \' )
            smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
        if( c == \'q\' || c == \'Q\' || (c & 255) == 27 )
            break;

        IplImage copy = result;
        IplImage* new_image = copy;
        cvWriteFrame( wrVideo1, new_image );
    }

    cvReleaseVideoWriter( &wrVideo1 );
    cvDestroyWindow( "Laplacian" );

    return 0;
}

  

 

以上是关于[OpenCV] Samples 12: laplace的主要内容,如果未能解决你的问题,请参考以下文章

[OpenCV] Samples 08: edge

[OpenCV] Samples 13: opencv_version

OpenCV 的“calcCovarMatrix”函数中“const Mat* samples”参数的工作原理?

[OpenCV] Samples 10: imagelist_creator

[OpenCV] Samples 04: contours2

[OpenCV] Samples 07: create_mask