opencv:访问像素
a.使用指针
#include <opencv.hpp> using namespace cv; using namespace std; int main() { //指针访问每个像素并赋值 Mat img = imread("001.jpg"); for (int i = 0; i < img.rows; i++) { uchar* data = img.ptr<uchar>(i); // 获取每i行的首地址 for (int j = 0; j < (img.cols*img.channels())/*列数乘以通道数*/; j++) data[j] = 200; } imshow("2", img); waitKey(0); return 0; }
b.通过迭代器
#include <opencv.hpp> using namespace cv; using namespace std; int main() { //使用迭代器 Mat img = imread("001.jpg"); Mat_<Vec3b>::iterator it = img.begin<Vec3b>(); // 迭代器的初始位置 Mat_<Vec3b>::iterator itend = img.end<Vec3b>(); // 迭代器的终止位置 for (; it != itend; it++) { (*it)[0] = 0; (*it)[2] = 0; (*it)[1] = 255; } imshow("2", img); waitKey(0); return 0; }
c.通过地址计算
#include <opencv.hpp> using namespace cv; using namespace std; int main() { //动态地址计算 use .at Mat img = imread("001.jpg"); for (int i = 0; i < img.rows; i++) for (int j = 0; j < img.cols; j++) { img.at<Vec3b>(i, j)[0] = 0; img.at<Vec3b>(i, j)[1] = 0; img.at<Vec3b>(i, j)[2] = 244; } imshow("2", img); waitKey(0); return 0; }
------------ 转载请注明出处 ------------