目录
1、怎么创建一张图片
cv::Mat image = imread("C:\\Users\\jin'yi'zhi\\Desktop\\images\\bk3.jpg",IMREAD_GRAYSCALE);
namedWindow("Display window", WINDOW_AUTOSIZE);//窗口的大小匹配图片的大小
imshow("Display window", image); //窗口的名称 显示的图片
waitKey(0);//窗口停顿
//destroyAllWindows();
运行结果:
显示一张具有灰度的图片,窗口的大小与图片的大小相匹配
图片显示10秒,然后销毁
cv::Mat image = imread("C:\\Users\\jin'yi'zhi\\Desktop\\images\\bk1.jpg");
namedWindow("Display window", WINDOW_AUTOSIZE);//窗口的大小匹配图片的大小
imshow("Display window", image); //窗口的名称 显示的图片
waitKey(10000);//窗口停顿10秒
destroyAllWindows();
运行结果:
2、获取图像基本信息
效果:获取图像的基本的信息,包括图像的尺寸,通道数
Mat image1 = imread("C:\\Users\\jin'yi'zhi\\Desktop\\images\\bk1.jpg"); //读取图像;
if (image1.empty())
{
cout << "读取错误" << endl;
return -1;
}
imshow("image1", image1); //显示图像;
cout << "图像的行数为: " << image1.rows << endl; //获取图像的高度,行数;
cout << "图像的列数为: " << image1.cols << endl; //获取图像的宽度,列数;
cout << "图像的通道数为: " << image1.channels() << endl; //获取图像的通道数,彩色图=3,灰度图=1;
cout << "图像的尺寸为: " << image1.size << endl; //获取图像的尺寸,行*列;
waitKey(0); //暂停,保持图像显示,等待按键结束
运行效果:
3、鼠标效果
点击图像上面的点,显示鼠标点的图像坐标值以及色彩值也就是RGB值
onMouse.h
#pragma once
#pragma once
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
void onMouse(int event, int x, int y, int flags, void* param); //evnet:鼠标事件类型 x,y:鼠标坐标 flags:鼠标哪个键
void onMouse(int event, int x, int y, int flags, void* param) //evnet:鼠标事件类型 x,y:鼠标坐标 flags:鼠标哪个键
{
Mat* im = reinterpret_cast<Mat*>(param);
switch (event) {
case EVENT_LBUTTONDOWN:
//显示图像像素值
if (static_cast<int>(im->channels()) == 1)
{
//若图像为单通道图像,则显示鼠标点击的坐标以及灰度值
switch (im->type())
{
case 0:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<uchar>(Point(x, y))) << endl; break;
case 1:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<char>(Point(x, y))) << endl; break;
case 2:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<ushort>(Point(x, y))) << endl; break;
case 3:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<short>(Point(x, y))) << endl; break;
case 4:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<int>(Point(x, y))) << endl; break;
case 5:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<float>(Point(x, y))) << endl; break;
case 6:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<double>(Point(x, y))) << endl; break;
}
}
else
{
//若图像为彩色图像,则显示鼠标点击坐标以及对应的B, G, R值
cout << "at (" << x << ", " << y << ")"
<< " B value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[0])
<< " G value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[1])
<< " R value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[2])
<< endl;
}
break;
}
}
main.cpp
Mat image1 = imread("C:\\Users\\jin'yi'zhi\\Desktop\\images\\bk1.jpg"); //读取图像;
if (image1.empty())
{
cout << "读取错误" << endl;
return -1;
}
imshow("image1", image1); //显示图像;
setMouseCallback("image1", onMouse, reinterpret_cast<void*>(&image1)); //关联图像显示窗口和onMouse函数
waitKey(0); //暂停,保持图像显示,等待按键结束
运行效果:
4、在图像中加入白色噪点
Salt.h
#pragma once
#include<iostream>
#include<opencv2/opencv.hpp>
#include <random> //随机数头文件
using namespace cv;
using namespace std;
void Salt(Mat image, int n); //n:加入噪声点数
Salt.cpp
#include "Salt.h"
void Salt(Mat image, int n)
{
//随机数生成器
default_random_engine generater;
uniform_int_distribution<int>randomRow(0, image.rows - 1);
uniform_int_distribution<int>randomCol(0, image.cols - 1);
int i, j;
for (int k = 0; k < n; k++)
{
i = randomCol(generater);
j = randomRow(generater);
if (image.channels() == 1)
{
image.at<uchar>(j, i) = 255;
}
else if (image.channels() == 3)
{
image.at<Vec3b>(j, i)[0] = 255;
image.at<Vec3b>(j, i)[1] = 255;
image.at<Vec3b>(j, i)[2] = 255;
}
}
}
main.cpp
Mat image1 = imread("C:\\Users\\jin'yi'zhi\\Desktop\\images\\bk1.jpg"); //读取图像;
if (image1.empty())
{
cout << "读取错误" << endl;
return -1;
}
imshow("image1", image1); //显示原图像;
Salt(image1, 5000); //加入5000个噪声点
imshow("image2", image1); //显示噪声图像;
waitKey(0); //暂停,保持图像显示,等待按键结束
return 0;
效果:
侵权删。
参考资料: