----访问图像中的像素
#include
#include
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("C:\\Users\\441\\Desktop\\ZL\\夏目\\1.jpg");
cout << "Is continuous? ";
if (src.isContinuous())
cout << "YES." << endl;
else
cout << "NO." << endl;
Mat src1(src, Rect(250, 125, 250, 150));
cout << "Is continuous? ";
if (src1.isContinuous())
cout << "YES." << endl;
else
cout << "NO." << endl;
return 0;
}
#include
#include
using namespace cv;
using namespace std;
void colorReduce(Mat& inputImage, Mat& outputImage, int div)
int main()
{
Mat src = imread("C:\\Users\\441\\Desktop\\ZL\\夏目\\1.jpg");
imshow("原始图像",src);
Mat dst;
dst.create(src.rows, src.cols, src.type());
double time = static_cast<double>(getTickCount());
colorReduce(src, dst, 128); //缩减128倍
time = ((double)getTickCount() - time) / getTickFrequency();
cout << "运行时间为:" << time << "秒." << endl;
imshow("效果图", dst);
waitKey(0);
return 0;
}
-->采用动态地址计算方法
void colorReduce(Mat& inputImage, Mat& outputImage, int div)
{
outputImage = inputImage.clone();
int rowNumber = outputImage.rows;
int colNumber = outputImage.cols;
for (int i = 0;i < rowNumber;i++)
{
for (int j = 0; j < colNumber;j++)
{
outputImage.at<Vec3b>(i, j)[0] = outputImage.at<Vec3b>(i, j)[0] / div * div + div / 2; //蓝色通道
outputImage.at<Vec3b>(i, j)[1] = outputImage.at<Vec3b>(i, j)[1] / div * div + div / 2; //绿色通道
outputImage.at<Vec3b>(i, j)[2] = outputImage.at<Vec3b>(i, j)[2] / div * div + div / 2; //红色通道
}
}
}
void colorReduce(Mat& inputImage, Mat& outputImage, int div)
{
outputImage = inputImage.clone(); //复制实参到临时变量
Mat_<Vec3b>::iterator it = outputImage.begin<Vec3b>(); //初始位置的迭代器
Mat_<Vec3b>::iterator itend = outputImage.end<Vec3b>(); //终止位置的迭代器
//存取彩色图像像素
for (;it != itend;++it)
{
(*it)[0] = (*it)[0] / div * div + div / 2;
(*it)[1] = (*it)[1] / div * div + div / 2;
(*it)[2] = (*it)[1] / div * div + div / 2;
}
}
void colorReduce(Mat& inputImage, Mat& outputImage, int div)
{
outputImage = inputImage.clone(); //复制实参到临时变量
int rowNumber = outputImage.rows; //行数
int colNumber = outputImage.cols * outputImage.channels(); //列数x通道数=每一行元素的个数
//双重循环,遍历所有的像素值
for (int i = 0; i < rowNumber;i++) //行循环
{
uchar* data = outputImage.ptr<uchar>(i); //获取第i行的首地址
for (int j = 0;j < colNumber;j++) //列循环
data[j] = (data[j] / div) * div + div / 2; //开始处理每个像素
}
}
void LUT(InputArray src, InputArray lut, OutputArray dst,int interpolation);
第一个参数:表示输入的图像
第二个参数:表示查找表(查找表也可以是单通道,也可以是3通道,如果输入图像为单通道,那查找表必须为单通道;若输入图像为3通道,查找表可以为单通道,也可以为3通道,若为单通道则表示对图像3个通道都应用这个表,若为3通道则分别应用 )
第三个参数:表示插值,默认为0
单通道
#include
using namespace cv;
int main()
{
uchar lutData[256];
for (int i = 0; i < 256; i++)
{
if (i <= 100)
lutData[i] = 0;
if (i > 100 && i <= 200)
lutData[i] = 100;
if (i > 200)
lutData[i] = 200;
}
Mat lut(1, 256, CV_8UC1, lutData);
Mat src = imread("C:\\Users\\441\\Desktop\\ZL\\夏目\\1.jpg");
Mat dst;
imshow("原始图像", src);
LUT(src, lut, dst);
imwrite("dst.jpg", dst);
imshow("效果图", dst);
waitKey(0);
}
三通道
#include
using namespace cv;
int main()
{
uchar lutData[256 * 3];
for (int i = 0; i < 256; i++)
{
if (i <= 100)
{
lutData[i * 3] = 0;
lutData[i * 3 + 1] = 100;
lutData[i * 3 + 2] = 200;
}
if (i > 100 && i <= 200)
{
lutData[i * 3] = 100;
lutData[i * 3 + 1] = 200;
lutData[i * 3 + 2] = 0;
}
if (i > 200)
{
lutData[i * 3] = 200;
lutData[i * 3 + 1] = 0;
lutData[i * 3 + 2] = 100;
}
}
Mat lut(1, 256, CV_8UC3, lutData);
Mat src = imread("C:\\Users\\441\\Desktop\\ZL\\夏目\\1.jpg");
Mat dst;
imshow("原始图像", src);
LUT(src, lut, dst);
imwrite("dst.jpg", dst);
imshow("效果图", dst);
waitKey(0);
}
定义
getTickCount( )函数返回CPU自某个事件以来走过的时钟周期数
getTickFrequency( )函数返回CPU一秒钟所走的时钟周期数。
主要代码
double time0 = static_cast<double>(getTickCount()); //记录起始时间
... //图像操作
time0 = ((double)getTickCount() - time0) / getTickFrequency();
cout << "运行时间为:" << time0 << "秒." << endl;
---->亮度和对比度中访问图像像素的方法
(见第二点)