在开始之前先下载一个可以查看Mat图像数据的插件
1、打开vs,找到菜单栏->工具->扩展与更新->联机->搜索Image
2、下载完成之后关闭VS,提示安装信息后安装即可。
3、打开ImageWatch。程序运行过程中可在调试模式下查看图像信息。
#include <iostream>
#include <thread>
#include "Matx.h"
#include <opencv2\core.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
using namespace cv;
using namespace std;
void colorInverse(cv::Mat src,cv::Mat &dst)
{
//dst = cv::Mat(src.rows, src.cols, src.type);
if (src.channels() == 1)
{
//像素的遍历2---通过迭代器访问像素
cv::Mat_<uchar>::iterator it = dst.begin<uchar>();
cv::Mat_<uchar>::iterator it_end = dst.end<uchar>();
while (it != it_end)
{
(*it) = 255 - (*it);
it++;
}
}
else if (src.channels() == 3)
{
cv::Mat_<Vec3b>::iterator it = dst.begin<Vec3b>();
cv::Mat_<Vec3b>::iterator it_end = dst.end<Vec3b>();
while (it != it_end)
{
(*it)[0] = 255 - (*it)[0];
(*it)[1] = 255 - (*it)[1];
(*it)[2] = 255 - (*it)[2];
it++;
}
}
}
int main()
{
int div = 5;
cv::Mat src = cv::imread("C:\\Users\\Administrator\\Desktop\\board\\board-01.png");
cvtColor(src, src, COLOR_BGR2GRAY);
//像素的访问1
uchar *data = src.ptr<uchar>(5);
uchar d = data[6];
//像素的访问2
d = src.at<uchar>(6,6);
cout << d << endl;
//像素的遍历1---通过指针遍历像素
for (int i = 0; i < src.rows; i++)
{
uchar *data = src.ptr<uchar>(i);
for (int j = 0; j < src.cols*src.channels(); j++)
{
data[j] = 255 - data[j];
}
}
Mat dst = src.clone();
colorInverse(src, dst);
system("pause");
return 0;
}