<OpenCV1> 简单滤波器+波纹函数

来源:互联网 发布:程序员刷题app 编辑:程序博客网 时间:2024/06/05 11:12
#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;}

锐化测试效果原图

锐化测试效果图

波纹测试原图

波纹测试效果图

0 0
原创粉丝点击