天天看點

OpenCV圖像增強之圖像反轉 C++

圖像增強:增強處理是對圖像進行加工,使其經過對于特定的應用比原始圖形更适合的一種處理。

特定一詞是指面向特定問題的。例如,對于增強X射線圖像非常有用的方法,可能并不是适用于增強紅外線。

圖像增強是視覺上最具吸引力的圖像處理領域之一;

圖像增強常用三類基本函數:

線性函數(反轉和恒等變換)、對數函數(對數和反對數變換)、幂律函數(n次幂和n次根變換)

圖像反轉用于增強嵌入在一幅圖像的暗區域中的白色或灰色細節。特别是當黑色面積在尺寸上占主導地位時。

對數變換

下圖是基本灰階變換函數:

OpenCV圖像增強之圖像反轉 C++

圖像反轉表示為:   s = L -1 -r

代碼實作:

timer.h

#ifndef TIMER_H

#define TIMER_H

#include <iostream>

#include <chrono>

class Timer {

public:

    Timer() : t1(res::zero()) , t2(res::zero()) {

        tic();

    }

    ~Timer() {}

    void tic() { t1 = clock::now(); }

    void toc(const char* str) {

        t2 = clock::now();

        std::cout << str << " time: " << std::chrono::duration_cast<res>(t2 - t1).count() / 1e3 <<

                     "ms." << std::endl;

    }

private:

    typedef std::chrono::high_resolution_clock clock;

    typedef std::chrono::microseconds res;

    clock::time_point t1;

    clock::time_point t2;

};

#endif // TIMER_H

// main.cpp

#include <iostream>

#include "opencv2/highgui/highgui.hpp"

#include "opencv2/imgproc/imgproc.hpp"

#include "timer.h"

int main()

{

    cv::Mat image = cv::imread("xral.tif");

    if (image.empty()) {

        std::cout << "Couldn't open file." << std::endl;

        return -1;

    }

    cv::imshow("image", image);

    Timer timer;

    timer.tic();

    cv::Mat temp = cv::Scalar(255, 255, 255) - image;

    timer.toc("single ");

    cv::imshow("qufan1", temp);

    timer.tic();

    cv::Mat temp2 = ~image;

    timer.toc("operator~");

    cv::imshow("qufan2", temp2);

    cv::waitKey();

    return 0;

}

處理結果顯示:

OpenCV圖像增強之圖像反轉 C++

繼續閱讀