# 2. 膨胀的具体实现

## 1) OpenCV实现

Mat img = imread(imagename, IMREAD_GRAYSCALE);
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}

//OpenCV方法
Mat dilated_cv;
dilate(img, dilated_cv, Mat());


dilate()函数第一个参数表示输入影像，第二个参数表示输出影像，第三个表示一个默认的核，在3X3的范围内寻找最大值。

## 2) C/C++实现

//从文件中读取成灰度图像
const char* imagename = "D:\\Data\\imgDemo\\lena.jpg";
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}

//自定义方法
Mat dilated_my;
dilated_my.create(img.rows, img.cols, CV_8UC1);
for (int i = 0; i < img.rows; ++i)
{
for (int j = 0; j < img.cols; ++j)
{
//uchar minV = 255;
uchar maxV = 0;

//遍历周围最大像素值
for (int yi = i-1; yi <= i+1; yi++)
{
for (int xi = j-1; xi <= j+1; xi++)
{
if (xi<0||xi>= img.cols|| yi<0 || yi >= img.rows)
{
continue;
}
//minV = (std::min<uchar>)(minV, img.at<uchar>(yi, xi));
maxV = (std::max<uchar>)(maxV, img.at<uchar>(yi, xi));
}
}
dilated_my.at<uchar>(i, j) = maxV;
}
}


## 3) 验证与结果

#include <iostream>
#include <algorithm>
#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
//从文件中读取成灰度图像
const char* imagename = "D:\\Data\\imgDemo\\lena.jpg";
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}

//OpenCV方法
Mat dilated_cv;
dilate(img, dilated_cv, Mat());

//自定义方法
Mat dilated_my;
dilated_my.create(img.rows, img.cols, CV_8UC1);
for (int i = 0; i < img.rows; ++i)
{
for (int j = 0; j < img.cols; ++j)
{
//uchar minV = 255;
uchar maxV = 0;

//遍历周围最大像素值
for (int yi = i-1; yi <= i+1; yi++)
{
for (int xi = j-1; xi <= j+1; xi++)
{
if (xi<0||xi>= img.cols|| yi<0 || yi >= img.rows)
{
continue;
}
//minV = (std::min<uchar>)(minV, img.at<uchar>(yi, xi));
maxV = (std::max<uchar>)(maxV, img.at<uchar>(yi, xi));
}
}
dilated_my.at<uchar>(i, j) = maxV;
}
}

//比较两者的结果
Mat c;
compare(dilated_cv, dilated_my, c, CMP_EQ);

//显示
imshow("原始", img);
imshow("膨胀_cv", dilated_cv);
imshow("膨胀_my", dilated_my);
imshow("比较结果", c);

waitKey();

return 0;
}


# 3. 腐蚀的具体实现

#include <iostream>
#include <algorithm>
#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
//从文件中读取成灰度图像
const char* imagename = "D:\\Data\\imgDemo\\lena.jpg";
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}

//OpenCV方法
Mat eroded_cv;
erode(img, eroded_cv, Mat());

//自定义方法
Mat eroded_my;
eroded_my.create(img.cols, img.rows, CV_8UC1);
for (int i = 0; i < img.rows; ++i)
{
for (int j = 0; j < img.cols; ++j)
{
uchar minV = 255;
//uchar maxV = 0;

//遍历周围最大像素值
for (int yi = i-1; yi <= i+1; yi++)
{
for (int xi = j-1; xi <= j+1; xi++)
{
if (xi<0||xi>= img.cols|| yi<0 || yi >= img.rows)
{
continue;
}
minV = (std::min<uchar>)(minV, img.at<uchar>(yi, xi));
//maxV = (std::max<uchar>)(maxV, img.at<uchar>(yi, xi));
}
}
eroded_my.at<uchar>(i, j) = minV;
}
}

//比较两者的结果
Mat c;
compare(eroded_cv, eroded_my, c, CMP_EQ);

//显示
imshow("原始", img);
imshow("膨胀_cv", eroded_cv);
imshow("膨胀_my", eroded_my);
imshow("比较结果", c);

waitKey();

return 0;
}


posted @ 2019-03-31 23:01  charlee44  阅读(4203)  评论(0编辑  收藏