/* 像素访问 */ #include <iostream> #include <cv.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; int main(int argc, int* argv[]) { Mat image = imread("lena.bmp"); Mat out1 = image.clone(); long n_rows = out1.rows; long n_cols = out1.cols * out1.channels(); /* * 行指针 */ uchar *p; for(long i=0; i<n_rows; i++) { p = out1.ptr<uchar>(i); for(long j=0; j<n_cols; j++) { p[j] = 255 - p[j]; } } /* * 元素指针 */ Mat out2; out2.create(image.size(), image.type()); // reserve size and type but alloc new space n_rows = out2.rows; n_cols = out2.cols * out2.channels(); p = out2.data; double c = 0.5; double b = 30; for (long i=0; i<n_rows*n_cols; i++) { *(p+i) = (uchar)(255 * 0.5 * ((double)(image.data[i]) / 255) + b); } /* * Display */ imshow("src_image", image); imshow("reverse_image", out1); imshow("mici_image", out2); waitKey(6000); return 0; }