本节知识点
1,读写图像
a,imread可以加载灰度图或者RGB图像
b,imwrite保存图像,类型由扩展名决定
2,读写图像的像素
a,读取灰度图像像素点的值(CV_8UC1)
Scalar intensity=img.at
或者
Scalar intensity=img.at
3,读一个RGB像素点的像素值
Vec3f intensity=img.at
float blue=intensity.val[0]
float green=intensity.val[1]
float red=intensity.val[2]
4,修改像素值
a,修改灰度图像像素值
img.at
b,修改RGB三通道图像像素值
img.at
img.at
img.at
5,生成一副空白图像
img=Scalar(0)
6,ROI(感兴趣区域选择)
Rect r(10,10,200,200)
Mat src=img(r);
7,Vec3b:对应的三通道的顺序是blue,green,red的uchar类型数据
下面给出修改图像三通道像素值的各种效果,大部分是反色处理
代码实现
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** args)
{
Mat image = imread("test.jpg");
if (image.empty())
{
cout << "不能加载图像..." << std::endl;
return -1;
}
Mat dst;
cvtColor(image, dst, CV_BGR2GRAY);
int height = image.rows;
int width = image.cols;
int channels = image.channels();
printf("height=%d,width=%d channels=%d",height,width,channels);
for (int row = 0; row < height;row++)
{
for (int col = 0; col < width;col++)
{
if (channels==3)
{
//image.at(row, col)[0] = 0;//blue 通道
//image.at(row, col)[1] = 0;//green 通道
//image.at(row, col)[2] = 255 - image.at(row, col)[2];// red 通道
//image.at(row, col)[0] = 255 - image.at(row, col)[0];//blue 通道
//image.at(row, col)[1] = 255 - image.at(row, col)[1];//green 通道
//image.at(row, col)[2] = 255 - image.at(row, col)[2];// red 通道
image.at(row, col)[0] = 0;//blue 通道
image.at(row, col)[1] = 255 - image.at(row, col)[1];//green 通道
image.at(row, col)[2] = 0;// red 通道
//image.at(row, col)[1] = 0;//green 通道
//image.at(row, col)[2] = 0;// red 通道
}
//else if (channels==1)
//{
// dst.at(row, col) = 255 - dst.at(row, col);
//}
}
}
namedWindow("输出图像-2", CV_WINDOW_AUTOSIZE);
imshow("输出图像-2", image);
waitKey(0);
return 0;
}