1. 程式人生 > >學習c++版opencv3.4之8

學習c++版opencv3.4之8

在影象上繪製線段,矩形,圓,橢圓,文字。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;

Mat src; //全域性變數
string drawdemo = "draw line to img demo";
void Myline(); //畫線段
void MyRectangle(); //畫矩形框
void Mycircle(); //畫圓
void Myellipse(); //畫橢圓
void Myputtext(); //寫字
int main(){
    src = imread("/Users/ming/Documents/test.jpg");
//    cvtColor(src, src, CV_BGR2GRAY);
    if (! src.data){
        printf("cannot load image ...");
    }
//    namedWindow("src img", CV_WINDOW_AUTOSIZE);
//    imshow("src img", src);
    
    Myline();
    MyRectangle();
    Mycircle();
    Myellipse();
    Myputtext();
    
    namedWindow(drawdemo, CV_WINDOW_AUTOSIZE);
    imshow(drawdemo, src);
    
    waitKey(0);
    return 0;
}

void Myline(){
    Point p1, p2;
    Scalar color;
    p1 = Point(20, 60); //線的始點
    p2 = Point(100, 200); //線的終點
//    p2.x = 200;
//    p2.y = 300;
    color = Scalar(0, 0, 255);
    line(src, p1, p2, color);
}
void MyRectangle(){
    Rect rect;
    rect = Rect(100, 200, 50, 25); //矩形起點x,起點y,寬,高
    Scalar color = Scalar(255, 0, 0);
    rectangle(src, rect, color);
}
void Mycircle(){
    Point p = Point(320, 190); //圓的中心點
    int r= 50; //圓的半徑
    Scalar color = Scalar(0, 255, 0);
    circle(src, p, r, color);
}
void Myellipse(){
    Point p = Point(src.rows/2, src.cols/2); //橢圓中心點
    Size axis = Size(50, 100); //長軸,短軸大小
    Scalar color = Scalar(127, 0, 127); //顏色
    double angle = 30, startangle = 0, endangle = 180;
    ellipse(src, p, axis, angle, startangle, endangle, color); //畫橢圓
}
void Myputtext(){
    string text = "put text on image";
    Point p = Point(200, 140);
    int fontface = CV_FONT_NORMAL; //字型型別
    cout << fontface << endl ;
    double fontscale = 1.2; // 尺寸因子,值越大文字越大
    Scalar color = Scalar(0, 0, 255);
    putText(src, text, p, fontface, fontscale, color);
//    putText(<#cv::InputOutputArray img#>, <#const cv::String &text#>, <#cv::Point org#>, <#int fontFace#>, <#double fontScale#>, <#cv::Scalar color#>)
}

在黑色背景圖上隨機畫線,顏色位置隨機變化,用

RNG rng;

rng.uniform(0, width)產生隨機數 //索引從0開始,要小心

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace std;
using namespace cv;

void random_line();
Mat img;
int main(){
//    img = Mat::zeros(400, 500, CV_8UC3);  //新建黑色背景圖1
    img.create(400, 500, CV_8UC3);
    img = Scalar(0, 0, 0);  //新建黑色背景圖2
//    cout << img.channels() << endl;
    
    random_line(); //畫隨機線條
    
//    imshow("empty", img);
//    waitKey(0);
    return 0;
}

void random_line(){
    int height = img.rows;
    int width = img.cols;
    Point start_point, end_point;
    RNG rng; //RNG為隨機數生成器
    for (int i = 0; i < 1000 ; i++){
        start_point = Point(rng.uniform(0, width), rng.uniform(0, height)); //索引是從0開始,要小心
//        start_point = Point(0, 0);
        end_point = Point(rng.uniform(0, width), rng.uniform(0, height));
        cout << "start point:" << start_point;
        cout << "end point:" << end_point;
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        line(img, start_point, end_point, color);
        
        namedWindow("random line img", CV_WINDOW_AUTOSIZE);
        imshow("random line img", img);
        if (waitKey(30) > 0){
            break;
        }
    }
}