文章目录
- 读写像素
- 修改像素值
- Vec3b与Vec3F
- 灰度图像增强
- 获取图像通道
- bitwise_not 算子对图像非操作
读写像素
读一个GRAY像素点的像素值(CV_8UC1) Scalar intensity = img.at(y, x); 或者 Scalar intensity = img.at(Point(x, y));
读一个RGB像素点的像素值 Vec3f intensity = img.at(y, x); float blue = intensity.val[0]; float green = intensity.val[1]; float red =
intensity.val[2];
修改像素值
灰度图像 img.at(y, x) = 128;
RGB三通道图像 img.at(y,x)[0]=128; // blue img.at(y,x)[1]=128; // green img.at(y,x)[2]=128; // red
空白图像赋值 img = Scalar(0);
ROI选择 Rect r(10, 10, 100, 100); Mat smallImg = img®;
Vec3b与Vec3F
- Vec3b对应三通道的顺序是blue、green、red的uchar类型数据。
- Vec3f对应三通道的float类型数据
- 把CV_8UC1转换到CV32F1实现如: src.convertTo(dst, CV_32F);
灰度图像增强
可以用增强图像加钱图像的辨析度
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{Mat src = imread("test.jpg");//读取图片if (src.empty()){cout << "could not load img...";return -1;}namedWindow("test");//设置窗口名称imshow("test", src);//灰度图像反转Mat gray_image;cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图namedWindow("invert gray");imshow("invert gray", gray_image);int width = gray_image.cols;//获取图像的宽高int high = gray_image.rows;for (int i = 0; i < high; i++){for (int j = 0; j < width; j++){int gray = gray_image.at<uchar>(i, j);//获取行列坐标点gray_image.at<uchar>(i, j) = 255 - gray;//图像反转}}namedWindow("invert gray");imshow("invert gray", gray_image);waitKey(0);return 0;
}
获取图像通道
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{Mat src = imread("test.jpg");//读取图片if (src.empty()){cout << "could not load img...";return -1;}namedWindow("test");//设置窗口名称imshow("test", src);//灰度图像反转Mat gray_image;cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图//namedWindow("invert gray");//imshow("invert gray", gray_image);int width = gray_image.cols;//获取图像的宽高int high = gray_image.rows;int nc = src.channels();//获取通道值Mat dst;dst.create(src.size(), src.type());for (int row = 0; row < high; row++){for (int col = 0; col < width; col++){if (nc == 1)//单通道{int gray = gray_image.at<uchar>(row, col);//获取行列坐标点gray_image.at<uchar>(row, col) = 255 - gray;//图像反转}else if (nc == 3){int b = src.at<Vec3b>(row, col)[0];int g = src.at<Vec3b>(row, col)[1];int r = src.at<Vec3b>(row, col)[2];dst.at<Vec3b>(row, col)[0] = b;dst.at<Vec3b>(row, col)[1] = g;dst.at<Vec3b>(row, col)[2] = r;//调整bgr参数获取想要的效果gray_image.at<uchar>(row, col) = min(r, min(b, g));//获取rgb的最小值}}}//namedWindow("invert gray");//imshow("invert gray", gray_image);imshow("三色通道", dst);waitKey(0);return 0;
}
bitwise_not 算子对图像非操作
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{Mat src = imread("test.jpg");//读取图片if (src.empty()){cout << "could not load img...";return -1;}namedWindow("test");//设置窗口名称imshow("test", src);//灰度图像反转Mat gray_image;cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图//namedWindow("invert gray");//imshow("invert gray", gray_image);int width = gray_image.cols;//获取图像的宽高int high = gray_image.rows;int nc = src.channels();//获取通道值Mat dst;dst.create(src.size(), src.type());//for (int row = 0; row < high; row++)//{// for (int col = 0; col < width; col++)// {// if (nc == 1)//单通道// {// int gray = gray_image.at<uchar>(row, col);//获取行列坐标点// gray_image.at<uchar>(row, col) = 255 - gray;//图像反转// }// else if (nc == 3)// {// // int b = src.at<Vec3b>(row, col)[0];// int g = src.at<Vec3b>(row, col)[1];// int r = src.at<Vec3b>(row, col)[2];// dst.at<Vec3b>(row, col)[0] = b;// dst.at<Vec3b>(row, col)[1] = g;// dst.at<Vec3b>(row, col)[2] = 0;//调整bgr参数获取想要的效果// gray_image.at<uchar>(row, col) = min(r, min(b, g));//获取rgb的最小值// }// // }//}bitwise_not(src, dst);//namedWindow("invert gray");//imshow("invert gray", gray_image);imshow("三色通道", dst);waitKey(0);return 0;
}
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{Mat src = imread("test.jpg");//读取图片if (src.empty()){cout << "could not load img...";return -1;}namedWindow("test");//设置窗口名称imshow("test", src);//灰度图像反转Mat gray_image;cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图//namedWindow("invert gray");//imshow("invert gray", gray_image);int width = gray_image.cols;//获取图像的宽高int high = gray_image.rows;int nc = src.channels();//获取通道值Mat dst;dst.create(src.size(), src.type());for (int row = 0; row < high; row++){for (int col = 0; col < width; col++){if (nc == 1)//单通道{int gray = gray_image.at<uchar>(row, col);//获取行列坐标点gray_image.at<uchar>(row, col) = 255 - gray;//图像反转}else if (nc == 3){int b = src.at<Vec3b>(row, col)[0];int g = src.at<Vec3b>(row, col)[1];int r = src.at<Vec3b>(row, col)[2];dst.at<Vec3b>(row, col)[0] = 255-b;dst.at<Vec3b>(row, col)[1] = 255-g;dst.at<Vec3b>(row, col)[2] = 255-r;//调整bgr参数获取想要的效果//gray_image.at<uchar>(row, col) = min(r, min(b, g));//获取rgb的最小值}}}//bitwise_not(src, dst);//namedWindow("invert gray");//imshow("invert gray", gray_image);imshow("三色通道", dst);waitKey(0);return 0;
}