opencv常用库的使用

发布于:2024-05-12 ⋅ 阅读:(166) ⋅ 点赞:(0)

opencv库函数调用:opencv主要用来处理图像
    1、图像
        灰度图cvtColor(src,dst,CV_BGR2GRAY);
        彩色图cvtColor(src,dst,CV_BGR2RGB);
        像素的深度:用来表达一个像素所需要的比特数


    2、读取图像
        Mat cv::imread(const string& filename, int flags = 1);
            示例:Mat img = imread("lena.jpg");


    3、显示图像
        void cv::imshow(const string& winname, InputArray mat);
            示例:imshow("lena",img);


    4、保存图像
        void cv::imwrite(const string& filename, InputArray img, const vector<int>& params = vector<int>());
            示例:imwrite("lena.jpg",img);


    5、Mat类
        Mat的构造函数:
            Mat::Mat(int rows, int cols, int type, const Scalar& s);

        Mat::at 访问像素
        Mat::at<Vec3b>(int y, int x)
            示例: for(int i = 0;i < mat.rows;++i)
                    {
                        for(int j = 0;j < mat.cols;++j)
                        {
                            Vec3b a = {0xFF, 0xFF, 0xFF};

                            mat.at<Vec3b>(i, j) = a;
                        }
                    }
                    mat.at<Vec3b>(5, 5) = Vec3b(3,4,5);


    6、图像的翻转
        flip(origion, dst, 0);  //水平翻转
        flip(origion, dst, 1);  //竖直翻转
        flip(origion, dst, -1);     //同时翻转


    7、图像的缩放
        resize(origion, dst, Size(100,100));


    8、图像的模糊
        blur(origion, dst, Size(9,9));  //中值滤波
        9*9的卷积核
        GaussianBlur(origion, dst, Size(9,9), 0, 0);    //高斯滤波
        9*9的卷积核,0,0代表标准差

        两者的区别:高斯滤波的卷积核带有权重,中值滤波则没有权重是求平均值


    9、图像的二值化处理
        threshold(bin, bin, 127, 255, THRESH_BINARY_INV);


    10、腐蚀:
        /* 腐蚀主要用来消噪, 突兀的出现一些噪点会被腐蚀掉,
        * 原理:卷积核只要有一个背景0, 则该点直接为0
        * 膨胀:增强噪点 */
        erode(origion, dst, Mat(), Point(-1,-1), 1);    //主要作用是用来消噪的,1代表迭代次数


    11、膨胀:
        dilate(origion, dst, Mat(), Point(-1,-1), 1);
        膨胀:增强噪点


   使用opencv调用摄像头实现人脸检测定位
        #include <vector>
        int main(void)
        {
            VideoCapture cap;   //摄像头
            CascadeClassifier classiFier("D:/opencv-3.4.5-build/install/etc/haarcascades/haarcascade_frontalface_default.xml"); //人脸分类器
            Mat image;  //图像
            Mat gary;   //灰度图
            vector<Rect> vr;    //矩形框

            if (cap.open(0))    //打开摄像头
            {
                while (cap.read(image)) //读取图像
                {
                    cvtColor(image, gary, COLOR_BGR2GRAY);  //转化为灰度图
                    classiFier.detectMultiScale(gary, vr);  //检测人脸
                    
                    for (auto &x : vr)  //遍历矩形框
                    {
                        rectangle(image, x, Scalar(0,0,255), 2);    //绘制矩形框
                    }
                    imshow("cap", image);   
                    waitKey(30);    
                }
            }
            else
            {
                cout << "error!" << endl;
            }
            return 0;
        }


在QT下使用opencv实现图片人脸检测定位
    int main(void)
    {
        QString fileName = QFileDialog::getOpenFileName(this, "choose a file", "d:/", "Images (*.png *.xpm *.jpg)");    //选择文件
        if(!fileName.isEmpty()) //判断是否选择文件
        {
            Mat origion = imread(fileName.toStdString());   //读取图像
            cvtColor(origion, origion, COLOR_BGR2GRAY); //转化为灰度图
            threshold(origion, origion, 127, 255, THRESH_BINARY_INV);   //二值化
            QImage image(origion.data, origion.cols, origion.rows, origion.step, QImage::Format_Indexed8);  //转化为QImage

            QPixmap pixmap = QPixmap::fromImage(image); //转化为QPixmap
            ui->label->setPixmap(pixmap);   //显示
            ui->label->setScaledContents(true); //自适应(窗口可拖动)  
        }
    }


在QT下调用opencv库实现摄像头人脸检测定位
    if(cap.open(0)) //打开摄像头
    {
        QTimer *timer = new QTimer(this);   //定时器
        connect(timer, &QTimer::timeout, this, &MyWiget::onTimerTimeOut);   //连接信号和槽
        timer->start(30);   //30ms
    }
    void MyWiget::onTimerTimeOut()  //定时器槽函数
    {
        Mat frame;  //图像
        if (cap.read(frame))    //读取图像
        {
             cvtColor(frame, frame, COLOR_BGR2GRAY);    //转化为灰度图
             QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_Indexed8); //转化为QImage
             QPixmap pixmap =QPixmap::fromImage(image); //转化为QPixmap
             ui->label->setPixmap(pixmap);  //显示
             ui->label->setScaledContents(true);    //自适应(窗口可拖动)
        }
    }


网站公告

今日签到

点亮在社区的每一天
去签到