根据你的例子,你想要完成的事情被称为与核的卷积.
您可以设置内核并调用cv::filter2D() http://docs.opencv.org/modules/imgproc/doc/filtering.html#void%20filter2D(InputArray%20src,%20OutputArray%20dst,%20int%20ddepth,%20InputArray%20kernel,%20Point%20anchor,%20double%20delta,%20int%20borderType)为您申请:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main()
{
cv::Mat img = cv::imread("input.jpg");
if (img.empty())
{
std::cout << "!!! Failed to open input image" << std::endl;
return -1;
}
cv::Mat kernel = (cv::Mat_<float>(3, 3) << 0, 0, 0,
0, 1, 0,
0, 0, 0);
cv::Mat dst;
cv::filter2D(img, dst, -1, kernel, cv::Point(-1, -1), 0, cv::BORDER_DEFAULT);
cv::imshow("output", dst);
cv::waitKey(0);
return 0;
}
因此,无需迭代像素并自行执行计算。
OpenCV 文档对此进行了解释:制作您自己的线性滤波器! http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html.