📄 bingxingbianjiedib.cpp
字号:
#include "stdafx.h"
#include "windowsx.h"
#include "math.h"
#include "BingXingBianJieDib.h"
#include "MainFrm.h"
#include "DynSplitView2.h"
BingXingBianJieDib::BingXingBianJieDib()
{
}
BingXingBianJieDib::~BingXingBianJieDib()
{
}
///***************************************************************/
/*函数名称:Lunkuotiqu()
/*函数类型:void
/*功能:对图像进行轮廓提取。
/***************************************************************/
void BingXingBianJieDib::Lunkuotiqu()
{
LPBYTE p_data ; //原图数据区指针
int wide,height; //原图长、宽
// 指向源图像的指针
LPBYTE lpSrc;
// 指向缓存图像的指针
LPBYTE lpDst;
// 指向缓存DIB图像的指针
LPBYTE temp;
//循环变量
long i;
long j;
unsigned char n1,n2,n3,n4,n5,n6,n7,n8;
//像素值
unsigned char pixel;
// 暂时分配内存,以保存新图像
p_data=GetData();
wide=GetWidth();
height=GetHeight();
temp = new BYTE[wide * height];
for (j=0;j<height;j++)
{
for(i=0;i<wide;i++)
{
lpSrc = (LPBYTE)p_data + wide * j + i;
if(*lpSrc>127)
*lpSrc=255;
else
*lpSrc=0;
}
}
// 初始化新分配的内存,设定初始值为255
memset(temp, 255, wide * height);
for(j = 1; j <height-1; j++)
{
for(i = 1;i <wide-1; i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (LPBYTE)p_data + wide * j + i;
// 指向目标图像倒数第j行,第i个象素的指针
lpDst = (LPBYTE)temp + wide * j + i;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = (unsigned char)*lpSrc;
if(pixel == 0)
{
*lpDst = (unsigned char)0;
n1 = (unsigned char)*(lpSrc + wide -1);
n2 = (unsigned char)*(lpSrc + wide );
n3 = (unsigned char)*(lpSrc + wide +1);
n4 = (unsigned char)*(lpSrc -1);
n5= (unsigned char)*(lpSrc +1);
n6 = (unsigned char)*(lpSrc - wide -1);
n7 = (unsigned char)*(lpSrc - wide );
n8 = (unsigned char)*(lpSrc - wide +1);
//如果相邻的八个点都是黑点
if(n1+n2+n3+n4+n5+n6+n7+n8==0)
{
*lpDst = (unsigned char)255;
}
}
}
}
// 复制腐蚀后的图像
memcpy(p_data,temp, wide * height);
// 释放内存
delete temp;
}
///***************************************************************/
/*函数名称:Diedaifazhi(int *tongji)
/*函数类型:void
/*参数说明:tonji ---直方图灰度值统计
/*功能:对图像进行迭代阀值选取。
/***************************************************************/
void BingXingBianJieDib::Diedaifazhi(int *tongji)
{
// 循环变量
LONG i;
LONG j;
// 指向DIB象素指针
LPBYTE p_data;
// 找到DIB图像象素起始位置
p_data = GetData();
// DIB的宽度
LONG wide = this->GetDibWidthBytes();
// DIB的高度
LONG height = this->GetHeight();
// 迭代阀值
int T1, T2;
T1 = 127;
T2 = 0;
// 临时变量
int Temp0, Temp1, Temp2, Temp3;
Temp0 = Temp1 = Temp2 = Temp3 = 0;
while (true)
{
// 计算下一个迭代阀值
for (i = 0; i < T1 + 1; i++)
{
Temp0 += tongji[i] * i;
Temp1 += tongji[i];
}
for (i = T1 + 1; i < 256; i++)
{
Temp2 += tongji[i] * i;
Temp3 += tongji[i];
}
T2 = (Temp0 / Temp1 + Temp2 / Temp3) / 2;
// 看迭代结果是否已收敛
if (T1 == T2)
break;
else
T1 = T2;
}
// 对各像素进行灰度转换
for (j = 0; j < height; j ++)
{
for (i = 0; i < wide; i ++)
{
// 读取像素
unsigned char temp = *((unsigned char *)p_data + wide * j + i);
// 判断像素灰度值是否超出范围
if (temp < T1)
temp = 0;
else
temp = 255;
// 回写处理完的像素
*((unsigned char *)p_data + wide * j + i) = temp;
}
}
}
///***************************************************************/
/*函数名称:Fenbutongji(int *tongji)
/*函数类型:void
/*参数说明:tonji ---直方图灰度值统计
/*功能:对图像进行灰度值统计。
/***************************************************************/
void BingXingBianJieDib::Fenbutongji(int *tongji)
{
// 循环变量
LONG i;
LONG j;
//变量初始化
memset(tongji,0,sizeof(int) * 256);
// 指向DIB象素指针
LPBYTE p_data;
// 找到DIB图像象素起始位置
p_data = this->GetData();
// DIB的宽度
LONG wide = GetDibWidthBytes();
// DIB的高度
LONG height = GetHeight();
// 对各像素进行灰度转换
for (j = 0; j < height; j ++)
{
for (i = 0; i <wide; i++)
{
// 对各像素进行灰度统计
unsigned char temp = *((unsigned char *)p_data + wide * j + i);
tongji[temp]++;
}
}
}
///***************************************************************/
/*函数名称:Lunkuogenzong()
/*函数类型:void
/*功能:对图像进行边界跟踪。
/***************************************************************/
void BingXingBianJieDib::Lunkuogenzong()
{
// 指向源图像的指针
LPBYTE lpSrc;
LPBYTE p_data ;
// 指向缓存图像的指针
LPBYTE lpDst;
// 指向缓存DIB图像的指针
LPBYTE temp;
long wide;
long height;
//循环变量
long i;
long j;
//像素值
long pixel;
//是否找到起始点及回到起始点
bool bFindStartPoint;
//是否扫描到一个边界点
bool bFindPoint;
//起始边界点与当前边界点
Point StartPoint,CurrentPoint;
//八个方向和起始扫描方向
int Direction[8][2]={{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};
int BeginDirect;
p_data=GetData();
if(m_pBitmapInfoHeader->biBitCount<9) //灰度图像
{
wide=GetWidth();
height=GetHeight();
for (j=0;j<height;j++)
{
for(i=0;i<wide;i++)
{
lpSrc = (LPBYTE)p_data + wide * j + i;
if(*lpSrc>127)
*lpSrc=255;
else
*lpSrc=0;
}
}
// 暂时分配内存,以保存新图像
temp = new BYTE[wide*height];
// 初始化新分配的内存,设定初始值为255
lpDst = temp;
memset(lpDst, (BYTE)255, wide * height);
//先找到最左上方的边界点
bFindStartPoint = false;
for (j = 0;j < height && !bFindStartPoint;j++)
{
for(i = 0;i < wide && !bFindStartPoint;i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (LPBYTE)(p_data + wide * j + i);
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
if(pixel ==0)
{
bFindStartPoint = true;
StartPoint.Height = j;
StartPoint.Width = i;
// 指向目标图像倒数第j行,第i个象素的指针
lpDst = (LPBYTE)(temp + wide * j + i);
*lpDst = 0;
}
}
}
//由于起始点是在左下方,故起始扫描沿左上方向
BeginDirect = 0;
//跟踪边界
bFindStartPoint = false;
//从初始点开始扫描
CurrentPoint.Height = StartPoint.Height;
CurrentPoint.Width = StartPoint.Width;
while(!bFindStartPoint)
{
bFindPoint = false;
while(!bFindPoint)
{
//沿扫描方向查看一个像素
lpSrc = (LPBYTE)(p_data + wide * ( CurrentPoint.Height + Direction[BeginDirect][1])
+ (CurrentPoint.Width + Direction[BeginDirect][0]));
pixel = *lpSrc;
if(pixel== 0)
{
bFindPoint = true;
CurrentPoint.Height = CurrentPoint.Height + Direction[BeginDirect][1];
CurrentPoint.Width = CurrentPoint.Width + Direction[BeginDirect][0];
if(CurrentPoint.Height == StartPoint.Height && CurrentPoint.Width == StartPoint.Width)
{
bFindStartPoint = true;
}
lpDst = (LPBYTE)(temp + wide * CurrentPoint.Height + CurrentPoint.Width);
*lpDst = 0;
//扫描的方向逆时针旋转两格
BeginDirect--;
if(BeginDirect == -1)
BeginDirect = 7;
BeginDirect--;
if(BeginDirect == -1)
BeginDirect = 7;
}
else
{
//扫描方向顺时针旋转一格
BeginDirect++;
if(BeginDirect == 8)
BeginDirect = 0;
}
}
}
// 复制图像
memcpy(p_data, temp, wide * height);
// 释放内存
delete temp;
}
}
///***************************************************************/
/*函数名称:Zhongzitianchong(CPoint SeedPoint)
/*函数类型:void
/*参数说明:SeedPoint ---选区种子点
/*功能:对图像进行种子填充。
/***************************************************************/
void BingXingBianJieDib::Zhongzitianchong(CPoint SeedPoint)
{
// 指向源图像的指针
LPBYTE lpSrc;
//图像的高和宽
int height,wide ;
//像素值
long pixel;
//种子堆栈及指针
Seed *Seeds;
int StackPoint;
LPBYTE temp;
//当前像素位置
int iCurrentPixelx,iCurrentPixely;
temp =this->GetData();
lpSrc=temp;
if(m_pBitmapInfoHeader->biBitCount<9) //灰度图像
{
height=this->GetHeight();
wide=this->GetWidth();
for(int j=0;j<height;j++)
{
for(int i=0;i<wide;i++)
{
if(*lpSrc>110)
*lpSrc=255;
else
*lpSrc=0;
lpSrc++;
}
}
//初始化种子
Seeds = new Seed[wide*height];
Seeds[1].Height = SeedPoint.y;
Seeds[1].Width = SeedPoint.x;
StackPoint = 1;
while( StackPoint != 0)
{
//取出种子
iCurrentPixelx = Seeds[StackPoint].Width;
iCurrentPixely = Seeds[StackPoint].Height;
StackPoint--;
lpSrc = (LPBYTE)temp + wide * iCurrentPixely + iCurrentPixelx;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
//将当前点涂黑
*lpSrc =0;
//判断左面的点,如果为白,则压入堆栈
//注意防止越界
if(iCurrentPixelx > 0)
{
lpSrc = (LPBYTE)temp + wide * iCurrentPixely + iCurrentPixelx - 1;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely;
Seeds[StackPoint].Width = iCurrentPixelx - 1;
}
}
//判断上面的点,如果为白,则压入堆栈
//注意防止越界
if(iCurrentPixely < height - 1)
{
lpSrc = (LPBYTE)temp + wide * (iCurrentPixely + 1) + iCurrentPixelx;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely + 1;
Seeds[StackPoint].Width = iCurrentPixelx;
}
}
//判断右面的点,如果为白,则压入堆栈
//注意防止越界
if(iCurrentPixelx < wide - 1)
{
lpSrc = (LPBYTE)temp + wide * iCurrentPixely + iCurrentPixelx + 1;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely;
Seeds[StackPoint].Width = iCurrentPixelx + 1;
}
}
//判断下面的点,如果为白,则压入堆栈
//注意防止越界
if(iCurrentPixely > 0)
{
lpSrc = (LPBYTE)temp + wide * (iCurrentPixely - 1) + iCurrentPixelx;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely - 1;
Seeds[StackPoint].Width = iCurrentPixelx;
}
}
}
//释放堆栈
delete Seeds;
}
else //24位彩色
{
height=this->GetHeight();
wide=this->GetDibWidthBytes();
//初始化种子
Seeds = new Seed[wide*height];
Seeds[1].Height = SeedPoint.y;
Seeds[1].Width = SeedPoint.x*3;
StackPoint = 1;
while( StackPoint != 0)
{
//取出种子
iCurrentPixelx = Seeds[StackPoint].Width;
iCurrentPixely = Seeds[StackPoint].Height;
StackPoint--;
lpSrc = (LPBYTE)temp + wide * iCurrentPixely + iCurrentPixelx;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
//将当前点涂黑
*lpSrc =0;
//判断左面的点,如果为白,则压入堆栈
//注意防止越界
if(iCurrentPixelx > 0)
{
lpSrc = (LPBYTE)temp + wide * iCurrentPixely + iCurrentPixelx - 1;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely;
Seeds[StackPoint].Width = iCurrentPixelx - 1;
}
}
//判断上面的点,如果为白,则压入堆栈
//注意防止越界
if(iCurrentPixely < height - 1)
{
lpSrc = (LPBYTE)temp + wide * (iCurrentPixely + 1) + iCurrentPixelx;
//取得当前指针处的像素值,注意要转换为unsigned char型
pixel = *lpSrc;
if (pixel == 255)
{
StackPoint++;
Seeds[StackPoint].Height = iCurrentPixely + 1;
Seeds[StackPoint].Width = iCurrentPixelx;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -