1,opencv dots_image_kernel
cpp
// halcon dots_image kernel估算(d=5)
cv::Mat getDotKernel(int d = 5){
// 保证d为正的奇数
d |= 0x01;
cv::Mat kernel = cv::Mat::zeros(d + 2, d + 2, CV_8UC1);
int cx = kernel.cols / 2;
int cy = kernel.rows / 2;
int cnt255 = 0, cnt128 = 0, cnt0 = 0;
for (int row = 0; row < kernel.rows; ++row) {
for (int col = 0; col < kernel.cols; ++col) {
float dis = std::sqrt(std::pow(col - cx, 2) + std::pow(row - cy, 2));
if (dis < d / 2.0){
kernel.at<uchar>(row, col) = 255;
cnt255++;
}
else if (dis < d / 2.0 + 1.0){
kernel.at<uchar>(row, col) = 128;
cnt128++;
}
else {
kernel.at<uchar>(row, col) = 0;
cnt0++;
}
}
}
//
cv::imwrite("kernel8u.0.png", kernel);
//
for (int row = 0; row < kernel.rows; ++row) {
for (int col = 0; col < kernel.cols; ++col){
switch (kernel.at<uchar>(row, col))
{
case 255:
kernel.at<uchar>(row, col) = cnt128;
break;
case 128:
kernel.at<uchar>(row, col) = -cnt255;
default:
break;
}
}
}
cv::imwrite("kernel8u.1.png", kernel);
//
if (cnt255 *cnt128){
kernel.convertTo(kernel, CV_32F, 1.0 / (cnt255*cnt128));
}
return kernel;
}
2,单元测试
3,结果