Opencv计算机视觉编程攻略-第一节 图像读取与基本处理

1. 图像读取

导入依赖项的h文件

cpp 复制代码
#include <iostream>

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
项目 Value
core.hpp 基础数据结构和操作(图像存储、矩阵运算、文件 I/O)
highgui.hpp 图像显示、窗口管理、用户交互(图像/视频显示、用户输入处理、结果保存)
imgproc.hpp 图像处理算法(图像滤波、几何变换、边缘检测、形态学操作)

二 读取图片

cpp 复制代码
Mat image; // 图像矩阵
std::cout << "This image is " << image.rows << " x "
	<< image.cols << std::endl;
// read the input image as a gray-scale image
image = cv::imread("E:/CODE/images/puppy.bmp", cv::IMREAD_GRAYSCALE);

cv::IMREAD_GRAYSCALE 以灰度图 ,默认是彩色图

1. Mat 常见操作

cpp 复制代码
//创建Mat图像矩阵 CV_8U CV_8UC3 参数对应位数 通道数
Mat image1(240, 320, CV_8U, 100); 
//100 为默认值,三通道使用cv::Scalar(0, 0, 255)
//图像直接复制
image3.copyTo(image2);
//图像对象直接继承
Mat image4(image3);
// 图像对象格式转换
image1.convertTo(image2, CV_32F, 1 / 255.0, 0.0);
// 获取mat块某一片区域
image=  cv::imread("puppy.bmp");
// define image ROI at image bottom-right
imageROI= image(cv::Rect(image.cols-logo.cols,image.rows-logo.rows,
	                     logo.cols,logo.rows));
 // use the logo as a mask (must be gray-level)
 cv::Mat mask(logo);

// insert by copying only at locations of non-zero mask 0值区域为透明
logo.copyTo(imageROI,mask);

2. Matx 矩阵计算

cpp 复制代码
	// a 3x3 matrix of double-precision
	cv::Matx33d matrix(3.0, 2.0, 1.0,
		2.0, 1.0, 3.0,
		1.0, 2.0, 3.0);
	// a 3x1 matrix (a vector)
	cv::Matx31d vector(5.0, 1.0, 3.0);
	// multiplication
	cv::Matx31d result = matrix * vector;

三 窗口响应函数

下列函数实现opencv窗口的点击事件监听

cpp 复制代码
void onMouse(int event, int x, int y, int flags, void* param) {

	Mat* im = reinterpret_cast<Mat*>(param);
	switch (event) {	// dispatch the event
	case cv::EVENT_LBUTTONDOWN: // mouse button down event
	// display pixel value at (x,y)
	cout << "at (" << x << "," << y << ") value is: "
		<< static_cast<int>(im->at<uchar>(cv::Point(x, y))) << std::endl;
	break;
	}
}

将监听函数绑定到窗口

cpp 复制代码
	namedWindow("Original Image"); // define the window (optional)
	imshow("Original Image", image); // show the image
	// 绑定点击事件
	setMouseCallback("Original Image", onMouse, reinterpret_cast<void*>(&image));

四 图像绘制内容

cpp 复制代码
flip(image, result, 1); // 图像翻转
// 0 for vertical,                     
// negative for both
// 保存位图
imwrite("output.bmp", result); // save result

// 在image中画圆
cv::circle(image,              // destination image 
	cv::Point(155, 110), // center coordinate
	65,                 // radius  
	0,                  // color (here black)
	3);                 // thickness
// 在图像上写字
cv::putText(image,                   // destination image
	"This is a dog.",        // text
	cv::Point(40, 200),       // text position
	cv::FONT_HERSHEY_PLAIN,  // font type
	2.0,                     // font scale
	255,                     // text color (here white)
	2);                      // text thickness
相关推荐
生信碱移31 分钟前
简单方法胜过大语言模型?!单细胞扰动敲除方法的实验
大数据·人工智能·深度学习·算法·语言模型·自然语言处理·数据分析
www_pp_1 小时前
使用Python和OpenCV进行指纹识别与验证
开发语言·python·opencv
byxdaz2 小时前
PyTorch处理数据--Dataset和DataLoader
人工智能·深度学习·机器学习
船长@Quant4 小时前
PyTorch量化技术教程:第四章 PyTorch在量化交易中的应用
pytorch·python·深度学习·机器学习·量化交易·ta-lib
m0_678693335 小时前
深度学习笔记19-YOLOv5-C3模块实现(Pytorch)
笔记·深度学习·yolo
自由鬼5 小时前
Google开源机器学习框架TensorFlow探索更多ViT优化
人工智能·python·深度学习·机器学习·tensorflow·机器训练
-一杯为品-5 小时前
【动手学深度学习】#6 卷积神经网络
人工智能·深度学习·cnn
点我头像干啥6 小时前
乳腺超声图像结节分割
人工智能·深度学习·opencv·计算机视觉
Uzuki6 小时前
AI可解释性 I | 对抗样本(Adversarial Sample)论文导读(持续更新)
深度学习·机器学习·可解释性
船长@Quant6 小时前
VectorBT:使用PyTorch+LSTM训练和回测股票模型 进阶二
pytorch·python·深度学习·lstm·量化策略·sklearn·量化回测