#include <iostream>
#include"opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
int row, col;
float red = 0.299;
float blue = 0.587;
float green = 0.114;
int i, j;
VideoCapture capture(0);
while (1)
{
Mat image;
int row, col;
capture >> image;
row = image.rows;
col = image.cols;
Mat gray(row, col, CV_8UC1);
uchar* data;
for (i = 0; i < row; i++)
{
/*
下面这一行代码也是没太搞懂为什么这么做,但是这是在提取图像中某一行的指针
但是我在matlab中对图像的操作方法在这整不大行,数据类型冲突了
*/
data = gray.ptr<uchar>(i);
for (j = 0; j < col; ++j)
{
data[j] = (int)(image.at<Vec3b>(i,j)[0]*blue + image.at<Vec3b>(i, j)[1] * green + image.at<Vec3b>(i, j)[2] * red);
}
}
imshow("", gray);
if (waitKey(30) == 27)
{
cout << "按下ESC键" << endl;
break;
}
}
return 0;
}
获取摄像头的视频,并且灰度化
最新推荐文章于 2020-12-22 13:50:46 发布
本篇博客介绍了一个使用C++与OpenCV实现的简单应用程序,该程序能够从摄像头捕获彩色图像并将其实时转换为灰度图像。通过调整红、绿、蓝通道的权重来计算灰度值,并展示了如何利用OpenCV库处理视频流。
7471

被折叠的 条评论
为什么被折叠?



