core核心模块--随机数发生器&文字绘制

本文主要介绍如何在OpenCV中使用随机数发生器(RNG)生成均匀分布的随机数,并通过这些随机数进行图像上的文字绘制。首先,实例化RNG并产生均匀分布的随机数,接着利用随机数生成Scalar来绘制随机线条,最后用随机数定位并显示文字。参考了官方文档,提供了完整的代码实现。

1.目的
(1)使用随机数发生器(RNG)获得均匀分布的随机数
(2)使用putText函数显示文字

2.部分代码实现
(1)实例化一个随机数发生器

    //使用0xffffffff初始化随机数生成器
    RNG rng(0xffffffff)

(2)使用随机数发生器产生均匀分布

    //产生0-100的随机整数
    //uniform:均匀分布
    int min = 0;
    int max = 100;
    rng.uniform(min, max);

(3)使用随机数发生器随机产生Scalar

static Scalar RandomColor(RNG rng){
    return(Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)));
}

(4)使用随机数发生器绘制随机线条

int DrawingRandomLine(Mat& image, char windowName[], RNG rng){
    if(!image.data){
        cout << "more parameters are required!!!" << endl;
        return(-1);
    }
    int lineType = 8;
    Point X,Y;
    for(int i=0; i<100; i++){
        //使用rng.uniform获取1-500的随机整数
        X.x = rng.uniform(1,500);
        X.y = rng.uniform(1,500);
        Y.x = rng.uniform(1,500);
        Y.y = rng.uniform(1,500);
        //RandomColor(rng):随机产生颜色
        line(image, X, Y, RandomColor(rng), rng.uniform(1,10), lineType);
        imshow(windowName, image);
        //waitKey(DELAY)等待键盘输入,并延迟
        if(waitKey(DELAY)>=0){
            return(-1);
        }
    }
    return(0);
}

(4)使用随机数发生器绘制文字

int DrawingText(Mat& image, char windowName[], RNG rng){
    int lineType = 8;
    Point org;
    for(int i=0; i<100; i++){
        org.x = rng.uniform(1,500);
        org.y = rng.uniform(1,500);
        /*
        putText参数解释
        image:绘制画板
        “OpenCV Forever”:绘制的文字信息
        org:绘制的文字的左上角坐标
        rng.uniform(0,8):字体类型
        rng.uniform(1,5):文字缩放比例
        RandomColor(rng):字体颜色
        rng.uniform(1,10):字体粗细
        lineType:画线类型
        */
        putText(image, "OpenCV Forever", org, rng.uniform(0,8), rng.uniform(1,5), RandomColor(rng), rng.uniform(1,10), lineType);
        imshow(windowName, image);
        if(waitKey(DELAY)>=0){
            return(-1);
        }
    }
    waitKey(0);
    return(0);
}

3.完整代码
Random.cpp

#include "CommonInclude.h"
#define DELAY 10
static Scalar RandomColor(RNG rng){
    return(Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)));
}

int DrawingRandomLine(Mat& image, char windowName[], RNG rng){
    if(!image.data){
        cout << "more parameters are required!!!" << endl;
        return(-1);
    }
    int lineType = 8;
    Point X,Y;
    for(int i=0; i<100; i++){
        //使用rng.uniform获取1-500的随机整数
        X.x = rng.uniform(1,500);
        X.y = rng.uniform(1,500);
        Y.x = rng.uniform(1,500);
        Y.y = rng.uniform(1,500);
        line(image, X, Y, RandomColor(rng), rng.uniform(1,10), lineType);
        imshow(windowName, image);
        //waitKey(DELAY)等待键盘输入,并延迟
        if(waitKey(DELAY)>=0){
            return(-1);
        }
    }
    return(0);
}

int DrawingText(Mat& image, char windowName[], RNG rng){
    int lineType = 8;
    Point org;
    for(int i=0; i<100; i++){
        org.x = rng.uniform(1,500);
        org.y = rng.uniform(1,500);
        /*
        putText参数解释
        image:绘制画板
        “OpenCV Forever”:绘制的文字信息
        org:绘制的文字的左上角坐标
        rng.uniform(0,8):字体类型
        rng.uniform(1,5):文字缩放比例
        RandomColor(rng):字体颜色
        rng.uniform(1,10):字体粗细
        lineType:画线类型
        */
        putText(image, "OpenCV Forever", org, rng.uniform(0,8), rng.uniform(1,5), RandomColor(rng), rng.uniform(1,10), lineType);
        imshow(windowName, image);
        if(waitKey(DELAY)>=0){
            return(-1);
        }
    }
    waitKey(0);
    return(0);
}

int main(int argc, char** argv){
    int imageX;
    int cReturn;
    char windowName[] = "RandomDrawing";
    cout << "Input size of image:";
    cin >> imageX;
    Mat image = Mat::zeros(imageX,imageX,CV_8UC3);
    //实例化一个随机数生成器
    RNG rng(0xFFFFFFFF);
    cReturn = DrawingRandomLine(image, windowName, rng);
    if(cReturn == -1){
        cout << "end of the function!!!" << endl;
        return(-1);
    }
    cReturn = DrawingText(image, windowName, rng);
    if(cReturn == -1){
        cout << "end of the function!!!" << endl;
        return(-1);
    }   
    return(0);
}

CommonInclude.h

#ifndef COMMON_INCLUDE
#define COMMON_INCLUDE
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
#endif

参考文献
1.http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/random_generator_and_text/random_generator_and_text.html#drawing-2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值