掩膜操作增强对比度


矩阵的掩膜操作 用于图片增强对比度。

可通过构造掩膜 使用filter2D()

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>


using namespace std;
using namespace cv;


int main() {
    Mat src = imread("/Users/apple/Desktop/test3.png", IMREAD_COLOR);
    if (src.empty()) { // if (!src.data())
        cout << "could not load image..." << endl;
        return -1;
    }

    namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE);
    imshow("test opencv setup", src);
    
    
//    int cols = src.cols * src.channels();
//    int rows = src.rows;
//    int offsetx = src.channels();
//
//
//    for (int row = 1; row < rows - 1; row++) {
//        const uchar* current = src.ptr<uchar>(row);
//        const uchar* next = src.ptr<uchar>(row + 1);
//        const uchar* previous = src.ptr<uchar>(row - 1);
//        uchar* output = dst.ptr(row);
//        for (int col = offsetx; col < cols; col++) {
//            output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
//        }
//    }
    // 注意 saturate_cast<>() 的用法: 控制值在0~255之前
    
    
    Mat dst;
    double t = getTickCount();
    Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    filter2D(src, dst, src.depth(), kernel);
    double timeconsum = (getTickCount() - t) / getTickFrequency();
    cout << "spent: " << timeconsum << endl;
    
    namedWindow("contrast image demo", CV_WINDOW_AUTOSIZE);
    imshow("contrast image demo", dst);
    
    
    waitKey(0);
    
    return 0;
}

注意saturate_cast<>()的使用,控制值在0~255之间。


还有一个知识点:

通过调用getTickCount() 和 getTickFrequency()来计算所用时间。

猜你喜欢

转载自blog.csdn.net/ringggr_h/article/details/79915806