【发布时间】:2017-03-12 00:04:02
【问题描述】:
以下程序不工作。我正在实施高斯 3x3 过滤器来锐化图像文件FACE DETECTION.png,但结果只是显示白色。我认为在卷积运算中sum的值大于255。我需要一个解决方案...
代码:
int main()
{
Mat src, dst;
float sum;
/// Load an image
src = imread("FACE DETECTION.png", 0);
if( !src.data )
{ return -1; }
// define the kernel
float Kernel[3][3] = {
{1.0, 2.0, 1.0},
{2.0, 4.0, 2.0},
{1.0, 2.0, 1.0}
};
dst = src.clone();
for(int y = 0; y < src.rows; y++)
for(int x = 0; x < src.cols; x++)
dst.at<uchar>(y,x) = 0.0;
//convolution operation
for(int y = 1; y < src.rows - 1; y++){
for(int x = 1; x < src.cols - 1; x++){
sum = 0.0;
for(int k = -1; k <= 1;k++){
for(int j = -1; j <=1; j++){
sum = sum + Kernel[j+1][k+1]*src.at<uchar>(y - j, x - k);
sum = sum>255? 255:sum;
sum = sum<0? 0:sum;
}
}
dst.at<uchar>(y,x) = sum;
}
}
namedWindow("final");
imshow("final", dst);
namedWindow("initial");
imshow("initial", src);
waitKey();
return 0;
}
【问题讨论】:
标签: opencv image-processing gaussian convolution