c++ - c++中的腐蚀函数没有给出任何输出
问题描述
我正在尝试实现一个在不使用 opencv 中内置的侵蚀功能的情况下侵蚀图片的功能。
我的方法是检查图像区域中是否有任何 0,如果内核为 1,则我无法设置 1,这意味着条件为假。然后我的第二个 if 语句意味着条件为真,然后我的 1 被设置。
void erosion(const cv::Mat& image, cv::Mat& erosion_image, const cv::Mat& kernel)
{
bool check;
int count;
int count_2;
int anchorx = kernel.rows / (2.0);
int anchory = kernel.cols / (2.0);
for (int x = 0; x < image.rows; x++) {
for (int y = 0; y < image.cols; y++) {
kernel.at<uchar>(x, y);
for (int count = 0; count < kernel.rows; count++) {
for (int count_2 = 0; count_2 < kernel.cols; count_2++) {
if (image.at<uchar>(x + count, y + count_2) == 0 && kernel.at<uchar>(count, count_2) == 1) {
check = false;
}
}
}
if (check) {
erosion_image.at<uchar>(x, y) = 1;
}
}
}
}
这是正确的方法吗?先感谢您
解决方案
这是二值图像腐蚀的定义。如果您处理灰度图像,您应该将像素值设置为其邻域Wiki-Erosion中点的最小值。
在下面的代码中,我使用了 255 而不是最大像素值 1 - 默认为 CV_8U 图像类型的 OpenCV 最大值。
注意,您还需要选择如何处理图像的边框像素。在下面的代码中,他们只是没有处理。二值图像的腐蚀可以表示为:
void erosion(const cv::Mat& image, cv::Mat& erosion_image, const cv::Mat& kernel)
{
bool check;
int count;
int count_2;
int anchorx = kernel.rows / (2.0);
int anchory = kernel.cols / (2.0);
for (int x = anchorx; x < image.rows - anchorx; x++) {
for (int y = anchory; y < image.cols - anchory; y++) {
check = true;
if (image.at<uchar>(x, y))
{
for (int count = 0; count < kernel.rows && check; count++) {
for (int count_2 = 0; count_2 < kernel.cols && check; count_2++) {
if (image.at<uchar>(x - anchorx + count, y - anchory + count_2) == 0 &&
kernel.at<uchar>(count, count_2) == 1) {
erosion_image.at<uchar>(x, y) = 0;
check = false;
}
}
}
if (check)
erosion_image.at<uchar>(x, y) = 255;
}
else
erosion_image.at<uchar>(x, y) = 0;
}
}
}
推荐阅读
- python - 如何使用 2 个条件属性对 python 列表进行排序
- javascript - 函数 moveon() - Javascript 权威指南 6/E
- java - 具有至少一个任何类型注释的方法的切入点
- javascript - 有什么方法可以从 AVI 视频文件中创建 ObjectURL?
- javascript - Angular 6外部js,css参考不起作用
- javascript - 与单个复选框交互后,无法在取消选中/选中标题复选框时取消选中/选中复选框 - Vuejs
- curl - Telegram Bot,发送文件的占位符?
- ubuntu - CURL - 没有输出到终端 - Ubuntu 14.04
- android - Android Studio 未显示设计预览但应用程序正在运行
- javascript - 如何在 javascript 函数中使用“get_template_directory_uri”