opencv影象對比度
阿新 • • 發佈:2019-01-22
// 處理圖片的對比度.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: