OpenCV (c++)基本操作

#ifndef LESSON_1_STUDYCV_H
#define LESSON_1_STUDYCV_H

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect.hpp>

#include <iostream>

#define QUIT_VALUE 0x1B
const int CameraId = 0;

using  namespace std;
using namespace cv;

void show_image(const string &path)
{
    Mat img = imread(path);
    imshow("img", img);
    waitKey(0);
}

void open_vodeoOrCamera(const string &path)
{
    cout << path << endl;
    VideoCapture cap(path);
    Mat img;
    while (true) {
        cap.read(img);
        imshow("Image", img);
        if(QUIT_VALUE == waitKey(1)){
            break;
        }
    }
}

void cvtColor_demo(const string &path)
{
    Mat img = imread(path);
    Mat imgGray, imgBlur, imgCanny, imgDil;
    cvtColor(img, imgGray, COLOR_RGB2GRAY);
    GaussianBlur(img, imgBlur, Size(7, 7), 5, 0 );
    Canny(img, imgCanny, 50, 150);

    Mat kernel = getStructuringElement(MORPH_RECT, Size(5,5));
    dilate(imgCanny, imgDil, kernel);

//    imshow("img", img);
//    imshow("gray", imgGray);
//    imshow("imgBlur", imgBlur);
//    imshow("imgCanny", imgCanny);
    imshow("imgDil", imgDil);

    waitKey(0);
}

void resize_demo(const string &path)
{
    Mat img = imread(path);
    Mat resizeImg, imgCrop;

    resize(img, resizeImg, Size(), 0.5,0.5);
    Rect roi(100,100,300,250);
    imgCrop = img(roi);

    imshow("img", img);
    imshow("resizeImg", resizeImg);
    imshow("imgCrop", imgCrop);

    waitKey(0);
}

    Draw Shapes and Text   ///
void create_demo()
{
    // Black Image
    Mat img(512,512,CV_8UC3, Scalar(255,255,255)); //NOLINT

    circle(img, Point(256,256), 155,Scalar(0,69,255), FILLED);
    rectangle(img, Point(130,226), Point(328,286), Scalar(255,255,255), FILLED);
    line(img, Point(130,296), Point(328,296), Scalar(255,255,255), 2);
    putText(img, "qinzhou", Point(137,262), FONT_HERSHEY_DUPLEX, 0.75, Scalar(0,69,255), 2);
    imshow("img", img);

    waitKey(0);
}

    Warp Image   ///
float w = 250.0f, h = 350.0f;
Mat matrix, imgWarp; //NOLINT
void Warp_Image(string &path){
    Mat img = imread(path);

    Point2f src[4] = {{440,117},{644,158},
                      {337,330},{567,386}};
    Point2f dst[4] = {{0.0f,0.0f},{w,0},
                      {0,h},{w,h}};

    matrix = getPerspectiveTransform(src, dst);
    warpPerspective(img, imgWarp, matrix, Point(w, h));

    for(int i=0; i<4; i++){ // NOLINT
        circle(img, src[i], 10,Scalar(0,69,255), FILLED);
    }

    imshow("img", img);
    imshow("imgWarp", imgWarp);
    waitKey(0);
}

    颜色检测   ///
void color_detection(const string &path)
{
    Mat img = imread(path);
    Mat imgHSV, mask;
    Mat cl(512,512, static_cast<int>(CV_8UC3), Scalar(19,240,255)); //NOLINT
    imshow("cl", cl);
    int hmin = 0, smin = 110, vmin = 153;
    int hmax = 19, smax = 240, vmax = 255;//检测的颜色范围
    cvtColor(img, imgHSV, COLOR_BGR2HSV);

    namedWindow("TrackBar", (640, 200)); // NOLINT
    createTrackbar("hue min", "TrackBar", &hmin, 179);

    while(true) {
        Scalar lower(hmin, smin, vmin);
        Scalar upper(hmax, smax, vmax);
        inRange(imgHSV, lower, upper, mask);

//        imshow("img", img);
//        imshow("imgHSV", imgHSV);
        imshow("mask", mask);
        if(waitKey(0) == QUIT_VALUE){
            break;
        }
    }
}

void getContours(Mat &imgDil, Mat &img)
{
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    string objType;

    findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    //drawContours(img, contours, -1, Scalar(255,0,255), 3);

    for (int i=0; i<contours.size(); ++i){
        double area = contourArea(contours[i]); // 轮廓面积

        vector<vector<Point>> conPloy(contours.size());
        vector<Rect> boundRect(contours.size());

        if(area >= 1000){
            float_t peri = arcLength(contours[i], true); // 周长
            approxPolyDP(contours[i], conPloy[i], 0.02*peri, true); // 近似曲线或多边形
            drawContours(img, conPloy, i, Scalar(255,0,255), 3); // 画出近似曲线或多边形轮廓
            cout << conPloy[i].size() << endl;
            boundRect[i] = boundingRect(conPloy[i]); // 获取最小右上角边界矩形
            rectangle(img, boundRect[i].tl(),  boundRect[i].br(), Scalar(0,255,0), 4);

            int objCar = static_cast<int>(conPloy[i].size());
            if(objCar == 3) {   objType = "Tri";  } // 轮廓数量为3,三角形
            else if(objCar == 4)  { // 轮廓数量为3,矩形
                float_t aspRadio = static_cast<float_t>(boundRect[i].width) / static_cast<float_t>(boundRect[i].height);
                if(aspRadio > 0.95 && aspRadio < 1.05){ objType = "Square"; } // 正方形
                else objType = "Rect";
            }
            else if(objCar > 4)  {   objType = "Circle";  } // 圆

            putText(img, objType, {boundRect[i].x, boundRect[i].y - 5}, FONT_HERSHEY_DUPLEX, 0.75, Scalar(255,0,0), 2);
        }
    }
}

    轮廓检测   ///
void contours_detection(const string &path = "../images/shapes.png"){
    Mat img = imread(path);
    imshow("img", img);

    Mat imgGray, imgBlur, imgCanny, imgDil;

    // Preporocessing (预处理)
    cvtColor(img, imgGray, COLOR_RGB2GRAY);
    GaussianBlur(img, imgBlur, Size(7, 7), 5, 0 );
    Canny(img, imgCanny, 50, 150);
    Mat kernel = getStructuringElement(MORPH_RECT, Size(5,5));
    dilate(imgCanny, imgDil, kernel);

    getContours(imgDil, img);

    imshow("img", img);
    // imshow("imgGray", imgGray);
    // imshow("imgBlur", imgBlur);
    // imshow("imgCanny", imgCanny);
    // imshow("imgDil", imgDil);

    waitKey(0);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值