OpenCV学习笔记(6):滤波filter2D()

来源:互联网 发布:天津淘宝托管 编辑:程序博客网 时间:2024/05/18 02:21
#include "stdafx.h"#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <iostream>using namespace std;using namespace cv;void Sharpen(const Mat&src, Mat & result){CV_Assert(src.depth() == CV_8U);const int channels = src.channels();result.create(src.size(), src.type());for (int i = 1; i < src.rows - 1; i++){const uchar*previous = src.ptr<uchar>(i - 1);const uchar*current = src.ptr<uchar>(i);const uchar*next = src.ptr<uchar>(i + 1); uchar *out = result.ptr<uchar>(i);for (int j = channels; j < channels*(src.cols - 1); j++){*out++ = saturate_cast<uchar>(5 * current[j] - current[j - channels] - current[j + channels] -previous[j] - next[j]);}}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));}int main(int argc, char* argv[]){Mat I, J,K;const char * filename = argc >= 2 ? argv[1] : "lena.png";if (argc >= 3 && !strcmp(argv[2], "G"))I=imread(filename, CV_LOAD_IMAGE_GRAYSCALE);elseI=imread(filename, CV_LOAD_IMAGE_COLOR);if (!I.data)cout << "cannot load image!" << endl;const char * window1 = "src";const char * window2 = "result";namedWindow(window1, WINDOW_AUTOSIZE);namedWindow(window2, WINDOW_AUTOSIZE);imshow(window1, I);double time = getTickCount();Sharpen(I, J);time = ((double)getTickCount() - time) / getTickFrequency();cout << "time is:" << time << endl;imshow(window2, J);waitKey(0);Mat kernl = (Mat_<char>(3, 3) << 0, -1, 0,-1, 5, -1,0, -1, 0);time = (double)getTickCount();filter2D(I, K, I.depth(), kernl);time = ((double)getTickCount() - time) / getTickFrequency();cout << "time is:" << time << endl;imshow(window2, K);waitKey(0);return 0;}

0 0
原创粉丝点击