📄 splitproc.cpp
字号:
// 回存处理结果到DIB
for(i = 0; i < lLineBytes * lHeight; i++)
*(lpDIBBits + i) = m_temp[i];
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 释放内存
delete[] m_temp;
// 恢复光标
EndWaitCursor();
}
/*************************************************************************
*
* 函数名称:
* Laplacian()
*
* 参数:
* HDIB hDIB - 待处理的DIB
*
* 返回值:
* void - 无返回值
*
* 说明:
* 对图像进行拉普拉斯算子的边缘检测
*
************************************************************************/
void CSplitProc::Laplacian(HDIB hDIB)
{
// 循环变量
LONG i;
LONG j;
LONG k;
LONG l;
// 指向DIB的指针
LPBYTE lpDIB;
// 指向DIB象素指针
LPBYTE lpDIBBits;
// 锁定DIB
lpDIB = (LPBYTE) ::GlobalLock((HGLOBAL) hDIB);
// 找到DIB图像象素起始位置
lpDIBBits = m_clsDIB.FindDIBBits(lpDIB);
// 判断是否是24-bpp位图
if (m_clsDIB.DIBBitCount(lpDIB) != 24)
{
// 提示用户
MessageBox("请先将其转换为24位色位图,再进行处理!", "系统提示" , MB_ICONINFORMATION | MB_OK);
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 返回
return;
}
// 更改光标形状
BeginWaitCursor();
// DIB的宽度
LONG lWidth = m_clsDIB.DIBWidth(lpDIB);
// DIB的高度
LONG lHeight = m_clsDIB.DIBHeight(lpDIB);
// 计算图像每行的字节数
LONG lLineBytes = WIDTHBYTES(lWidth * 24);
// 不能用char,也不能用::strcpy()
unsigned char* m_temp;
m_temp=new unsigned char [lLineBytes * lHeight];
// 中间变量
int v_r, v_g, v_b, p_g;
// 检测模版
int g[9]={-1, -1, -1, -1, 8, -1, -1, -1, -1};
// 复制图象数据到中间缓存
for (i = 0; i < lLineBytes * lHeight; i++)
m_temp[i] = *(lpDIBBits + i);
// 3X3 模版
for (i = 0; i < lWidth; i++) //被处理像素在i列
{
for (j = 0; j < lHeight; j++) //被处理像素在j行
{
v_r = v_g = v_b = p_g = 0;
for (k = i - 1; k < i + 2; k++) //3*3模版
{
for (l = j - 1; l < j + 2; l++)
{
// 防止内存溢出
if (k >= 0 && l >= 0 && k < lWidth && l < lHeight)
{
v_r += *(lpDIBBits + l * lLineBytes + k * 3) * g[p_g];
v_g += *(lpDIBBits + l * lLineBytes + k * 3 + 1) * g[p_g];
v_b += *(lpDIBBits + l * lLineBytes + k * 3 + 2) * g[p_g];
p_g++;
}
}
}
if (v_r < 0)
v_r = 0;
if (v_g < 0)
v_g = 0;
if (v_b < 0)
v_b = 0;
m_temp[j * lLineBytes + i * 3] = v_r;
m_temp[j * lLineBytes + i * 3 + 1] = v_g;
m_temp[j * lLineBytes + i * 3 + 2] = v_b;
}
}
// 回存处理结果到DIB
for(i = 0; i < lLineBytes * lHeight; i++)
*(lpDIBBits + i) = m_temp[i];
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 释放内存
delete[] m_temp;
// 恢复光标
EndWaitCursor();
}
/*************************************************************************
*
* 函数名称:
* GuassLaplacian()
*
* 参数:
* HDIB hDIB - 待处理的DIB
*
* 返回值:
* void - 无返回值
*
* 说明:
* 对图像进行高斯——拉普拉斯算子的边缘检测
*
************************************************************************/
void CSplitProc::GuassLaplacian(HDIB hDIB)
{
// 循环变量
LONG i;
LONG j;
LONG k;
LONG l;
// 指向DIB的指针
LPBYTE lpDIB;
// 指向DIB象素指针
LPBYTE lpDIBBits;
// 锁定DIB
lpDIB = (LPBYTE) ::GlobalLock((HGLOBAL) hDIB);
// 找到DIB图像象素起始位置
lpDIBBits = m_clsDIB.FindDIBBits(lpDIB);
// 判断是否是24-bpp位图
if (m_clsDIB.DIBBitCount(lpDIB) != 24)
{
// 提示用户
MessageBox("请先将其转换为24位色位图,再进行处理!", "系统提示" , MB_ICONINFORMATION | MB_OK);
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 返回
return;
}
// 更改光标形状
BeginWaitCursor();
// DIB的宽度
LONG lWidth = m_clsDIB.DIBWidth(lpDIB);
// DIB的高度
LONG lHeight = m_clsDIB.DIBHeight(lpDIB);
// 计算图像每行的字节数
LONG lLineBytes = WIDTHBYTES(lWidth * 24);
// 不能用char,也不能用::strcpy()
unsigned char* m_temp;
m_temp=new unsigned char [lLineBytes * lHeight];
// 中间变量
int v_r, v_g, v_b, p_g;
// 检测模版
int g[25]={-2,-4,-4,-4,-2,
-4, 0, 8, 0,-4,
-4, 8,24, 8,-4,
-4, 0, 8, 0,-4,
-2,-4,-4,-4,-2};
// 复制图象数据到中间缓存
for (i = 0; i < lLineBytes * lHeight; i++)
m_temp[i] = *(lpDIBBits + i);
// 5X5 模版
for (i = 0; i < lWidth; i++) //被处理像素在i列
{
for (j = 0; j < lHeight; j++) //被处理像素在j行
{
v_r = v_g = v_b = p_g = 0;
for (k = i - 2; k < i + 3; k++) //5*5模版
{
for (l = j - 2; l < j + 3; l++)
{
// 防止内存溢出
if (k >= 0 && l >= 0 && k < lWidth && l < lHeight)
{
v_r += *(lpDIBBits + l * lLineBytes + k * 3) * g[p_g];
v_g += *(lpDIBBits + l * lLineBytes + k * 3 + 1) * g[p_g];
v_b += *(lpDIBBits + l * lLineBytes + k * 3 + 2) * g[p_g];
p_g++;
}
}
}
if (v_r < 0)
v_r = 0;
if (v_g < 0)
v_g = 0;
if (v_b < 0)
v_b = 0;
m_temp[j * lLineBytes + i * 3] = v_r;
m_temp[j * lLineBytes + i * 3 + 1] = v_g;
m_temp[j * lLineBytes + i * 3 + 2] =v_b;
}
}
// 回存处理结果到DIB
for(i = 0; i < lLineBytes * lHeight; i++)
*(lpDIBBits + i) = m_temp[i];
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 释放内存
delete[] m_temp;
// 恢复光标
EndWaitCursor();
}
/*************************************************************************
*
* 函数名称:
* 1 Hough()
*
* 参数:
* HDIB hDIB - 待处理的DIB
*
* 返回值:
* void - 无返回值
*
* 说明:
* 该函数将半径40个象素的圆周从原始图象中提取出来。
*
************************************************************************/
void CSplitProc::Hough(HDIB hDIB)
{
// 循环变量
LONG i;
LONG j;
LONG k;
// 指向DIB的指针
LPBYTE lpDIB;
// 指向DIB象素指针
LPBYTE lpDIBBits;
// 锁定DIB
lpDIB = (LPBYTE) ::GlobalLock((HGLOBAL) hDIB);
// 找到DIB图像象素起始位置
lpDIBBits = m_clsDIB.FindDIBBits(lpDIB);
// 判断是否是24-bpp位图
if (m_clsDIB.DIBBitCount(lpDIB) != 24)
{
// 提示用户
MessageBox("请先将其转换为24位色位图,再进行处理!", "系统提示" , MB_ICONINFORMATION | MB_OK);
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 返回
return;
}
// 更改光标形状
BeginWaitCursor();
// DIB的宽度
LONG lWidth = m_clsDIB.DIBWidth(lpDIB);
// DIB的高度
LONG lHeight = m_clsDIB.DIBHeight(lpDIB);
// 计算图像每行的字节数
LONG lLineBytes = WIDTHBYTES(lWidth * 24);
// 不能用char,也不能用::strcpy()
unsigned char* m_temp;
m_temp = new unsigned char [lLineBytes * lHeight];
// 中间变量
int cx, cy1, cy2;
memset(m_temp, 0, sizeof(m_temp));
// 以离散点为圆心画圆
for (i = 0; i < lWidth; i++) //被处理像素在i列
{
for (j = 0; j < lHeight; j++) //被处理像素在j行
{
if (*(lpDIBBits + j * lLineBytes + i * 3) == 0)
{
for (k = -40; k < 40; k++)
{
cx = k;
cy1 = (int)sqrt(40 * 40 - cx * cx);
cy2 = (int)sqrt(40 * 40 - cx * cx) * (-1);
cx += i;
cy1 += j;
cy2 += j;
if (cx >= 0 && cx < lWidth && cy1 >= 0 && cy1 < lHeight)
{
m_temp[cy1 * lLineBytes + cx * 3]++;
m_temp[cy1 * lLineBytes + cx * 3 + 1]++;
m_temp[cy1 * lLineBytes + cx * 3 + 2]++;
}
if (cx >= 0 && cx < lWidth && cy2 >= 0 && cy2 < lHeight)
{
m_temp[cy2 * lLineBytes + cx * 3]++;
m_temp[cy2 * lLineBytes + cx * 3 + 1]++;
m_temp[cy2 * lLineBytes + cx * 3 + 2]++;
}
}
}
}
}
// 显示中间统计结果
for (i = 0; i <lLineBytes * lHeight; i++)
*(lpDIBBits + i) = m_temp[i];
// 通过求最大值计算出圆心位置
int nMaxValue = 0;
int nMaxX, nMaxY;
for (i = 0; i < lWidth; i++) //被处理像素在i列
{
for (j = 0; j < lHeight; j++) //被处理像素在j行
{
if (nMaxValue < m_temp[j * lLineBytes + i * 3])
{
nMaxValue = m_temp[j * lLineBytes + i * 3];
nMaxX = i;
nMaxY = j;
}
}
}
// 显示检测结果
for (i = 0; i <lLineBytes * lHeight; i++)
m_temp[i] = 255;
for (k = -40; k < 40; k++)
{
cx = k;
cy1 = (int)sqrt(40 * 40 - cx * cx);
cy2 = (int)sqrt(40 * 40 - cx * cx) * (-1);
cx += nMaxX;
cy1 += nMaxY;
cy2 += nMaxY;
if (cx >= 0 && cx < lWidth && cy1 >= 0 && cy1 < lHeight)
{
m_temp[cy1 * lLineBytes + cx * 3] = 0;
m_temp[cy1 * lLineBytes + cx * 3 + 1] = 0;
m_temp[cy1 * lLineBytes + cx * 3 + 2] = 0;
}
if (cx >= 0 && cx < lWidth && cy2 >= 0 && cy2 < lHeight)
{
m_temp[cy2 * lLineBytes + cx * 3] = 0;
m_temp[cy2 * lLineBytes + cx * 3 + 1] = 0;
m_temp[cy2 * lLineBytes + cx * 3 + 2] = 0;
}
}
// 显示检测结果
for (i = 0; i <lLineBytes * lHeight; i++)
*(lpDIBBits + i) = m_temp[i];
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 释放内存
delete[] m_temp;
// 恢复光标
EndWaitCursor();
}
/*************************************************************************
*
* 函数名称:
* Kirsch()
*
* 参数:
* HDIB hDIB - 待处理的DIB
*
* 返回值:
* void - 无返回值
*
* 说明:
* 该函数使用Kirsch算子对图象进行边缘检测
*
************************************************************************/
void CSplitProc::Kirsch(HDIB hDIB)
{
// 循环变量
LONG i;
LONG j;
LONG k;
LONG l;
// 指向DIB的指针
LPBYTE lpDIB;
// 指向DIB象素指针
LPBYTE lpDIBBits;
LPBYTE lpNewDIBBits1;
LPBYTE lpNewDIBBits2;
LPBYTE lpDst1;
LPBYTE lpDst2;
HLOCAL hNewDIBBits1;
HLOCAL hNewDIBBits2;
// 锁定DIB
lpDIB = (LPBYTE) ::GlobalLock((HGLOBAL) hDIB);
// 找到DIB图像象素起始位置
lpDIBBits = m_clsDIB.FindDIBBits(lpDIB);
// 判断是否是24-bpp位图
if (m_clsDIB.DIBBitCount(lpDIB) != 24)
{
// 提示用户
MessageBox("请先将其转换为24位色位图,再进行处理!", "系统提示" , MB_ICONINFORMATION | MB_OK);
// 解除锁定
::GlobalUnlock((HGLOBAL) hDIB);
// 返回
return;
}
// 更改光标形状
BeginWaitCursor();
// DIB的宽度
LONG lWidth = m_clsDIB.DIBWidth(lpDIB);
// DIB的高度
LONG lHeight = m_clsDIB.DIBHeight(lpDIB);
// 计算图像每行的字节数
LONG lLineBytes = WIDTHBYTES(lWidth * 24);
// 暂时分配内存,以保存新图象
hNewDIBBits1 = LocalAlloc(LHND, lLineBytes * lHeight);
if (hNewDIBBits1 == NULL)
{
// 分配内存失败
return;
}
// 锁定内存
lpNewDIBBits1 = (LPBYTE)LocalLock(hNewDIBBits1);
// 暂时分配内存,以保存新图象
hNewDIBBits2 = LocalAlloc(LHND, lLineBytes * lHeight);
if (hNewDIBBits1 == NULL)
{
// 分配内存失败
return;
}
// 锁定内存
lpNewDIBBits2 = (LPBYTE)LocalLock(hNewDIBBits2);
// 复制图象到缓存
lpDst1 = lpNewDIBBits1;
lpDst2 = lpNewDIBBits2;
memcpy(lpNewDIBBits1, lpDIBBits, lLineBytes * lHeight);
memcpy(lpNewDIBBits2, lpDIBBits, lLineBytes * lHeight);
// 不能用char,也不能用::strcpy()
unsigned char* m_temp;
m_temp = new unsigned char [lLineBytes * lHeight];
// Kirsch算子的八个方向上的掩模模版
int g[8][9] = { { 5, 5, 5, -3, 0, -3, -3, -3, -3},
{ -3, 5, 5, -3, 0, 5, -3, -3, -3},
{ -3, -3, 5, -3, 0, 5, -3, -3, 5},
{ -3, -3, -3, -3, 0, 5, -3, 5, 5},
{ -3, -3, -3, -3, 0, -3, 5, 5, 5},
{ -3, -3, -3, 5, 0, -3, 5, 5, -3},
{ 5, -3, -3, 5, 0, -3, 5, -3, -3},
{ 5, 5, -3, 5, 0, -3, -3, -3, -3}};
// 中间变量
int v_r, v_g, v_b, p_g;
// 复制图象数据到中间缓存
memcpy(m_temp, lpNewDIBBits1, lLineBytes * lHeight);
// 3X3 模版
for (i = 0; i < lWidth; i++) //被处理像素在i列
{
for (j = 0; j < lHeight; j++) //被处理像素在j行
{
v_r = v_g = v_b = p_g = 0;
for (k = i - 1; k < i + 2; k++) //3*3模版
{
for (l = j - 1; l < j + 2; l++)
{
// 防止内存溢出
if (k >= 0 && l >= 0 && k < lWidth && l < lHeight)
{
v_r += *(lpNewDIBBits1 + l * lLineBytes + k * 3) * g[0][p_g];
v_g += *(lpNewDIBBits1 + l * lLineBytes + k * 3 + 1) * g[0][p_g];
v_b += *(lpNewDIBBits1 + l * lLineBytes + k * 3 + 2) * g[0][p_g];
p_g++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -