void QuickDemo::pixel_visit_demo(Mat & image)
{int w = image.cols;//宽度int h = image.rows;//高度int dims = image.channels();//通道数 图像为灰度dims等于一 图像为彩色时dims等于三 for (int row = 0; row < h; row++) {for (int col = 0; col < w; col++) {if (dims == 1) //如果是灰度图像{int pv = image.at<uchar>(row, col);//获取当前像素 都是uchar类型 转型/*对其进行改变*/image.at<uchar>(row, col)=255-pv;//pv原来就是0->255 减去也没有改变范围}if (dims = 3)//如果是彩色图像 {//读取Vec3b bgr=image.at<Vec3b>(row, col);//获取回来是三个值 Vec3b系统设定的/*对其进行改变*/ //对图像颜色取反image.at<Vec3b>(row, col)[0] = 255 - bgr[0];//第0个通道image.at<Vec3b>(row, col)[1] = 255 - bgr[1];image.at<Vec3b>(row, col)[2] = 255 - bgr[2];}}}imshow("像素读写演示", image);
}
把图片改为灰度
Mat src = imread("E:/download/images/test.png", IMREAD_GRAYSCALE);
把后面那删了
成功