opencv中利用at访问像素元素出现的bug

以下代码运行出现如图1所示错误:

#include

#include

using namespace std;
using namespace cv;

Mat denois_abs(Mat & src);

int main()
{
Mat src = imread("F:\\wangjiao\\picture\\1.bmp",0);
Mat dst =  denois_abs(src);
imshow("dst",dst);
imwrite("F:\\wangjiao\\20180330\\denois_abs_result.bmp", dst);
return 0;
}


Mat denois_abs(Mat & src)
{
float a = 1.5;
float b = 0.5;
Mat dst = src.clone();
Mat var = src * a + b;
for (int r = 1; r < src.rows - 1; ++r)
{
for (int c = 1; c < src.cols - 1; ++c)
{
float weight_sum = 0.0f;
float val_sum = 0.0f;
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
float delta = src.at(r + i, c + j) - src.at(r, c);
delta *= delta;
float weight = 1.0f - delta / (delta + var.at(r, c));


weight_sum += weight;
val_sum += src.at(r + i, c + j) * weight;
}
}
dst.at(r, c) = val_sum / weight_sum;
}
}
return dst;

}

 opencv中利用at访问像素元素出现的bug_第1张图片

                                                               图1

解决方案一:

#include
#include

using namespace std;
using namespace cv;

Mat denois_abs(Mat & src);

int main()
{
Mat src = imread("F:\\wangjiao\\picture\\1.bmp",0);
Mat dst =  denois_abs(src);
imshow("dst",dst);
imwrite("F:\\wangjiao\\20180330\\denois_abs_result.bmp", dst);
return 0;
}

Mat denois_abs(Mat & src)
{
float a = 1.5;
float b = 0.5;
Mat dst = src.clone();
Mat var = src * a + b;
for (int r = 1; r < src.rows - 1; ++r)
{
for (int c = 1; c < src.cols - 1; ++c)
{
float weight_sum = 0.0f;
float val_sum = 0.0f;
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
float delta = src.at(r + i, c + j) - src.at(r, c);
delta *= delta;
float weight = 1.0f - delta / (delta + var.at(r, c));


weight_sum += weight;
val_sum += src.at(r + i, c + j) * weight;
}
}
dst.at(r, c) = val_sum / weight_sum;
}
}
return dst;

}

解决方案二:

#include
#include

using namespace std;
using namespace cv;

Mat denois_abs(Mat & src);
int main()
{
Mat src = imread("F:\\wangjiao\\picture\\1.bmp",0);
Mat dst =  denois_abs(src);
imshow("dst",dst);
imwrite("F:\\wangjiao\\20180330\\denois_abs_result.bmp", dst);
return 0;
}
Mat denois_abs(Mat & src)
{
src.convertTo(src,CV_32FC1);
float a = 1.5;
float b = 0.5;
Mat dst = src.clone();
Mat var = src * a + b;
for (int r = 1; r < src.rows - 1; ++r)
{
for (int c = 1; c < src.cols - 1; ++c)
{
float weight_sum = 0.0f;
float val_sum = 0.0f;
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
float delta = src.at(r + i, c + j) - src.at(r, c);
delta *= delta;
float weight = 1.0f - delta / (delta + var.at(r, c));
weight_sum += weight;
val_sum += src.at(r + i, c + j) * weight;
}
}
dst.at(r, c) = val_sum / weight_sum;
}
}
dst.convertTo(dst, CV_8UC1);
return dst;
}

 

你可能感兴趣的:(opencv中利用at访问像素元素出现的bug)