计算机视觉学习(1)

来源:互联网 发布:炎黄网络 怎么样 编辑:程序博客网 时间:2024/04/29 18:37


总结一下那天下午学的计算机视觉的内容

1,打开摄像机,可以看到帅帅的自己害羞,也可以读取视频

       VideoCapture cap(0);//打开摄像头,0表示主摄像头,1是外接摄像头while (true){Mat frame;cap >> frame;//打开一帧namedWindow("string",1);//0可以伸缩窗口imshow("string", frame);waitKey(10);//等待

2.可以读取图片与像素值

       Mat imggray = imread("123.jpg", 1);//1表示三通道的彩图,0表示灰度图cvtColor(imggray, imggray, CV_RGB2GRAY);//转化为灰度图//cout <<(int) imggray.at<uchar>(1, 1) << endl;//读取(1,1)像素值,强转为int imshow("123", imggray);waitKey(10);system("pause");

3.对Mat对象的操作,以及矩阵的加减乘,逆,转置

       Mat imag1 = Mat(5, 5, CV_64FC1);//二维数组Mat imag2 = Mat::eye(5, 5, CV_64FC1);//单位矩阵Mat imag = Mat::ones(5, 5, CV_64FC1);//都为1的矩阵Mat image = Mat::zeros(5, 5, CV_64FC1);//都为0的矩阵Mat sum = imag + image;//相加,-相减,*相乘imag.inv();//求逆imag.t();//转置cout << sum<< endl;

4.对图像X方向求导

g(x,y)=f(x,y-1)-f(x,y+1)

VideoCapture cap(0);while (true){Mat frame;cap >> frame;cvtColor(frame, frame, CV_RGB2GRAY);Mat dimg = Mat(frame.rows, frame.cols - 2, CV_8UC1);//新建Mat是对X方向求导后的读取到的for (int i = 0; i < frame.rows; i++){for (int j = 1; j < frame.cols - 2; j++)//第一列和最后一列不能求{dimg.at<uchar>(i, j - 1) = frame.at<uchar>(i, j - 1) - frame.at<uchar>(i, j + 1);//求导公式}}imshow("123", dimg);waitKey(10);}

5,图像卷积

        VideoCapture cap(0);while (true){Mat frame;cap >> frame;cvtColor(frame, frame, CV_RGB2GRAY);Mat dimg=Mat (frame.rows, frame.cols - 2, CV_8UC1);Mat model(1, 3, CV_64FC1);//frame矩阵和model矩阵的卷积,用dimg表示出来model.at<double>(0, 0) = 1;//给model矩阵赋值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 = 0; n < model.cols; n++){sum += (double)(frame.at<uchar>(i + m, j +n-1))*model.at<double>(m, n);//得到每一                                                 个卷积后的值}}dimg.at<uchar>(i, j - 1) = (uchar)sum;//将得到的值赋到dimg对应的位置}}//循环遍历imshow("123", dimg);waitKey(10);}

5.高斯模糊

经过高斯之后再用卷积

       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 cap(0);while (true){Mat frame;cap >> 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("a", frame);imshow("gauss", dimg);waitKey(10);//等待}

6.利用API操作

VideoCapture cap(0);while (true){Mat frame;cap>> frame;cvtColor(frame, frame, CV_RGB2GRAY);//直接调用方法//GaussianBlur(frame, frame, cvSize(5, 5), 10, 10 );//高斯,模糊//Canny(frame, frame, 100, 100);//提取边缘,高频Sobel(frame,frame,0,1,1);//边缘,几乎没有噪声imshow("q", frame);waitKey(10);}





0 0
原创粉丝点击