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

}

}

相关推荐
爱吃羊的老虎4 分钟前
【大模型应用】MCP (Model Context Protocol):AI界的USB接口
人工智能
用户5191495848459 分钟前
curl中的TFTP实现:整数下溢导致堆内存越界读取漏洞
人工智能·aigc
老赵全栈实战11 分钟前
《从零搭建RAG系统第5天:安装Ollama》
人工智能
星爷AG I17 分钟前
12-4 表情识别(AGI基础理论)
人工智能·agi
Sopaco18 分钟前
AI驱动的多智能体协作模式:Actor-Critic在软件开发中的应用
人工智能
heimeiyingwang25 分钟前
AI 在企业客服场景的应用:智能问答与工单自动化
人工智能
小程故事多_8030 分钟前
OpenViking,重新定义AI Agents上下文管理的开源利器
人工智能·aigc
菜鸟小芯30 分钟前
DAY4 基于 OpenClaw + 飞书开放平台实现 AI 新闻推送机器人
人工智能·机器人·飞书
systeminof34 分钟前
七色年味映双流:AI镜头下的烟火中国年
人工智能
yuezhilangniao36 分钟前
【AI 编辑器开发规范 v2.1 版】—— 为 AI 时代的敏捷开发而生
人工智能·编辑器·敏捷流程