# 数字图像处理（六）形态学处理之腐蚀、膨胀、开运算、闭运算

## 一. 图像腐蚀

void CImageProcessingView::OnXtxFs()
{
// TODO: 在此添加命令处理程序代码
if (numPicture == 0)
{
AfxMessageBox("请输入一张图像", MB_OK, 0);
return;
}

if (m_nBitCount != 24)
{
AfxMessageBox("输入图片不是24位", MB_OK, 0);
return;
}
AfxMessageBox("形态学-腐蚀1！", MB_OK, 0);
//int kelnel[3][3];//定义3*3的结构元素
/*结构元素
{	{1,1,1}
{1,1,1}
{1,1,1}
}
*/
int num;//记录每一行需要填充的字节
if (m_nWidth * 3 % 4 != 0)
{
num = 4 - m_nWidth * 3 % 4;
}
else
{
num = 0;
}

//打开临时的图片
FILE *fpo = fopen(BmpName, "rb");
FILE *fpw = fopen(BmpNameLin, "wb+");
unsigned char *ImageSize;
ImageSize = new unsigned char[m_nImage];

int x, y, i, j, val,xx,yy;
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 255;//这里我给val赋值为255，如果发现结构元素对应的像素有是0的点，就赋值成0
for (j = 0; j < 3; j++)
{
yy = y + j - 1;
for (i = 0; i < 3; i++)
{
xx = x + i - 1;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] == 0)
{
val = 0;
break;
}
}
if (val == 0) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num +1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}

fwrite(ImageSize, m_nImage, 1, fpw);

fclose(fpo);
fclose(fpw);
numPicture = 2;
level = 400;
Invalidate();
}

void CImageProcessingView::OnXtxFs2()
{
// TODO: 在此添加命令处理程序代码
if (numPicture == 0)
{
AfxMessageBox("请输入一张图像", MB_OK, 0);
return;
}

if (m_nBitCount != 24)
{
AfxMessageBox("输入图片不是24位", MB_OK, 0);
return;
}
AfxMessageBox("形态学-腐蚀1！", MB_OK, 0);
//int kelnel[2][2];//定义3*3的结构元素
/*结构元素
{	{1,1,1}
{1,1,1}
}
*/
int num;//记录每一行需要填充的字节
if (m_nWidth * 3 % 4 != 0)
{
num = 4 - m_nWidth * 3 % 4;
}
else
{
num = 0;
}

//打开临时的图片
FILE *fpo = fopen(BmpName, "rb");
FILE *fpw = fopen(BmpNameLin, "wb+");
unsigned char *ImageSize;
ImageSize = new unsigned char[m_nImage];

int x, y, i, j, val, xx, yy;
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 255;
for (j = 0; j < 2; j++)//这边是2*2
{
yy = y + j - 1;
for (i = 0; i < 2; i++)
{
xx = x + i - 1;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] == 0)
{
val = 0;
break;
}
}
if (val == 0) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}

fwrite(ImageSize, m_nImage, 1, fpw);

fclose(fpo);
fclose(fpw);
numPicture = 2;
level = 400;
Invalidate();
}

void CImageProcessingView::OnXtxFs3()
{
// TODO: 在此添加命令处理程序代码
if (numPicture == 0)
{
AfxMessageBox("请输入一张图像", MB_OK, 0);
return;
}

if (m_nBitCount != 24)
{
AfxMessageBox("输入图片不是24位", MB_OK, 0);
return;
}
AfxMessageBox("形态学-腐蚀1！", MB_OK, 0);
//int kelnel[3][3] = {	{0,1,0},
//						{1,1,1},
//						{0,1,0} };//定义3*3的结构元素
int kelnel[3][3] = {{ 1,0,1 },
{ 0,0,0 },
{ 1,0,1 } };//定义3*3的结构元素

int num;//记录每一行需要填充的字节
if (m_nWidth * 3 % 4 != 0)
{
num = 4 - m_nWidth * 3 % 4;
}
else
{
num = 0;
}

//打开临时的图片
FILE *fpo = fopen(BmpName, "rb");
FILE *fpw = fopen(BmpNameLin, "wb+");
unsigned char *ImageSize;
ImageSize = new unsigned char[m_nImage];

int x, y, i, j, val, xx, yy;
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 255;
for (j = 0; j < 3; j++)
{
yy = y + j - 1;
for (i = 0; i < 3; i++)
{
xx = x + i - 1;
if (kelnel[j][i]) continue;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] == 0)
{
val = 0;
break;
}
}
if (val == 0) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}

fwrite(ImageSize, m_nImage, 1, fpw);

fclose(fpo);
fclose(fpw);
numPicture = 2;
level = 400;
Invalidate();
}

## 一. 图像膨胀

void CImageProcessingView::OnXtxPz()
{
// TODO: 在此添加命令处理程序代码
// TODO: 在此添加命令处理程序代码
if (numPicture == 0)
{
AfxMessageBox("请输入一张图像", MB_OK, 0);
return;
}

if (m_nBitCount != 24)
{
AfxMessageBox("输入图片不是24位", MB_OK, 0);
return;
}
AfxMessageBox("形态学-膨胀1！", MB_OK, 0);
//int kelnel[3][3] = {	{0,1,0},
//						{1,1,1},
//						{0,1,0} };//定义3*3的结构元素
int kelnel[3][3] = { { 0,1,0 },
{ 1,1,1 },
{ 0,1,0 } };//定义3*3的结构元素

int num;//记录每一行需要填充的字节
if (m_nWidth * 3 % 4 != 0)
{
num = 4 - m_nWidth * 3 % 4;
}
else
{
num = 0;
}

//打开临时的图片
FILE *fpo = fopen(BmpName, "rb");
FILE *fpw = fopen(BmpNameLin, "wb+");
unsigned char *ImageSize;
ImageSize = new unsigned char[m_nImage];

int x, y, i, j, val, xx, yy;
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 0;//先赋值黑色，再赋值白色
for (j = 0; j < 3; j++)
{
yy = y + j - 1;
for (i = 0; i < 3; i++)
{
xx = x + i - 1;
if (kelnel[j][i] == 0) continue;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] != 0)
{
val = 255;
break;
}
}
if (val == 255) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}

