#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
//使用Mat的ptr 操作像素
int main()
{
//加载图片,3通道
Mat img = imread("1.jpg");
//获取第20行的第20个像素点,3通道的图像像素点类型为Vec3b,单通道的像素类型为uchar
//ptr是一个函数模板,返回一个指针
//typedef Vec<uchar, 3> Vec3b;
Vec3b pix = img.ptr<Vec3b>(20)[20];
//Vec3b *p = img.ptr<Vec3b>(20);
//Vec3b pix = p[20];
cout << "pix = " << pix << endl; //整体输出
printf("%d %d %d\n",pix[0],pix[1],pix[2]);//单个分量输出
//修改像素点的值:把从原点开始的100行100列置为blue
for (int i = 0; i < 100; ++i)
{
for (int j = 0; j < 100; ++j)
{
img.ptr<Vec3b>(i)[j] = Vec3b(255,0,0);
}
}
imshow("1.jpg",img);
imwrite("111.jpg",img);
waitKey(0);
destroyAllWindows();
return 0;
}
- 头文件及库
INCLUDEPATH += D:\Tools\opencv\opencv3.2_win_lib\install\include
LIBS += -LD:\Tools\opencv\opencv3.2_win_lib\install\x86\mingw\bin -lopencv_core320
-lopencv_highgui320 -lopencv_imgproc320 -lopencv_imgcodecs320