帮看一下这一段opencv代码,实现的是一个简单的卷积操作

C++语言 码拜 7年前 (2017-04-19) 950次浏览
总是出错
#in[i]clude<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
void convolution(Mat& image, Mat& result, float* Template, int widthTemplate, int heightTemplate);
int main()
{
Mat srcImage = imread(“boldt.jpg”,0);
Mat dstImage(srcImage.size(), CV_32FC1);
imshow(“1”, srcImage);
float Template[9] = { -1, 0, 1,
-1, 0, 1,
-1, 0, 1 };
convolution(srcImage, dstImage, Template, 3, 3);
dstImage.convertTo(dstImage, CV_8UC1);
imshow(“2”, dstImage);
waitKey(0);
return 0;
}
void convolution(Mat& image, Mat& result, float* Template, int widthTemplate, int heightTemplate)
{
int halfWidth = widthTemplate / 2;
int halfHeight = heightTemplate / 2;
for (int y = halfHeight; y < image.rows – halfHeight; y++)
{
for (int x = halfWidth; y < image.cols – halfWidth; x++)
{
float curDx = 0;
for (int j = -halfHeight; j <= halfHeight; j++)
{
for (int i = -halfWidth; i <= halfWidth; i++)
{
curDx += image.at<uchar>(y + j, x + i)**(Template + (j+halfHeight)*widthTemplate + i + halfWidth);
}
}
result.at<float>(y, x) = curDx;
}
}
}
解决方案

80

for (int x = halfWidth; y < image.cols – halfWidth; x++)
能否应该是
for (int x = halfWidth; x < image.cols – halfWidth; x++)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明帮看一下这一段opencv代码,实现的是一个简单的卷积操作
喜欢 (0)
[1034331897@qq.com]
分享 (0)