📄 specialdetectionfilter.cpp
字号:
/////////////////////////////////////////////////////////////////////////////////
//
// SpecialDetectionFilter.cpp: implementation of the CSpecialDetectionFilter class.
//
////////////////////////////////////////////////////////////////////////////////
// 版权所有(2002)
// Copyright(2002)
// 编写者: 向世明
// Author: Xiang Shiming
#include "stdafx.h"
#include "SpecialDetectionFilter.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CSpecialDetectionFilter, CImageAreaProcess)
CSpecialDetectionFilter::CSpecialDetectionFilter()
{
m_dwOperation = IMAGE_SOBEL_EDGE_DETECT;
m_nCols = 3;
m_nRows = 3;
}
CSpecialDetectionFilter::~CSpecialDetectionFilter()
{}
#ifdef _DEBUG
void CSpecialDetectionFilter::Dump(CDumpContext& dc) const
{
CImageAreaProcess::Dump(dc);
}
void CSpecialDetectionFilter::AssertValid() const
{
CImageAreaProcess::AssertValid();
}
#endif
BOOL CSpecialDetectionFilter::Filter(LPBYTE lpbyBits32, int x, int y, int nWidth, int nHeight, int nScanWidth, int nScanHeight)
{
//第一步, 参数合法性检测
ASSERT(lpbyBits32);
//添加一个检测条件:
if((m_dwOperation == IMAGE_SOBEL_EDGE_DETECT) ||
(m_dwOperation == IMAGE_KIRSCH_EDGE_DETECT))
{
m_nRows = 3;
m_nCols = 3;
}
if((x > (nScanWidth - 1)) || (y > (nScanHeight - 1))) return FALSE;
//有效区域的宽度和高度
int w = min(nWidth, nScanWidth - x);
int h = min(nHeight, nScanHeight - y);
//行字节数
DWORD dwWidthBytes = (DWORD)nScanWidth * 4;
//建立一份拷贝
BYTE* pbySrcCopy = new BYTE[ (dwWidthBytes * nScanHeight) ];
if(pbySrcCopy == NULL) return FALSE;
::CopyMemory(pbySrcCopy, lpbyBits32, dwWidthBytes * nScanHeight);
int i, j;
//处理上下边界
//上下边界的高度:
int nBorderH = (m_nRows - 1) / 2;
for(i = 0;i < nBorderH;i++)
{
//当前像素y坐标
int yy = y + i;
//指向目的地址
BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;
for(j = 0;j < w;j++)
{
//当前像素x坐标
int xx = x + j;
//进行卷积操作
PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
//写向目的数据缓冲区
*pbyDst++ = rgb.blue;
*pbyDst++ = rgb.green;
*pbyDst++ = rgb.red;
pbyDst++;
}
}
//下边界Y坐标
int nYBottom = (y + h - 1);
//处理下边界
for(i = 0;i < nBorderH;i++)
{
//当前像素y坐标
int yy = nYBottom - i;
//指向目的地址
BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;
for(j = 0;j < w;j++)
{
//当前像素x坐标
int xx = x + j;
//进行卷积操作
PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
//写向目的数据缓冲区
*pbyDst++ = rgb.blue;
*pbyDst++ = rgb.green;
*pbyDst++ = rgb.red;
pbyDst++;
}
}
//垂直边界宽度
int nBorderW = (m_nCols - 1) / 2;
//剩余高度 + 1 + nBorderH:
int nRemnantH = (h - nBorderH);
//处理左边界
for(i = nBorderH; i < nRemnantH;i++)
{
//当前像素y坐标
int yy = y + i;
//指向目的地址
BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;
for(j = 0;j < nBorderW;j++)
{
//当前像素x坐标
int xx = x + j;
//进行卷积操作
PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
//写向目的数据缓冲区
*pbyDst++ = rgb.blue;
*pbyDst++ = rgb.green;
*pbyDst++ = rgb.red;
pbyDst++;
}
}
//右边界x坐标起点
int nXRight = x + w - nBorderW;
//处理右边界
for(i = nBorderH; i < nRemnantH;i++)
{
//当前像素y坐标
int yy = y + i;
//指向目的地址
BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * nXRight;
for(j = 0;j < nBorderW;j++)
{
//当前像素x坐标
int xx = nXRight + j;
//进行卷积操作
PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
//写向目的数据缓冲区
*pbyDst++ = rgb.blue;
*pbyDst++ = rgb.green;
*pbyDst++ = rgb.red;
pbyDst++;
}
}
//内部宽度 - nBorderW + 1
int nInnerW = w - nBorderW;
//处理内部
for(i = nBorderH; i < nRemnantH;i++)
{
//当前像素y坐标
int yy = y + i;
//指向目的地址
BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;
for(j = 0;j < nInnerW;j++)
{
//当前像素x坐标
int xx = x + j;
//进行卷积操作
PIXELCOLORRGB rgb = FilterPixelInner(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
//写向目的数据缓冲区
*pbyDst++ = rgb.blue;
*pbyDst++ = rgb.green;
*pbyDst++ = rgb.red;
pbyDst++;
}
}
delete[] pbySrcCopy ;
return TRUE;
}
PIXELCOLORRGB CSpecialDetectionFilter::FilterPixelInner(LPBYTE lpbyBitsSrc32, int x, int y, int nScanWidth, int nScanHeight)
{
//卷积核元素的总个数;
int nNum = m_nRows * m_nCols;
//行字节数
DWORD dwWidthBytes = (DWORD)nScanWidth * 4;
//存储一个像素邻域的RGB三分量
BYTE* pbyRed = new BYTE[nNum];
BYTE* pbyGreen = new BYTE[nNum];
BYTE* pbyBlue = new BYTE[nNum];
//邻域中的左上角点.
int xx = x - ((m_nCols - 1) / 2);
int yy = y - ((m_nRows - 1) / 2);
//三个数组pnRed, pnGreen, pnBlue的索引值
int index = 0;
//用嵌套循环获取小邻域数据.
for(int i = 0;i < m_nRows;i++)
{
//y坐标
int nY = yy + i;
//指针, 指向行数据
BYTE* pbySrc = lpbyBitsSrc32 + ((DWORD)nY) * dwWidthBytes + 4 * xx;
for(int j = 0;j < m_nCols;j++)
{
//x坐标
//记录颜色分量
pbyBlue[index] = *pbySrc++;
pbyGreen[index] = *pbySrc++;
pbyRed[index] = *pbySrc++;
pbySrc++;
index++;
}
}
//RGB颜色结构, 在 IMG.H 中定义
//特殊计算
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -