平台:Windows 10 20H2 Visual Studio 2015 OpenCV 4.5.3
二值图像
完整源码
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat Erode(Mat src, Mat Mask, uint32_t x0, uint32_t y0)
{
uint32_t x = 0, y = 0;
Mat dst(src.rows, src.cols, CV_8U, Scalar(0));
for (x = 0; x < src.cols; ++x)
{
for (y = 0; y < src.rows; ++y)
{
uint8_t dst_P = 1;
for (uint32_t xm = 0; xm < Mask.cols; ++xm)
{
for (uint32_t ym = 0; ym < Mask.rows; ++ym)
{
if (dst_P && Mask.at<uchar>(Point(xm, ym)) && (x + xm - x0) < src.cols && (y + ym - y0) < src.rows)
dst_P &= (Mask.at<uchar>(Point(xm, ym)) & src.at<uchar>(Point(x + xm - x0, y + ym - y0)));
}
if (!dst_P)
break;
}
if (dst_P)
dst.at<uchar>(Point(x, y)) = src.at<uchar>(Point(x, y));
else
dst.at<uchar>(Point(x, y)) = 0;
}
}
return dst;
}
Mat Dilate(Mat src, Mat Mask, uint32_t x0, uint32_t y0)
{
uint32_t x = 0, y = 0;
Mat dst(src.rows, src.cols, CV_8U, Scalar(0));
for (x = 0; x < src.cols; ++x)
{
for (y = 0; y < src.rows; ++y)
{
uint8_t dst_P = 0;
for (uint32_t xm = 0; xm < Mask.cols; ++xm)
{
for (uint32_t ym = 0; ym < Mask.rows; ++ym)
{
if (!dst_P && Mask.at<uchar>(Point(xm, ym)) && (x + xm - x0) < src.cols && (y + ym - y0) < src.rows)
dst_P |= src.at<uchar>(Point(x + xm - x0, y + ym - y0));
}
if (dst_P)
break;
}
for (uint32_t xm = 0; xm < Mask.cols; ++xm)
{
for (uint32_t ym = 0; ym < Mask.rows; ++ym)
{
if ((x + xm - x0) < src.cols && (y + ym - y0) < src.rows)
if (dst_P)
dst.at<uchar>(Point(x + xm - x0, y + ym - y0)) = 255;
else
dst.at<uchar>(Point(x + xm - x0, y + ym - y0)) = src.at<uchar>(Point(x, y));
}
}
}
}
return dst;
}
int main(int argc, char * argv[])
{
Mat image = imread("D:\\Work\\OpenCV\\Workplace\\Test_1\\2.jpg", 0);
if (image.empty())
{
printf("读取图片错误,请确认目录下是否有imread函数指定图片存在!\n");
return 0;
}
Mat binaryImage;
threshold(image, binaryImage, 127, 255, THRESH_BINARY);
uint8_t element_a[3][3] =
{ 1, 1, 1,
1, 1, 1,
1, 1, 1, };
Mat element = Mat(sizeof(element_a)/sizeof(element_a[0]), sizeof(element_a[0])/element_a[0][0], CV_8U, element_a);
imshow("原二值图像", binaryImage);
Mat erodedImage;
erode(binaryImage, erodedImage, element);
imshow("Opencv的腐蚀运算", erodedImage);
imshow("我的腐蚀运算", Erode(binaryImage, element, 1, 1));
Mat dilatedImage;
dilate(binaryImage, dilatedImage, element);
imshow("Opencv的膨胀运算", dilatedImage);
imshow("我的膨胀运算", Dilate(binaryImage, element, 1, 1));
Mat OpenImage;
morphologyEx(binaryImage, OpenImage, MORPH_OPEN, element);
imshow("Opencv的开运算", OpenImage);
imshow("我的开运算", Dilate(Erode(binaryImage, element, 1, 1), element, 1, 1));
Mat CloseImage;
morphologyEx(binaryImage, CloseImage, MORPH_CLOSE, element);
imshow("Opencv的闭运算", CloseImage);
imshow("我的闭运算", Erode(Dilate(binaryImage, element, 1, 1), element, 1, 1));
waitKey(0);
return 0;
}
效果
原图
腐蚀运算
膨胀运算
开运算
闭运算
灰度图像
完整源码
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat Erode(Mat src, Mat Mask, uint32_t x0, uint32_t y0)
{
uint32_t x = 0, y = 0;
Mat dst(src.rows, src.cols, CV_8U, Scalar(0));
for (x = 0; x < src.cols; ++x)
{
for (y = 0; y < src.rows; ++y)
{
uint8_t dst_P = 255;
for (uint32_t xm = 0; xm < Mask.cols; ++xm)
{
for (uint32_t ym = 0; ym < Mask.rows; ++ym)
{
if (dst_P && Mask.at<uchar>(Point(xm, ym)) && (x + xm - x0) < src.cols && (y + ym - y0) < src.rows)
if (dst_P > src.at<uchar>(Point(x + xm - x0, y + ym - y0)))
dst_P = src.at<uchar>(Point(x + xm - x0, y + ym - y0));
}
if (!dst_P)
break;
}
dst.at<uchar>(Point(x, y)) = dst_P;
}
}
return dst;
}
Mat Dilate(Mat src, Mat Mask, uint32_t x0, uint32_t y0)
{
uint32_t x = 0, y = 0;
Mat dst(src.rows, src.cols, CV_8U, Scalar(0));
for (x = 0; x < src.cols; ++x)
{
for (y = 0; y < src.rows; ++y)
{
uint8_t dst_P = 0;
for (uint32_t xm = 0; xm < Mask.cols; ++xm)
{
for (uint32_t ym = 0; ym < Mask.rows; ++ym)
{
if (dst_P != 255 && Mask.at<uchar>(Point(xm, ym)) && (x + xm - x0) < src.cols && (y + ym - y0) < src.rows)
if (dst_P < src.at<uchar>(Point(x + xm - x0, y + ym - y0)))
dst_P = src.at<uchar>(Point(x + xm - x0, y + ym - y0));
}
if (dst_P == 255)
break;
}
dst.at<uchar>(Point(x, y)) = dst_P;
}
}
return dst;
}
int main(int argc, char * argv[])
{
Mat image = imread("D:\\Work\\OpenCV\\Workplace\\Test_1\\8.jpg", 0);
if (image.empty())
{
printf("读取图片错误,请确认目录下是否有imread函数指定图片存在!\n");
return 0;
}
uint8_t element_a[7][7] =
{ 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, };
Mat element = Mat(sizeof(element_a)/sizeof(element_a[0]), sizeof(element_a[0])/element_a[0][0], CV_8U, element_a);
imshow("原灰度图像", image);
Mat erodedImage;
erode(image, erodedImage, element);
imshow("Opencv的腐蚀运算", erodedImage);
imshow("我的腐蚀运算", Erode(image, element, 3, 3));
Mat dilatedImage;
dilate(image, dilatedImage, element);
imshow("Opencv的膨胀运算", dilatedImage);
imshow("我的膨胀运算", Dilate(image, element, 3, 3));
Mat OpenImage;
morphologyEx(image, OpenImage, MORPH_OPEN, element);
imshow("Opencv的开运算", OpenImage);
imshow("我的开运算", Dilate(Erode(image, element, 3, 3), element, 3, 3));
Mat CloseImage;
morphologyEx(image, CloseImage, MORPH_CLOSE, element);
imshow("Opencv的闭运算", CloseImage);
imshow("我的闭运算", Erode(Dilate(image, element, 3, 3), element, 3, 3));
waitKey(0);
return 0;
}
效果
原图
腐蚀运算
膨胀运算
开运算
闭运算
|