当前位置: 首页 > article >正文

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

1. 图像读取

导入依赖项的h文件

#include <iostream>

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

二 读取图片

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 常见操作

//创建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 矩阵计算

	// 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窗口的点击事件监听

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;
	}
}

将监听函数绑定到窗口

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

四 图像绘制内容

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

http://www.kler.cn/a/597662.html

相关文章:

  • 百度SEO和必应SEO优化方法
  • 【css酷炫效果】纯CSS实现科技感网格背景
  • JVM运行时数据区内部结构难记?一个例子优化记忆
  • 摄影工作室预约管理系统基于Spring BootSSM
  • 校园自习室预约小程序(源码+部署教程)
  • 基于Spring Boot的健身房管理系统的设计与实现(LW+源码+讲解)
  • python网络爬虫开发实战之网页数据的解析提取
  • 如何在Spring Boot项目中集成LangChain4J开源开发框架
  • 论文阅读:2024-NAACL Semstamp、2024-ACL (Findings) k-SemStamp
  • 【第21节】windows sdk编程:网络编程基础
  • 【Spring】Spring Task详解
  • java设计模式之建造者模式《装修启示录》​
  • MAC-在使用@Async注解的方法时,分布式锁管理和释放
  • 嵌入式开发之STM32学习笔记day08
  • Mac:Ant 下载+安装+环境配置(详细讲解)
  • Web3如何影响未来的社交平台:去中心化社交的兴起
  • 区块链在医疗数据共享中的应用:解锁安全与透明的新维度
  • 广度优先搜索(BFS) vs 深度优先搜索(DFS):算法对比与 C++ 实现
  • 洛谷 P10108 [GESP202312 六级] 闯关游戏 题解
  • Android Studio控制台中文乱码解决方案