实现效果
具体代码
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image;
image = cv::imread("1.jpg");
if (image.empty()) { // 图像为空?
std::cout << "读取图像失败!"<< std::endl;
exit(EXIT_FAILURE); // 退出程序
}
//定义窗口并显示图像:
cv::namedWindow("原始图像"); // 创建窗口(可省)
cv::imshow("原始图像", image); // 显示图像
cv::Mat result; // 创建另一个空的图像
// 水平翻转图像
cv::flip(image, // 输入图像
result, // 输出图像
1); // 正数表示水平翻转,0表示垂直翻转,负数表示水平和垂直同时翻转
cv::imshow("输出图像", result);
cv::waitKey(0);
return 0;
}
图像编程入门第一节
画圆和文本输入![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f6a29ed4f1fc7c182abad4fea07fab16.png)
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
void onMouse(int event, int x, int y, int flags, void* param)
{
cv::Mat *im = reinterpret_cast<cv::Mat*>(param);
switch (event) { // 什么鼠标事件?
case cv::EVENT_LBUTTONDOWN: // 鼠标左键按下事件
// 输出(x, y)处像素值
std::cout << "坐标 (" << x << "," << y << ") 处像素值为: "<< static_cast<int>(im->at <uchar>(cv::Point(x, y))) << std::endl;
break;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image;
image = cv::imread("1.jpg");
cv:: circle(
image, // 目标图像
cv::Point(155, 110), //中心点坐标
65, //半径
0, //颜色(这里用黑色,针对灰度图像)
3 // 线的粗细
);
cv::putText(
image, // 目标图像
"2018031601027", // 文本(中文兼容不好)
cv::Point(40, 200), // 文本位置
cv::FONT_HERSHEY_PLAIN, // 字体类型(字体很有限)
2.0, // 字体大小
255,// 字体颜色(这里用白色,对于彩色图像则是蓝色)
2 // 文本粗细
);
if (image.empty()) { // 图像为空?
std::cout << "读取图像失败!"<< std::endl;
exit(EXIT_FAILURE); // 退出程序
}
//定义窗口并显示图像:
cv::namedWindow("原始图像"); // 创建窗口(可省)
cv::imshow("原始图像", image); // 显示图像
cv::Mat result; // 创建另一个空的图像
// 水平翻转图像
cv::flip(image, // 输入图像
result, // 输出图像
1); // 正数表示水平翻转,0表示垂直翻转,负数表示水平和垂直同时翻转
cv::imshow("输出图像", result);
cv::imwrite("output.jpg", result);
cv::setMouseCallback(
"原始图像", // 捕获鼠标事件的窗口
onMouse, // 鼠标回调函数名
reinterpret_cast<void*>(&image) // 传图像对象指针
);cv::waitKey(0);
return 0;
}