/*
2021.3.27txwtech
读写图像
读写像素
修改像素值
imread 可以指定加载为灰度或者RGB图像
Imwrite 保存图像文件,类型由扩展名决定
读一个GRAY像素点的像素值(CV_8UC1)
Scalar intensity = img.at<uchar>(y, x);
或者 Scalar intensity = img.at<uchar>(Point(x, y));
读一个RGB像素点的像素值
Vec3f intensity = img.at<Vec3f>(y, x);
float blue = intensity.val[0];
float green = intensity.val[1];
float red = intensity.val[2];
Vec3b对应三通道的顺序是blue、green、red的uchar类型数据。
Vec3f对应三通道的float类型数据
把CV_8UC1转换到CV32F1实现如下:
src.convertTo(dst, CV_32F);
//对选中代码上下移动,alt+方向键
*/
/*
2021.3.27txwtech
读写图像
读写像素
修改像素值
imread 可以指定加载为灰度或者RGB图像
Imwrite 保存图像文件,类型由扩展名决定
读一个GRAY像素点的像素值(CV_8UC1)
Scalar intensity = img.at<uchar>(y, x);
或者 Scalar intensity = img.at<uchar>(Point(x, y));
读一个RGB像素点的像素值
Vec3f intensity = img.at<Vec3f>(y, x);
float blue = intensity.val[0];
float green = intensity.val[1];
float red = intensity.val[2];
Vec3b对应三通道的顺序是blue、green、red的uchar类型数据。
Vec3f对应三通道的float类型数据
把CV_8UC1转换到CV32F1实现如下:
src.convertTo(dst, CV_32F);
//对选中代码上下移动,alt+方向键
*/
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
Mat src;
Mat gray_src;
src = imread("E:/pictures/test1.jpg");
if (src.empty())
{
printf("cannot load image");
return -1;
}
namedWindow("showpic",CV_WINDOW_AUTOSIZE);
imshow("showpic",src);
//转为灰度图像
cvtColor(src,gray_src,CV_RGB2GRAY);//更改色彩空间
namedWindow("gray_window", CV_WINDOW_AUTOSIZE);
imshow("gray_window", gray_src);
int height = gray_src.rows;
int width = gray_src.cols;
for(int row=0;row<height;row++)
{
for (int col = 0; col < width; col++)
{
int gray_value = gray_src.at<uchar>(row,col);
//更改像素值-使之反色显示
gray_src.at<uchar>(row,col) = 255 - gray_value;
}
}
namedWindow("灰度图像反差的图像",CV_WINDOW_AUTOSIZE);
imshow("灰度图像反差的图像",gray_src);//显示更改后的图像
//操作彩色图像
Mat dst2;
dst2.create(src.size(),src.type());
height = src.rows;
width = src.cols;
int channelcount = src.channels();
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
if (channelcount == 1)
{
int gray_value = gray_src.at<uchar>(row,col);
gray_src.at<uchar>(row, col) = 255 - gray_value;
}
else if (channelcount == 3)//3通道表示彩色图像
{
/*int color_b = src.at<Vec3b>(row, col)[0];
int color_g = src.at<Vec3b>(row, col)[1];
int color_r = src.at<Vec3b>(row, col)[2];*/
int color_b = src.at<Vec3b>(row, col)[0];
int color_g = src.at<Vec3b>(row, col)[1];
int color_r = src.at<Vec3b>(row, col)[2];
//使彩色像素点反色
dst2.at<Vec3b>(row, col)[0] = 255 - color_b;
//dst2.at<Vec3b>(row, col)[0] = 0;//如果某一个通道都赋值为0,就会缺少一个色彩,
dst2.at<Vec3b>(row, col)[1] = 255 - color_g;
dst2.at<Vec3b>(row, col)[2] = 255 - color_r;
//还可以3通道转单通道灰度图像
//取最大像素点
gray_src.at<uchar>(row, col) = max(color_r,max(color_b, color_g));
取最小像素点
//gray_src.at<uchar>(row, col) = min(color_r, min(color_b, color_g));
}
}
}
namedWindow("彩色图像反色显示",CV_WINDOW_AUTOSIZE);
imshow("彩色图像反色显示", dst2);
//API函数实现彩色反色相同效果
Mat dst3;
dst3.create(src.size(),src.type());
bitwise_not(src,dst3);//通过位操作实现方法,求反差图片
namedWindow("bitwise_not实现图像反色显示", CV_WINDOW_AUTOSIZE);
imshow("bitwise_not实现图像反色显示", dst3);
namedWindow("彩色转灰度取最大像素点", CV_WINDOW_AUTOSIZE);
imshow("彩色转灰度取最大像素点", gray_src);
waitKey(0);
return 0;
}