fwrite(ImageSize, m_nImage, 1, fpw);

fclose(fpo);
fclose(fpw);
numPicture = 2;
level = 400;
Invalidate();
}

## 三. 图像开运算

void CImageProcessingView::OnXtxKys()
{
// TODO: 在此添加命令处理程序代码
if (numPicture == 0)
{
AfxMessageBox("请输入一张图像", MB_OK, 0);
return;
}

if (m_nBitCount != 24)
{
AfxMessageBox("输入图片不是24位", MB_OK, 0);
return;
}
AfxMessageBox("形态学-开运算！", MB_OK, 0);
//int kelnel[3][3] = {	{0,1,0},
//						{1,1,1},
//						{0,1,0} };//定义3*3的结构元素
int kelnel[3][3] = { { 0,1,0 },
{ 1,1,1 },
{ 0,1,0 } };//定义3*3的结构元素

int num;//记录每一行需要填充的字节
if (m_nWidth * 3 % 4 != 0)
{
num = 4 - m_nWidth * 3 % 4;
}
else
{
num = 0;
}

//打开临时的图片
FILE *fpo = fopen(BmpName, "rb");
FILE *fpw = fopen(BmpNameLin, "wb+");
unsigned char *ImageSize;
ImageSize = new unsigned char[m_nImage];

int x, y, i, j, val, xx, yy;
//先腐蚀
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 255;//先赋值成白色
for (j = 0; j < 3; j++)
{
yy = y + j - 1;
for (i = 0; i < 3; i++)
{
xx = x + i - 1;
if (kelnel[j][i]) continue;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] == 0)//只要有一个不是白色，赋值成白色
{
val = 0;
break;
}
}
if (val == 0) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}
//再膨胀
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 0;//先赋值成黑色
for (j = 0; j < 3; j++)
{
yy = y + j - 1;
for (i = 0; i < 3; i++)
{
xx = x + i - 1;
if (kelnel[j][i] == 0) continue;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] != 0)//只要有一个是白色，赋值成白色
{
val = 255;
break;
}
}
if (val == 255) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}

fwrite(ImageSize, m_nImage, 1, fpw);

fclose(fpo);
fclose(fpw);
numPicture = 2;
level = 400;
Invalidate();
}

## 四. 图像闭运算

void CImageProcessingView::OnXtxBys()
{
// TODO: 在此添加命令处理程序代码
if (numPicture == 0)
{
AfxMessageBox("请输入一张图像", MB_OK, 0);
return;
}

if (m_nBitCount != 24)
{
AfxMessageBox("输入图片不是24位", MB_OK, 0);
return;
}
AfxMessageBox("形态学-闭运算！", MB_OK, 0);
//int kelnel[3][3] = {	{0,1,0},
//						{1,1,1},
//						{0,1,0} };//定义3*3的结构元素
int kelnel[3][3] = { { 0,1,0 },
{ 1,1,1 },
{ 0,1,0 } };//定义3*3的结构元素

int num;//记录每一行需要填充的字节
if (m_nWidth * 3 % 4 != 0)
{
num = 4 - m_nWidth * 3 % 4;
}
else
{
num = 0;
}

//打开临时的图片
FILE *fpo = fopen(BmpName, "rb");
FILE *fpw = fopen(BmpNameLin, "wb+");
unsigned char *ImageSize;
ImageSize = new unsigned char[m_nImage];

int x, y, i, j, val, xx, yy;
//先膨胀
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 0;//先赋值成黑色
for (j = 0; j < 3; j++)
{
yy = y + j - 1;
for (i = 0; i < 3; i++)
{
xx = x + i - 1;
if (kelnel[j][i]) continue;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] != 0)//只要有一个是白色，则赋值成白色
{
val = 255;
break;
}
}
if (val == 255) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}
//再腐蚀
for (y = 1; y < m_nHeight - 1; y++)
{
for (x = 1; x < m_nWidth - 1; x++)
{
val = 255;//先赋值成白色
for (j = 0; j < 3; j++)
{
yy = y + j - 1;
for (i = 0; i < 3; i++)
{
xx = x + i - 1;
if (kelnel[j][i] == 0) continue;
if (m_pImage[(xx + yy*m_nWidth) * 3 + yy * num] == 0)//只要有一个不是白色，赋值成黑色
{
val = 0;
break;
}
}
if (val == 0) break;
}
ImageSize[(x + y*m_nWidth) * 3 + y*num] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 1] = unsigned char(val);
ImageSize[(x + y*m_nWidth) * 3 + y*num + 2] = unsigned char(val);
}
}

fwrite(ImageSize, m_nImage, 1, fpw);

fclose(fpo);
fclose(fpw);
numPicture = 2;
level = 400;
Invalidate();
}


• 点赞 5
• 评论 2
• 分享
x

海报分享

扫一扫，分享海报

• 收藏 12
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

hjxu2016

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
07-05 2万+

05-03 6541
03-21 3232
07-01 8576
05-03 3889
03-20 5万+
04-05 406