OpenCV将我自己的过滤器应用于彩色图像



我正在使用OpenCV C++过滤图像颜色。我想使用自己的矩阵过滤图像。请参阅此代码:

img= "c:/Test/tes.jpg";
Mat im = imread(img);

然后我想过滤/乘以我的矩阵(这个矩阵可以用另一个矩阵 3x3 替换(

Mat filter = (Mat_<double>(3, 3) <<17.8824, 43.5161, 4.11935, 
                                   3.45565, 27.1554, 3.86714, 
                                   0.0299566, 0.184309, 1.46709);

如何将img垫矩阵与我自己的矩阵相乘?我仍然不明白如何将 3 通道 (RGB( 矩阵与另一个矩阵(单通道(相乘,并用新颜色生成图像。

你应该看看OpenCV文档。您可以使用此函数:

filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT )

这会在你的代码中给你这样的东西:

Mat output;
filter2D(im, output, -1, filter);

关于您对 3 通道矩阵的问题;它在文档中指定:

kernel – convolution kernel (or rather a correlation kernel), a single-channel floating point matrix; if you want to apply different kernels to different channels, split the image into separate color planes using split() and process them individually.

因此,默认情况下,您的"滤镜"矩阵将平等地应用于每个颜色平面。

编辑 您可以在opencv网站上找到一个功能齐全的示例:http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html

最新更新