opencv常用库的使用

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); //自适应(窗口可拖动)

}

}

相关推荐
love530love几秒前
ComfyUI 升级 v0.4.0 踩坑记录:解决 TypeError: QM_Queue.task_done() 报错
人工智能·windows·python·comfyui
金士镧(厦门)新材料有限公司3 分钟前
稀土化合物:推动科技发展的“隐形力量”
人工智能·科技·安全·全文检索·生活·能源
牛客企业服务6 分钟前
AI简历筛选:破解海量简历处理难题
人工智能
粟悟饭&龟波功12 分钟前
【GitHub热门项目精选】(2025-12-19)
前端·人工智能·后端·github
诸葛务农13 分钟前
类脑智能技术前沿进展及中美类脑智能技术比对
人工智能
LiYingL15 分钟前
ChartCap:利用大型数据集和新的评估指标抑制图表标题幻觉
人工智能
有来有去952718 分钟前
vllm推理服务指标监控看板搭建手册
人工智能·vllm
流浪法师1224 分钟前
MyPhishing-Web:AI 驱动的钓鱼邮件检测可视化平台
前端·人工智能
LinkTime_Cloud30 分钟前
谷歌深夜突袭:免费Flash模型发令,部分测试优于 GPT-5.2
人工智能·gpt·深度学习
职业码农NO.132 分钟前
智能体推理范式: Plan-and-Execute(规划与执行)
人工智能·python·数据分析·系统架构·知识图谱·agent·集成学习