簡單濾波器+波紋函數

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

using namespace std;
using namespace cv;

//該函數目標是讓目標圖像銳化
void Sharpen(const Mat &image, Mat& result)
{
    //主要是訪問目標像素周圍的函數
    //原理是拉普拉斯算子
    //也可以創建一個內核,然後使用filter2D函數
    //濾波函數對目標函數進行處理,和本函數一樣
    result.create(image.size(), image.type());
    int nchannels = image.channels();
    for (int j = 1; j < image.rows - 1; j++)
    {
        const uchar* previous = image.ptr<const uchar>(j - 1);
        const uchar* next = image.ptr<const uchar>(j + 1);
        const uchar* current = image.ptr<const uchar>(j);
        uchar* output = result.ptr<uchar>(j);
        for (int i = nchannels; i < (image.cols - 1)*nchannels; i++)
        {
            *output++ = saturate_cast<uchar>(5*current[i]-current[i-nchannels]
                -current[i+nchannels]-previous[i]-next[i]);
        }
    }
    result.row(0).setTo(Scalar(0));
    result.row(result.rows - 1).setTo(Scalar(0));
    result.col(0).setTo(Scalar(0));
    result.col(result.cols - 1).setTo(Scalar(0));
}

//該函數目的是讓目標圖像波紋狀
void wave(const Mat& image, Mat& result)
{
    //主要原理是圖像的重定位
    Mat srcX(image.rows, image.cols, CV_32F);
    Mat srcY(image.rows, image.cols, CV_32F);

    for (int i = 0; i < image.rows; i++)
    {
        for (int j = 0; j < image.cols; j++)
        {
            srcX.at<float>(i, j) = j;
            srcY.at<float>(i, j) = i + 5*sin(j/10.0);
        }
    }
    //重要的函數在這裏!
    remap(image, result, srcX, srcY, INTER_LINEAR);
}


int main(int argc, char* argv[])
{
    const char* imagename = "emosue.jpg";
    const char* imagename2 = "otherPic.jpg";

    //從文件中讀入圖像
    Mat img = imread(imagename);

    //銳化測試相關參數操作
    Mat result;
    Sharpen(img, result);

    //銳化測試
    //顯示圖像,很明顯,這兩張圖片一張是經過銳化的。
    //imshow("image", result);
    //imshow("image", img);

    //圖像加和測試
    Mat result2;
    Mat addPic = imread(imagename2);
    addWeighted(img, 0.7, addPic, 0.9, 0.0, result2);
    //imshow("additionTest", result2);

    //圖像重映射測試
    Mat result3;
    wave(addPic, result3);
    imshow("Wave Test", result3);


    //此函數等待按鍵,按鍵盤任意鍵就返回
    waitKey(0);

    return 0;
}

銳化測試效果原圖

銳化測試效果圖

波紋測試原圖

波紋測試效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章