卷积是一个数学概念, 在图像处理中是一个操作, 是kernel(filter,卷积核,算子)在图像的每个像素的操作
卷积操作两个核心,一个是kernel的设计, 一个是算子遍历像素的方式
一般的算子有一些经典的, 如:
int main(int argc, char ** argv)
{
Mat src, gray, bin, dst;
src = imread("C:\\Users\\xujin\\Desktop\\test1.JPG");
if (!src.data)
{
cout << "no image";
return -1;
}
namedWindow("src_image", WINDOW_AUTOSIZE);
imshow("src_image", src);
Mat dst_x, dst_y;
Mat dstSobel_x, dstSobel_y, dstLap;
src.copyTo(dst_x);
src.copyTo(dst_y);
src.copyTo(dstSobel_x);
src.copyTo(dstSobel_y);
src.copyTo(dstLap);
Mat kernel_x = (Mat_(2, 2) << 1, 0, 0, -1);
Mat kernel_y = (Mat_(2, 2) << 0,-1,-1,0);
Mat kernelSobel_x = (Mat_(3, 3) << -1, 0, -1, -2, 0, -2, -1, 0, -1);
Mat kernelSobel_y = (Mat_(3, 3) << -1,-2,-1, 0, 0, 0, 1, 2, 1);
Mat kernelLap = (Mat_(2, 2) << 0, -1, 0, -1, -4, -1, 0, -1, 0);
filter2D(src, dst_x,-1, kernel_x, Point(-1, -1));
filter2D(src, dst_y, -1, kernel_x, Point(-1, -1)); //filter2D()是进行2D卷积操作的函数, 卷积核是一个小型矩阵,卷积核是设计核心
filter2D(src, dst_y, -1, kernelSobel_x, Point(-1, -1));
filter2D(src, dst_y, -1, kernelSobel_y, Point(-1, -1));
filter2D(src, dst_y, -1, kernelLap, Point(-1, -1));
namedWindow("dst_x_image", WINDOW_AUTOSIZE);
imshow("dst_x_image", dst_x);
namedWindow("dst_y_image", WINDOW_AUTOSIZE);
imshow("dst_y_image", dst_y);
namedWindow("dstSobel_x_image", WINDOW_AUTOSIZE);
imshow("dstSobel_x_image", dstSobel_x);
namedWindow("dstSobel_y_image", WINDOW_AUTOSIZE);
imshow("dstSobel_y_image", dstSobel_y);
namedWindow("dstLap_image", WINDOW_AUTOSIZE);
imshow("dstLap_image", dstLap);
waitKey(0);
return 0;
}