網路上可以找到相關資訊及說明,這邊不再說明。可參考以下網址:
https://blog.youkuaiyun.com/Chaolei3/article/details/79618602
http://nova.bime.ntu.edu.tw/~ttlin/Course01/lecture_notes/C1_LECTURE_NOTE_09(2%20in%201).pdf
這邊直接說明程式步驟。
- 載入圖檔
- 進行灰階處理
- 進行二值化(可依需求考慮是否處理)
- 進行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);
}