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

}

}

相关推荐
fantasy_arch2 分钟前
pytorch例子计算两张图相似度
人工智能·pytorch·python
AndrewHZ2 小时前
【3D重建技术】如何基于遥感图像和DEM等数据进行城市级高精度三维重建?
图像处理·人工智能·深度学习·3d·dem·遥感图像·3d重建
飞哥数智坊2 小时前
Coze实战第18讲:Coze+计划任务,我终于实现了企微资讯简报的定时推送
人工智能·coze·trae
Code_流苏2 小时前
AI热点周报(8.10~8.16):AI界“冰火两重天“,GPT-5陷入热议,DeepSeek R2模型训练受阻?
人工智能·gpt·gpt5·deepseek r2·ai热点·本周周报
赴3352 小时前
矿物分类案列 (一)六种方法对数据的填充
人工智能·python·机器学习·分类·数据挖掘·sklearn·矿物分类
大模型真好玩2 小时前
一文深度解析OpenAI近期发布系列大模型:意欲一统大模型江湖?
人工智能·python·mcp
双翌视觉2 小时前
工业视觉检测中的常见的四种打光方式
人工智能·计算机视觉·视觉检测
念念01073 小时前
基于MATLAB多智能体强化学习的出租车资源配置优化系统设计与实现
大数据·人工智能·matlab
nonono3 小时前
深度学习——常见的神经网络
人工智能·深度学习·神经网络
AKAMAI4 小时前
AI需要防火墙,云计算需要重新构想
人工智能·云原生·云计算