// 处理图片的对比度.cpp: 定义控制台应用程序的入口点。
//
# include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
#include "iostream"
using namespace std;
using namespace cv;
int main()
{
//方式1:手动处理
Mat src, dest;
src = imread("lena.jpg");
if (src.empty()) {
cout << "error";
return -1;
}
dest = Mat::zeros(src.size(), src.type());
int cols = src.cols * src.channels();
int offsetx = src.channels();
int rows = src.rows;
cout << cols<< "---" << rows << "---" << offsetx << endl;
for (int row = 1; row < rows - 1; row++) {
const uchar* current = src.ptr<uchar>(row-1);//当前的指针所代表的像素
const uchar* previous = src.ptr<uchar>(row);//前一帧所代表的像素
const uchar* next = src.ptr<uchar>(row+1);//下一帧所代表的像素
uchar* output = dest.ptr<uchar>(row);//目标对象像素
for (int col = offsetx; col < cols; col++ ) {
//带入目标公式current[col - offsetx]代表前一个current[col + offsetx]后面一个previous[col]正对着next[col]正对着(正对着代表列一样),同理前面的列不一样
// saturate_cast<uchar> // 函数代表把像素范围控制在0-255之内
output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
}
}
namedWindow("src");
imshow("src", src);
namedWindow("dest");
imshow("dest",dest);
//方式2
double start = getTickCount();
Mat dest2;
dest2 = Mat::zeros(src.size(), src.type());
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dest2, src.depth(), kernel);
double sum = (getTickCount() - start) / getTickFrequency();
printf("%.2f",sum);
namedWindow("dest2");
imshow("dest2", dest2);
waitKey();
return 0;
}
原图:
方式1:
方式2: