opencv C++使用卷積convolution/correlation進行影像處理
影片中有實做找邊緣(Laplacian Operator)、銳利化。
cross-correlation與卷積定義不同,但由於所用之Kermel Matrix均x,y軸對稱,所以是相同的。
#include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; void kernel(Mat src, Mat& im, Mat k, string desc, bool neg=0) { filter2D(src, im, src.depth(), k); if (neg) im = ~im; imshow(desc, im); } int main() { try { cout << "做影像處理使用 covolution/cross-corelation\n"; Mat src = imread("2dogs.jpg"); resize(src, src, src.size()); imshow("image", src); const double inv_sqrt2 = 0.70710678118654752440084436210485; Mat im, im0, im1; Mat kk0 = (Mat_<double>(3, 3) << 0, 1, 0, 1, -4, 1, 0, 1, 0); Mat kk1 = (Mat_<double>(3, 3) << 1, 1, 1, 1, -8, 1, 1, 1, 1); Mat kk2 = (Mat_<double>(3, 3) << inv_sqrt2, 1, inv_sqrt2, 1,\ -4*(1+ inv_sqrt2), 1, inv_sqrt2, 1, inv_sqrt2); Mat id= (Mat_<double>(3, 3) << 0, 0, 0, 0, 1, 0, 0, 0, 0); kernel(src, im, kk0, "找邊緣", 1); kernel(src, im, id-kk0, "銳利化"); kernel(src, im, kk1, "找邊緣2", 1); kernel(im, im0, kk1, "找邊緣2**2", 1); imwrite("找邊緣2_2.png", im0); kernel(src, im, id - kk1, "銳利化2"); kernel(im, im1, id - kk1, "銳利化2**2"); imwrite("銳利化2_2.png", im1); kernel(src, im, kk2, "找邊緣3", 1); kernel(im, im, kk2, "找邊緣3**2", 1); kernel(src, im,id - kk2, "銳利化3"); kernel(im, im, id - kk2, "銳利化3**2"); waitKey(0); return 0; } catch (exception& e) { cerr << e.what(); return 1; } }
沒有留言:
張貼留言