简体   繁体   English

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

[英]OpenCV apply my own filter to a color image

I'm working in OpenCV C++ to filtering image color. 我正在使用OpenCV C ++过滤图像颜色。 I want to filter the image using my own matrix. 我想用我自己的矩阵过滤图像。 See this code: 看到这段代码:

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

And then i want to filtering/multiply with my matrix (this matrix can replaced with another matrix 3x3) 然后我想用我的矩阵过滤/乘法(这个矩阵可以用另一个矩阵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);

How to multiply the img mat matrix with my own matrix? 如何将img mat矩阵与我自己的矩阵相乘? I'm still not understand how to multiply 3 channel (RGB) matrix with another matrix (single channel) and resulted image with new color. 我仍然不明白如何将3通道(RGB)矩阵与另一个矩阵(单通道)相乘,并产生具有新颜色的图像。

you should take a look at the opencv documentation . 你应该看一下opencv文档 You could use this function: 你可以使用这个功能:

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

which would give you something like this in your code: 这会在你的代码中给你这样的东西:

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

About your question for 3-channel matrix; 关于3通道矩阵的问题; it is specified in the documentation: 它在文档中指定:

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.

So by default your "filter" matrix will be applied equally to each color plane. 因此,默认情况下,您的“过滤器”矩阵将同等地应用于每个颜色平面。

EDIT You find a fully functional example on the opencv site: http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html 编辑你在opencv网站上找到了一个功能齐全的例子: http//docs.opencv.org/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM