先看halcon算子的使用:
read_image (Image,'photometric_stereo/embossed_01')
mean_image (Image,ImageMean,60,60)
dyn_threshold (Image, ImageMean, RegionDynThresh, 15, 'not_equal')
再看OpenCV的实现:
void CImagePreprocessing::dynamic_threshold_referHalcon(cv::Mat &frame_gray, int ksize, int offset) //仿Halcon
{
cv::Mat srcMean;
cv::Mat binary1;
cv::Mat binary2;
//均值滤波
blur(frame_gray, srcMean, cv::Size(9, 9));
//动态阈值
binary1 = cv::Mat::zeros(frame_gray.size(), CV_8UC1);
_HalconDynThreshold(frame_gray, srcMean, binary1, offset, Equal);
}
void CImagePreprocessing::_HalconDynThreshold(cv::Mat &src, cv::Mat &srcMean, cv::Mat &result, int offset, int LightDark)
{
//使用Opencv实现Halcon中的动态阈值
//src是原图,灰度图
//srcMean是平滑滤波之后的图
//最好不要把Offset这个变量设置为0,因为这样会导致最后找到太多很小的regions,而这基本上都是噪声。
//所以这个值最好是在5-40之间,值选择的越大,提取出来的regions就会越小。
int r = src.rows; //高
int c = src.cols; //宽
int Value = 0;
for (int i = 0; i < r; i++)
{
uchar *datasrc = src.ptr<uchar>(i); //指针访问图像像素
uchar *datasrcMean = srcMean.ptr<uchar>(i);
uchar *dataresult = result.ptr<uchar>(i);
for (int j = 0; j < c; j++)
{
switch (LightDark)
{
case Light:
Value = datasrc[j] - datasrcMean[j];
if (Value >= offset)
{
dataresult[j] = 255;
}
break;
case Dark:
Value = datasrcMean[j] - datasrc[j];
if (Value >= offset)
{
dataresult[j] = 255;
}
break;
case Equal:
Value = datasrc[j] - datasrcMean[j];
if (Value >= -offset && Value <= offset)
{
dataresult[j] = 255;
}
break;
case Not_equal:
Value = datasrc[j] - datasrcMean[j];
if (Value < -offset || Value > offset)
{
dataresult[j] = 255;
}
break;
default:
break;
}
}
}
}
|