cv::Mat m(3, 3, CV_32F); // create a 3x3 floating-point matrix float* ptr = m.ptr(0); // get a pointer to the first row of the matrix for (int i = 0; i < 9; i++) { ptr[i] = (float)i; // fill the matrix with sequential numbers } cv::Mat resized_m(2, 2, CV_32F); memcpy(resized_m.ptr (0), ptr, 2 * 2 * sizeof(float)); // resize the matrix by copying the first 2x2 elements from the original matrix
cv::Mat img = cv::imread("image.png"); // read a PNG image unsigned char* ptr = img.ptrThis example reads a PNG image, gets a pointer to its first row using Mat memptr, and then iterates over all its pixels to invert their colors. The inverted image is then displayed using OpenCV's imshow function.(0); // get a pointer to the first row of the image for (int i = 0; i < img.rows * img.cols * img.channels(); i++) { ptr[i] = 255 - ptr[i]; // invert the colors of the image } cv::imshow("Inverted Image", img); // display the inverted image