"人工智障 机器瞎学 数据掩埋" 入门笔记

OpenCV3最常用的基本操作

2018-03-20  本文已影响3人  HeoLis

OpenCV 介绍

OpenCV的全称是Open Source Computer Vision Library,是一个跨平台的计算机视觉库。OpenCV是由英特尔公司发起并参与开发,以BSD许可证授权发行,可以在商业和研究领域中免费使用。OpenCV可用于开发实时的图像处理、计算机视觉以及模式识别程序。该程序库也可以使用英特尔公司的IPP进行加速处理。

以上是维基百科关于OpenCV的介绍,简单来说它就是处理图像的工具包。本篇文章将介绍它的最基本操作。使用的是C++版的OpenCV3。

本篇文章将分为两个部分:

1.图像载入、显示和输出到文件以及滑块的使用

imread()函数

图像载入

Mat imread(const string& filename, int flags = 1);

第一个参数为文件名
第二个参数为载入标识

imshow()函数

图像显示

void imshow(const string& winname, InputArray mat);

namedWindow函数

初始化一个显示窗口

void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE)

可以调用destroyWindow()或者destroyAllWindows()函数来关闭窗口,并取消之前分配的与窗口相关的所有内存空间。

addWeighted 函数

图片融合

void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)

六个参数分别是

createTrackbar函数

创建滑条

CV_EXPORTS int createTrackbar(const string& trackbarname,
                    const string& winname,  
                    int* value, int count,  
                    TrackbarCallback onChange = 0,  
                    void* userdata = 0);  

六个参数如下

getTrackbarPos 函数

获取滑条位置

int getTrackbarPos(const string& trackbarname, const string& winname);

鼠标操作 setMouseCallback

该函数的作用是为指定窗口设置鼠标回调函数

void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata=0 )

2.基本绘图函数

线段:line 函数

CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineT ype=8, int shift=0);

椭圆:ellipse 函数

CV_EXPORTS_W void ellipse(CV_IN_OUT Mat& img, Point center, Size axes,double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1,int lineType=8, int shift=0);  

矩形:rectangle 函数

CV_EXPORTS_W void rectangle(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0);  

圆:circle 函数

CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0);  

填充多边形:fillPoly 函数

CV_EXPORTS void fillPoly(Mat& img, const Point** pts,const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() );  

显示文字:PutText 函数

void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )

FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN,
FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX,
FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL,
FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX,
以上所有类型都可以配合FONT_HERSHEY_ITALIC使用,产生斜体效果。

代码示例

#include<core.hpp>
#include<highgui.hpp>
#include<imgproc.hpp>
using namespace cv;

#define WINDOW_NAME1 "[绘制图1]"   //为窗口标题定义的宏
#define WINDOW_NAME2 "[绘制图2]"   //为窗口标题定义的宏
#define WINDOW_WIDTH 600            //定义窗口大小的宏

//绘制不同角度。相同尺寸的椭圆
void DrawEllipse(Mat img, double angle)
{
    int thickness = 2;
    int lineType = 8;
    ellipse(img, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2),
            Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16), angle,
            0, 360, Scalar(255, 129, 0), thickness, lineType);
}

//绘制实心圆
void DrawFilledCircle(Mat img, Point center)
{
    int thickness = -1; //线粗
    int lineType = 8;
    circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255),
            thickness, lineType);
}

//实现凹多边形绘制
void DrawPolygon(Mat img)
{
    int lineType = 8;

    //创建一些点
    Point rootPoints[1][20];
    rootPoints[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
    rootPoints[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
    rootPoints[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
    rootPoints[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
    rootPoints[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][5] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
    rootPoints[0][7] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][8] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][10] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][11] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][12] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][13] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][14] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
    rootPoints[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][17] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][18] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
    rootPoints[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
    const Point* ppt[1] = { rootPoints[0] };
    int npt[] = { 20 };
    fillPoly(img, ppt, npt, 1, Scalar(255, 255, 255), lineType);
}

// 绘制线
void DrawLine(Mat img, Point start, Point end)
{
    int thickness = 2;
    int lineType = 8;
    line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}


int main()
{
    //创建空白的Mat图像
    Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
    Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

    //----------- 绘制化学中原子的示例图-----------
    //1.先绘制出椭圆
    DrawEllipse(atomImage, 90);
    DrawEllipse(atomImage, 0);
    DrawEllipse(atomImage, 45);
    DrawEllipse(atomImage, -45);

    //2.再绘制出圆心
    DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));

    //----------- 绘制组合图形----------------
    //1.线绘制出多边形
    DrawPolygon(rookImage);

    //2.绘制矩形
    rectangle(rookImage, Point(0, 7 * WINDOW_WIDTH),
            Point(WINDOW_WIDTH, WINDOW_WIDTH), Scalar(0, 255, 255), -1, 8);

    //2.绘制一些线段
    DrawLine(rookImage, Point(0, 15 * WINDOW_WIDTH / 16),
        Point(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));
    DrawLine(rookImage, Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8),
             Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));
    DrawLine(rookImage, Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8),
             Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));
    DrawLine(rookImage, Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8),
             Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));

    //3.显示绘制出的图像
    imshow(WINDOW_NAME1, atomImage);
    moveWindow(WINDOW_NAME1, 0, 200);
    imshow(WINDOW_NAME2, rookImage);
    moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);

    waitKey(0);
    return 0;
}

欢迎关注我的公众号(GainAnEpoch),获取更多新手入门笔记。

上一篇 下一篇

猜你喜欢

热点阅读