对图像进行滤波可理解为用其核心矩阵对图像矩阵做处理。如在本例中是对图像做锐化处理,锐化滤波器的核心矩阵为:
0 -1 0
-1 5 -1
0 -1 0
即是将一个像素乘以5后与之相邻的元素(乘以系数后)之和。 opencv中代码如下:
#include <QCoreApplication>
#include <opencv2/opencv.hpp>
void sharpen(const cv::Mat &image,cv::Mat &result){
result.create(image.size(),image.type());
for(int j = 1; j < image.rows - 1; j++){//处理除第一行和最后一行的所有行
const uchar* previous =
image.ptr<const uchar>(j-1);//上一行
const uchar* current =
image.ptr<const uchar>(j);//当前行
const uchar* next =
image.ptr<const uchar>(j+1);//下一行
uchar* output = result.ptr<uchar>(j);//输出行
for(int i=1; i<image.cols-1; i++){
*output++= cv::saturate_cast<uchar>( //防溢出
5*current[i] - current[i-1]
-current[i+1] - previous[i] - next[i]);
}
}
//未处理的像素值设为0
result.row(0).setTo(cv::Scalar(0));
result.row(result.rows-1).setTo(cv::Scalar(0));
result.col(0).setTo(cv::Scalar(0));
result.col(result.cols-1).setTo(cv::Scalar(0));
}
int main(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
cv::Mat image = cv::imread("E:/image/opencv2/boldt.jpg",0);
cv::Mat result;
sharpen(image,result);
cv::imshow("iamge",image);
cv::imshow("result",result);
cv::imwrite("result.bmp",result);
cv::waitKey();
return 0;
//return a.exec();
}
一个核就定义了一个图像滤波器,opencv定义了一个特殊的函数来完成滤波处理,filter2D, 我们可以自定义自己的核用于其中,用filter来实现上述功能;
#include <QCoreApplication>
#include <opencv2/opencv.hpp>
void sharpen2D(const cv::Mat &image,cv::Mat &result){
//构造核
cv::Mat kernel(3,3,CV_32F,cv::Scalar(0));
//设置核中指定值
kernel.at<float>(1,1) = 5.0;
kernel.at<float>(0,1) = -1.0;
kernel.at<float>(2,1) = -1.0;
kernel.at<float>(1,0) = -1.0;
kernel.at<float>(1,2) = -1.0;
//滤波处理
cv::filter2D(image,result,image.depth(),kernel);
}
int main(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
cv::Mat image = cv::imread("E:/image/opencv2/boldt.jpg",0);
cv::Mat result;
sharpen2D(image,result);
cv::imshow("iamge",image);
cv::imshow("result",result);
cv::imwrite("result.bmp",result);
cv::waitKey();
return 0;
//return a.exec();
}
filter2D介绍
C++: void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT )
Parameters:
src – Source image.
dst – Destination image of the same size and the same number of channels as src .
ddepth – Desired depth of the destination image. If it is negative, it will be the same as src.depth() .
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.
anchor – Anchor of the kernel that indicates the relative position of a filtered point within the kernel. The anchor should lie within the kernel. The special default value (-1,-1) means that the anchor is at the kernel center.
delta – Optional value added to the filtered pixels before storing them in dst .
borderType – Pixel extrapolation method. See borderInterpolate() for details.
The function applies an arbitrary linear filter to an image. In-place operation is supported. When the aperture is partially outside the image, the function interpolates outlier pixel values according to the specified border mode.
The function does actually compute correlation, not the convolution:
\texttt{dst} (x,y) = \sum _{ \stackrel{0\leq x’ < \texttt{kernel.cols},}{0\leq y’ < \texttt{kernel.rows}} } \texttt{kernel} (x’,y’)* \texttt{src} (x+x’- \texttt{anchor.x} ,y+y’- \texttt{anchor.y} )
That is, the kernel is not mirrored around the anchor point. If you need a real convolution, flip the kernel using flip() and set the new anchor to (kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1) .
The function uses the DFT-based algorithm in case of sufficiently large kernels (~11 x 11 or larger) and the direct algorithm (that uses the engine retrieved by createLinearFilter() ) for small kernels.
本文深入探讨了图像滤波的概念及其在OpenCV库中的应用,详细介绍了锐化滤波器的工作原理及其实现方式,包括核心矩阵的使用和自定义滤波器的创建。同时,解释了filter2D函数的参数含义与使用方法,提供了通过核矩阵实现图像增强的技术细节。
463

被折叠的 条评论
为什么被折叠?



