[OpenCV] Samples 07: create_mask

鼠标画线,圈地,构造相关mask图片(黑白)。

支持鼠标左键右键中间键点击事件。

/*
* create_mask.cpp
*
* Author:
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
*
* This tutorial demonstrates how to make mask image (black and white).
* The program takes as input a source image and ouputs its corresponding
* mask image.
*/

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include <iostream>
#include <stdlib.h>

using namespace std;
using namespace cv;

Mat img0, img1, res1, final;

Point point;
int drag = 0;

int numpts = 100;
Point* pts = new Point[100];

int var = 0;
int flag = 0;
int flag1 = 0;

int minx,miny,maxx,maxy,lenx,leny;

int channel;

void mouseHandler(int, int, int, int, void*);

void mouseHandler(int event, int x, int y, int, void*)
{
    // Jeff --> Click Left button: status down.
    if (event == EVENT_LBUTTONDOWN && !drag)
    {
        if(flag1 == 0)
        {
            if(var==0)
                img1 = img0.clone();
            point = Point(x, y);
            circle(img1,point,2,Scalar(0, 0, 255),-1, 8, 0);
            pts[var] = point;
            var++;
            drag  = 1;
            if(var>1)
                line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);

            imshow("Source", img1);
        }
    }

    // Jeff --> Click Left button: status up.
    if (event == EVENT_LBUTTONUP && drag)
    {
        imshow("Source", img1);

        drag = 0;
    }

    /*
     * Jeff --> Click Right button.
     */
    if (event == EVENT_RBUTTONDOWN)
    {
        flag1 = 1;
        img1 = img0.clone();
        for(int i = var; i < numpts ; i++)
            pts[i] = point;

        if(var!=0)
        {
            const Point* pts3[1] = {&pts[0]};
            polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
        }

        for(int i=0;i<var;i++)
        {
            minx = min(minx,pts[i].x);
            maxx = max(maxx,pts[i].x);
            miny = min(miny,pts[i].y);
            maxy = max(maxy,pts[i].y);
        }
        lenx = maxx - minx;
        leny = maxy - miny;

        imshow("Source", img1);
    }

    if (event == EVENT_RBUTTONUP)
    {
        flag = var;

        final = Mat::zeros(img0.size(),CV_8UC3);
        res1 = Mat::zeros(img0.size(),CV_8UC1);
        const Point* pts4[1] = {&pts[0]};

        fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
        bitwise_and(img0, img0, final,res1);
        imshow("mask",res1);
        imwrite("mask.png",res1);

        imshow("Source", img1);

    }
    if (event == EVENT_MBUTTONDOWN)
    {
        for(int i = 0; i < numpts ; i++)
        {
            pts[i].x=0;
            pts[i].y=0;
        }
        var = 0;
        flag1 = 0;
        minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
        imshow("Source", img0);
        drag = 0;
    }
}

int main(int argc, char **argv)
{
    cv::CommandLineParser parser(argc, argv, "{help h | | show help message}{@input | | input image}");
    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }
    string input_image = parser.get<string>("@input");
    if (input_image.empty())
    {
        parser.printMessage();
        parser.printErrors();
        return 0;
    }

    Mat src = imread(input_image);

    minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;

    img0 = src;

    // Jeff --> number of channels.
    channel = img0.channels();
    cout << "channel = " << channel << endl;

    res1 = Mat::zeros(img0.size(),CV_8UC1);
    final = Mat::zeros(img0.size(),CV_8UC3);
    //////////// source image ///////////////////

    namedWindow("Source", 1);

    /*
     * Jeff --> listen to mouse event.
     */
    setMouseCallback("Source", mouseHandler, NULL);

    imshow("Source", img0);
    waitKey(0);

    img0.release();
    img1.release();
}
时间: 2024-08-11 03:33:08

[OpenCV] Samples 07: create_mask的相关文章

[OpenCV] Samples 10: imagelist_creator

yaml写法的简单例子.将 $ ./ 1 2 3 4 5 命令的参数(代表图片地址)写入yaml中. 写yaml文件. 参考:[OpenCV] Samples 06: [ML] logistic regression 读xml文件. { /* * Jeff --> Load xml. * transform to Mat. * FileStorage. */ cout << "loading the dataset..."; // Step 1. FileStorag

[OpenCV] Samples 05: convexhull

得到了复杂轮廓往往不适合特征的检测,这里再介绍一个点集凸包络的提取函数convexHull,输入参数就可以是contours组中的一个轮廓,返回外凸包络的点集 ---- 如此就能去掉凹进去的边. 对于凸包算法,其中最有名的莫过于Graham扫描算法,它的复杂度为nlog(n) 参考:计算几何之凸包(Algorithm show), 寻找轮廓 高级:Snake模型在轮廓提取中的应用 cvSnakeImage() #include "opencv2/imgproc/imgproc.hpp"

[OpenCV] Samples 13: opencv_version

cv::CommandLineParser的使用. I suppose CommandLineParser::has("something") should be true when the command line has --something in it. ./a.out -h ./a.out --help 打印keys的相关内容. #include <opencv2/core/utility.hpp> #include <iostream> using

Android学习七---Hello OpenCV samples

创建一个能够使用OpenCV JavaCameraView的应用程序来了解基于OpenCV java API 的应用程序的开发流程.有了Android的基础,在程序中需要修改的几个地方1.activity_main.xml 2.AndroidManifest.xml 3.MainActivity.java 一.创建项目 安装创建android程序的方式创建一个blank activity,项目名称为hellosamples,其他采用默认的activity_main.xml,MainActivit

[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>

[OpenCV] Samples 04: contours2

要先变为二值图像:cvThreshold 提取轮廓:cvFindContours #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <math.h> #include <iostream> using namespace cv; using namespace std; static void help() { cout

[OpenCV] Samples 11: image sequence

一帧一帧地读取视频流. VideoCapture sequence(file_video); sequence >> image. #include <opencv2/core/core.hpp> #include <opencv2/videoio/videoio.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using na

[OpenCV] Samples 08: edge

Canny edge detector 效率高,效果可控. TrackBar的使用. 技巧:gray找边缘后作为mask去CopyTo(). #include "opencv2/core/utility.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include <

Windows7在Eclipse中配置Python2.7+OpenCV

1.从http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u2-download-1377129.html下载jdk-7u2-windows-i586.exe,安装到D:\ProgramFiles\Java, 并将D:\ProgramFiles\Java\jdk1.7.0_02\bin添加到环境变量中: 2. 从http://www.eclipse.org/downloads/下载Eclipse Classic版本并解压缩: