计算机视觉(1)——卷积与opencv

    这篇文章中我介绍了我初学opencv的知识与体会
    文章开头,先放一张图!
    [Canny,Sobel](https://img-blog.csdn.net/20161122090108624)

**详细代码:**

一、摄像机与视频的读取

    VideoCapture cap(0);//有数字读摄像头,路径读视频
    while (true)
    {
        Mat frame;
        cap >> frame;//将读取的每一帧图片放入frame
        namedWindow("123", 0);//0可改变大小,1不//创建窗口
        imshow("123", frame);//在123窗口上输出图像
        waitKey(30);//30代表30ms,用于增强视频的连贯性,0代表无限延迟。常用30到60
    }
二、读取图片 与其中的像素值
//灰度图0255,彩图比灰度图多三倍
    Mat p1;
    p1 = imread("1.jpg", 1);//1彩图,0灰度图
    cvtColor(p1, p1, CV_RGB2GRAY);//颜色转换,cvtColor("输入素材Mat","输出素材Mat","颜色空间转换参数")
    imshow("123", p1);
    waitKey(0);
三、Mat对象一些操作
Mat image = Mat(5, 5, CV_64FC1);//5*5静态二维数组
    Mat image = Mat::zeros(5, 5, CV_64FC1);//zeros,eye,ones//0,1,单位矩阵
    cout << image << endl;
    Mat image1 = Mat::ones(5,5, CV_64FC1);
    cout << image1 << endl;
    Mat image2 = image +image1;//+,-,*亦可(大小一样)
    cout << image2 << endl;
    cout << image.at<double>(0, 0) << endl;

    image.copyTo.t;//t表示转置
四、图像x方向求导的卷积与非卷积操作
VideoCapture cop1(0);
    while (true)
    {
        Mat frame;
        cop1 >> frame;
        cvtColor(frame, frame, CV_RGB2GRAY);
        cout << "row" << frame.rows << "col" << frame.cols << endl;

        /*对矩阵求导,公式如下:
        Dx(x,y)=f(x+1,y)-f(x-1,y);
        Dy(x,y)=f(x,y+1)-f(x,y-1);
        */
        Mat dimg = Mat(frame.rows, frame.cols - 2, CV_8UC1);
        for (int i = 0; i < fream.rows; i++)
        {
            for (int j = 1; j < fream.cols - 1; j++)
            {
                dimg.at<uchar>(i, j-1) = fream.at<uchar>(i, j - 1) - fream.at<uchar>(i, j + 1);
            }
        }
        Mat dimg = Mat(frame.rows, frame.cols - 2, CV_8UC1);
        Mat model = Mat(1, 3, CV_64FC1);//定义一个卷积模板

        //初始化模板
        model.at<double>(0, 0) = 1;
        model.at<double>(0, 1) = 0;
        model.at<double>(0, 2) = -1;

        //卷积操作
        for (int i = 0; i<frame.rows; i++)
            {
            for (int j = 1; j<frame.cols - 1; j++)
            {
                int half = model.cols / 2;
                double sum = 0;
                for (int m = 0; m<model.rows; m++)
                {
                    for (int n = -half; n<model.cols - half; n++)
                    {
                        sum += (double)(frame.at<uchar>(i + m, j + n))*model.at<double>(m, n + half);//(double)为强制转化成double类型
                    }
                }
                dimg.at<uchar>(i, j - 1) = (uchar)sum;
            }

        }


        imshow("【灰度图】", frame);
        imshow("【求导后图】", dimg);

        waitKey(30);
    }


五、高斯模糊的核创建与卷积操作
//高斯卷积
    double sigma = 50;
    Mat gauss(5, 5, CV_64FC1);
    for (int i = -2; i<3; i++)
    {
        for (int j = -2; j<3; j++)
        {
            gauss.at<double>(i + 2, j + 2) = exp(-(i*i + j*j) / (2 * sigma*sigma));//正态分布公式
        }
    }

    double gssum = sum(gauss).val[0];//求和
    //归一化(平均)
    for (int i = -2; i<3; i++)
    {
        for (int j = -2; j<3; j++)
        {
            gauss.at<double>(i + 2, j + 2) /= gssum;
        }
    }

    cout<<gauss<<endl;

    VideoCapture cap2(0);

    while (true)
        {

        Mat frame;
        cap2 >> frame;
        cvtColor(frame, frame, CV_RGB2GRAY);
        Mat dimg = Mat(frame.rows - 4, frame.cols - 4, CV_8UC1);
        //卷积操作
        for (int i = 2; i<frame.rows - 2; i++)
        {
            for (int j = 2; j<frame.cols - 2; j++)
            {
                double sum = 0;

                for (int m = 0; m<gauss.rows; m++)
                {
                    for (int n = 0; n<gauss.cols; n++)
                    {
                        sum += (double)(frame.at<uchar>(i + m - 2, j + n - 2))*gauss.at<double>(m, n);
                    }
                }
                dimg.at<uchar>(i - 2, j - 2) = (uchar)sum;

            }
        }


        imshow("【原图】", frame);
        imshow("gauss", dimg);
        waitKey(10);

    }
六、相关API操作
VideoCapture cop3(0);
    while (true)
    {
        Mat frame;
        cop3 >> frame; 
        cvtColor(frame, frame, CV_RGB2GRAY);
        imshow("【灰度图】", frame);
        GaussianBlur(frame, frame, Size(5, 5), 0, 0);//高斯模糊
        imshow("GaussianBlur", frame);
        Canny(frame, frame, 100, 100);//Canny边缘检验算子
        imshow("Canny", frame);
        Sobel(frame, frame,0, 1, 1);//Sobl边缘检验算子
        imshow("Sobel", frame);
        waitKey(30);
    }
[想看源码戳这里,不过你会失望的](http://download.csdn.net/detail/typedef_dc/9689477)
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页