从官方文档可以找到算子的原理
C:/Program Files/MVTec/HALCON-18.11-Steady/doc/html/reference/operators/emphasize.html
运算符强调强调图像的高频区域(边缘和角落)。 生成的图像看起来更清晰。
首先,该过程使用低通 (mean_image) 进行滤波。 得到的灰度值 (res) 由获得的灰度值 (mean) 和原始灰度值 (orig) 计算得出,如下所示:
res := round((orig - mean) * Factor) + orig
因子用作对比度增加的度量。 分频由滤波器矩阵的大小决定:矩阵越大,分频越低。
作为边缘处理,灰度值镜像在图像的边缘。 灰度值的上溢和/或下溢被剪裁。
void CImagePreprocessing::emphasize(const cv::Mat &input, cv::Mat &output, int MaskWidth, int MaskHeight, float Factor)
{
//公式res := round((orig - mean) * Factor) + orig
//等价于在MaskHeight、MaskWidth的空间内中心化后增加方差
cv::Mat mean;
//等价于求指定范围窗口内的均值
cv::blur(input, mean, cv::Size(MaskWidth, MaskHeight));
output.create(input.size(), input.type());
if (input.type() == CV_8UC1)
{
for (int i = 0; i < input.rows; i++)
{
const uchar *rptr = input.ptr<uchar>(i);
uchar *mptr = mean.ptr<uchar>(i);
uchar *optr = output.ptr<uchar>(i);
for (int j = 0; j < input.cols; j++)
{
optr[j] = cv::saturate_cast<uchar>(round((rptr[j] - mptr[j]) * Factor) + rptr[j] * 1.0f);
}
}
}
else if (input.type() == CV_8UC3)
{
for (int i = 0; i < input.rows; i++)
{
const uchar *rptr = input.ptr<uchar>(i);
uchar *mptr = mean.ptr<uchar>(i);
uchar *optr = output.ptr<uchar>(i);
for (int j = 0; j < input.cols; j++)
{
//饱和转换 小于0的值会被置为0 大于255的值会被置为255
optr[j * 3] = cv::saturate_cast<uchar>(round((rptr[j * 3] - mptr[j * 3]) * Factor) + rptr[j * 3] * 1.0f);
optr[j * 3 + 1] = cv::saturate_cast<uchar>(round((rptr[j * 3 + 1] - mptr[j * 3 + 1]) * Factor) + rptr[j * 3 + 1] * 1.0f);
optr[j * 3 + 2] = cv::saturate_cast<uchar>(round((rptr[j * 3 + 2] - mptr[j * 3 + 2]) * Factor) + rptr[j * 3 + 2] * 1.0f);
}
}
}
}
---
参考文献
Opencv实现图像增强,仿照halcon的函数emphasize(Image : ImageEmphasize : MaskWidth, MaskHeight, Factor : )