膨脹(Dilation)及腐蝕(Erosion)原理及代碼

網路上可以找到相關資訊及說明,這邊不再說明。可參考以下網址:

https://blog.csdn.net/Chaolei3/article/details/79618602
http://nova.bime.ntu.edu.tw/~ttlin/Course01/lecture_notes/C1_LECTURE_NOTE_09(2%20in%201).pdf

這邊直接說明程式步驟。

  1. 載入圖檔
  2. 進行灰階處理
  3. 進行二值化(可依需求考慮是否處理)
  4. 進行Dilation或者Erosion等

本文採用法簡單作法,使用3X3的模塊,掃描原圖。Dilation就是,當掃描原圖點附近3X3其中一個區塊為1時,其點為1 。Erosion則是原圖點附近3X3區塊,1點為0時,皆為0,而Opening就是Erosion+Dilation。Closing是Dilation+Erosion。

程式碼如下(邊框部分未處理):

#include<opencv2/opencv.hpp>
#include<iostream>
using  namespace cv;
using namespace std;

Mat srcImage, grayImage, binarygray, erosion, dilation, closing, opening;


static void Erosion();
static void Dilation();
static void Closing();
static void Opening();

int main()
{

	srcImage = imread("C:\\Users\\User\\Downloads\\Image\\1cm.bmp");//路徑
	cvtColor(srcImage, grayImage, CV_RGB2GRAY);//灰階

	int threshold = 50;

	binarygray = Mat::zeros(grayImage.rows, grayImage.cols, grayImage.type());
	//二值化
	for (int i = 0; i < grayImage.rows; i++)
	{
		for (int j = 0; j < grayImage.cols; j++)
		{
			//binarygray.ptr<uchar>(i)[j] = grayImage.ptr<uchar>(i)[j];
			if (grayImage.ptr<uchar>(i)[j]>threshold){
				binarygray.ptr<uchar>(i)[j] = 255;
			}
			else{
				binarygray.ptr<uchar>(i)[j] = 0;

			}
		}
	}


	Erosion();
	Dilation();
	Closing();
	Opening();
	waitKey(0);
	return 0;
}
static void Opening()
{

	erosion = Mat::zeros(binarygray.rows, binarygray.cols, binarygray.type());

	for (int i = 0; i < binarygray.rows-2; i++)
	{
		for (int j = 0; j < binarygray.cols-2; j++)
		{
			
				//if (binarygray.ptr<uchar>(i)[j + 1] >0 && binarygray.ptr<uchar>(i + 1)[j] >0 && binarygray.ptr<uchar>(i + 1)[j + 1] > 0 && binarygray.ptr<uchar>(i)[j] > 0)
				if (binarygray.ptr<uchar>(i)[j] >0 && binarygray.ptr<uchar>(i + 1)[j] >0 && binarygray.ptr<uchar>(i + 2)[j] > 0 &&
					binarygray.ptr<uchar>(i)[j + 1] >0 && binarygray.ptr<uchar>(i + 1)[j + 1] > 0 && binarygray.ptr<uchar>(i + 2)[j + 1] > 0 &&
					binarygray.ptr<uchar>(i)[j + 2] > 0 && binarygray.ptr<uchar>(i + 1)[j + 2] > 0 && binarygray.ptr<uchar>(i + 2)[j + 2] > 0)
				{
					erosion.ptr<uchar>(i)[j] = 255;
				}
				else
				{
					erosion.ptr<uchar>(i)[j] = 0;
				}

				//erosion.ptr<uchar>(i)[j] = binarygray.ptr<uchar>(i)[j];
		}

	}
	opening = Mat::zeros(binarygray.rows, binarygray.cols, binarygray.type());

	for (int i = 0; i < erosion.rows - 2; i++)
	{
		for (int j = 0; j < erosion.cols - 2; j++)
		{

			//if ((erosion.ptr<uchar>(i)[j + 1] + erosion.ptr<uchar>(i + 1)[j] + erosion.ptr<uchar>(i + 1)[j + 1] + erosion.ptr<uchar>(i)[j]) == 0)
			if ((erosion.ptr<uchar>(i)[j] + erosion.ptr<uchar>(i + 1)[j] + erosion.ptr<uchar>(i + 2)[j] +
				erosion.ptr<uchar>(i)[j + 1] + erosion.ptr<uchar>(i + 1)[j + 1] + erosion.ptr<uchar>(i + 2)[j + 1] +
				erosion.ptr<uchar>(i)[j + 2] + erosion.ptr<uchar>(i + 1)[j + 2] + erosion.ptr<uchar>(i + 2)[j + 2]) == 0)
			{
				opening.ptr<uchar>(i)[j] = 0;
			}
			else
			{
				opening.ptr<uchar>(i)[j] = 255;
			}

	}
	imshow("opening", opening);
	imwrite("opening.bmp",opening);

}

static void Closing()
{

	dilation = Mat::zeros(binarygray.rows, binarygray.cols, binarygray.type());
	for (int i = 0; i < binarygray.rows-2; i++)
	{
		for (int j = 0; j < binarygray.cols-2; j++)
		{

				//if ((binarygray.ptr<uchar>(i)[j + 1] + binarygray.ptr<uchar>(i + 1)[j] + binarygray.ptr<uchar>(i + 1)[j + 1] + binarygray.ptr<uchar>(i)[j]) == 0)
				if ((binarygray.ptr<uchar>(i)[j] + binarygray.ptr<uchar>(i + 1)[j] + binarygray.ptr<uchar>(i + 2)[j] +
					binarygray.ptr<uchar>(i)[j + 1] + binarygray.ptr<uchar>(i + 1)[j + 1] + binarygray.ptr<uchar>(i + 2)[j + 1] +
					binarygray.ptr<uchar>(i)[j + 2] + binarygray.ptr<uchar>(i + 1)[j + 2] + binarygray.ptr<uchar>(i + 2)[j + 2]) == 0)

				{
					dilation.ptr<uchar>(i)[j] = 0;
				}
				else
				{
					dilation.ptr<uchar>(i)[j] = 255;
				}
		
				//dilation.ptr<uchar>(i)[j] = binarygray.ptr<uchar>(i)[j];
		}
	}

	closing = Mat::zeros(binarygray.rows, binarygray.cols, binarygray.type());
	for (int i = 0; i < dilation.rows - 2; i++)
	{
		for (int j = 0; j < dilation.cols - 2; j++)
		{

			//if (dilation.ptr<uchar>(i)[j + 1] >0 && dilation.ptr<uchar>(i + 1)[j] >0 && dilation.ptr<uchar>(i + 1)[j + 1] > 0 && dilation.ptr<uchar>(i)[j] > 0)
			if (dilation.ptr<uchar>(i)[j] >0 && dilation.ptr<uchar>(i + 1)[j] >0 && dilation.ptr<uchar>(i + 2)[j] > 0 &&
				dilation.ptr<uchar>(i)[j + 1] >0 && dilation.ptr<uchar>(i + 1)[j + 1] >0 && dilation.ptr<uchar>(i + 2)[j + 1] > 0 &&
				dilation.ptr<uchar>(i)[j + 2] > 0 && dilation.ptr<uchar>(i + 1)[j + 2] >0 && dilation.ptr<uchar>(i + 2)[j + 2] >0)
			{
				closing.ptr<uchar>(i)[j] = 255;
			}
			else
			{
				closing.ptr<uchar>(i)[j] = 0;
			}
		}

	}

	imshow("closing", closing);
	imwrite("closing.bmp", closing);
}

static void Dilation()
{
	dilation = Mat::zeros(binarygray.rows, binarygray.cols, binarygray.type());

	for (int i = 0; i < binarygray.rows-2; i++)
	{
		for (int j = 0; j < binarygray.cols-2; j++)
		{
				//if ((binarygray.ptr<uchar>(i)[j + 1] + binarygray.ptr<uchar>(i + 1)[j] + binarygray.ptr<uchar>(i + 1)[j + 1] + binarygray.ptr<uchar>(i)[j]) == 0)
				if ((binarygray.ptr<uchar>(i)[j] + binarygray.ptr<uchar>(i + 1)[j] + binarygray.ptr<uchar>(i + 2)[j] +
					binarygray.ptr<uchar>(i)[j + 1] + binarygray.ptr<uchar>(i + 1)[j + 1] + binarygray.ptr<uchar>(i + 2)[j + 1] +
					binarygray.ptr<uchar>(i)[j + 2] + binarygray.ptr<uchar>(i + 1)[j + 2] + binarygray.ptr<uchar>(i + 2)[j + 2]) == 0)

				{
					dilation.ptr<uchar>(i)[j] = 0;
				}
				else
				{
					dilation.ptr<uchar>(i)[j] = 255;
				}

				//dilation.ptr<uchar>(i)[j] = binarygray.ptr<uchar>(i)[j];
		}
	}

	imshow("dilation", dilation);
	imwrite("dilation.bmp", dilation);
}



static void Erosion()
{

	erosion = Mat::zeros(binarygray.rows, binarygray.cols, binarygray.type());

	for (int i = 0; i < binarygray.rows-2; i++)
	{
		for (int j = 0; j < binarygray.cols-2; j++)
		{
				//if (binarygray.ptr<uchar>(i)[j + 1] >0 && binarygray.ptr<uchar>(i + 1)[j] >0 && binarygray.ptr<uchar>(i + 1)[j + 1] > 0 && binarygray.ptr<uchar>(i)[j] > 0)
				if (binarygray.ptr<uchar>(i)[j] >0 && binarygray.ptr<uchar>(i + 1)[j] >0 && binarygray.ptr<uchar>(i + 2)[j] > 0 &&
					binarygray.ptr<uchar>(i)[j + 1] >0 && binarygray.ptr<uchar>(i + 1)[j + 1] > 0 && binarygray.ptr<uchar>(i + 2)[j + 1] > 0 &&
					binarygray.ptr<uchar>(i)[j + 2] > 0 && binarygray.ptr<uchar>(i + 1)[j + 2] > 0 && binarygray.ptr<uchar>(i + 2)[j + 2] > 0)
				{
					erosion.ptr<uchar>(i)[j] = 255;
				}
				else
				{
					erosion.ptr<uchar>(i)[j] = 0;
				}
					
				//erosion.ptr<uchar>(i)[j] = binarygray.ptr<uchar>(i)[j];
		}

	}

	imshow("erosion", erosion);
	imwrite("erosion.bmp", erosion);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值