📄 grayprocess.cpp
字号:
/////////////////////////////////////////////////////////////////////////////////
//
// GrayProcess.cpp: implementation of the CGrayProcess class.
//
////////////////////////////////////////////////////////////////////////////////
// Copyright(2005)
// Author: rtttty
#include "stdafx.h"
#include "GrayProcess.h"
//#include "Paramar.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CGrayProcess, CImagePointProcess)
CGrayProcess::CGrayProcess()
{
//缺省操作
m_dwOperation = IMAGE_GRAY_COLOR_GRAYED_OUT;
//灰度比例化百分比
m_nPercentage = 100;
//灰度线性变换的四个区间参数
m_byA = 0;
m_byB = 255;
m_byC = 0;
m_byD = 255;
//给定与之匹配的灰度频数
m_pnMatchFreq = NULL;
//原图像的灰度数学期望(expectation)
m_nExpSrc = 128;
//目标图像的灰度数学期望
m_nExpDst = 128;
}
CGrayProcess::~CGrayProcess()
{
}
#ifdef _DEBUG
void CGrayProcess::Dump(CDumpContext& dc) const
{
CImagePointProcess::Dump(dc);
}
void CGrayProcess::AssertValid() const
{
CImagePointProcess::AssertValid();
}
#endif
//设置一个int
void CGrayProcess::SetParam1i(int nPercentage)
{
m_nPercentage = nPercentage;
}
//在这里添加了线性变换的区间的代码
//void CGrayProcess::SetParamOfConvert()
//{
//}
//设置4个BYTE
void CGrayProcess::SetParam4by(BYTE bya, BYTE byb, BYTE byc, BYTE byd,int e)
{
//在这里添加了线性变换的区间的代码
//CParamar dpara = new CParamar;
//dpara.DoModal();
m_byA = bya;
m_byB = byb;
m_byC = byc;
m_byD = byd;
m_inte = e;
if(m_byA > m_byB)
{
BYTE byTemp = m_byA;
m_byA = m_byB;
m_byB = byTemp;
}
if(m_byC > m_byD)
{
BYTE byTemp = m_byC;
m_byC = m_byD;
m_byD = byTemp;
}
}
//设置目标直方图,
//保证 pnMatch 的长度不小于 256
void CGrayProcess::SetParam1iv(const int* pnMatch)
{
if(m_pnMatchFreq) delete[] m_pnMatchFreq;
else
m_pnMatchFreq = new int[256];
for(int i = 0; i < 256; i++)
m_pnMatchFreq[i] = pnMatch[i];
}
//nExpDst目标期望
//nRatioVarPer方差比值(百分值)
//nRatioVarPer = (int)((varSrc / varDst) * 100);
void CGrayProcess::SetParam2i(int nExpDst, int nRatioVarPer)
{
m_nExpDst = nExpDst;
m_nPercentage = nRatioVarPer;
}
BOOL CGrayProcess::MakeGray(LPBYTE lpbyBits32, int x, int y, int nWidth, int nHeight, int nScanWidth, int nScanHeight)
{
ASSERT(lpbyBits32);
BOOL bSucessfully = TRUE;
switch(m_dwOperation)
{
case IMAGE_GRAY_COLOR_GRAYED_OUT:
case IMAGE_GRAY_PERCENTAGE:
case IMAGE_GRAY_LINEARIZE:
{
bSucessfully = MakeLinearGray(lpbyBits32, x, y, nWidth, nHeight, nScanWidth, nScanHeight);
break;
}
case IMAGE_GRAY_LINEAR_ROUND_OFF:
{
bSucessfully=MakeJuanJi(lpbyBits32, x, y, nWidth, nHeight, nScanWidth, nScanHeight);
break;
}
case IMAGE_GRAY_REVERSE:
{
bSucessfully = MakeLinearGray(lpbyBits32, x, y, nWidth, nHeight, nScanWidth, nScanHeight);
break;
}
case IMAGE_GRAY_HISTOGRAM_BALANCE:
{
bSucessfully = MakeHistogramBalance(lpbyBits32, x, y, nWidth, nHeight, nScanWidth, nScanHeight);
break;
}
case IMAGE_GRAY_HISTOGRAM_MATCH:
{
bSucessfully = MakeHistogramMatch(lpbyBits32, x, y, nWidth, nHeight, nScanWidth, nScanHeight);
break;
}
case IMAGE_GRAY_STATISTIC_MATCH:
{
bSucessfully = MakeStatisticMatch(lpbyBits32, x, y, nWidth, nHeight, nScanWidth, nScanHeight);
break;
}
default : break;
}
return bSucessfully;
}
void CGrayProcess::GetJuanji(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int a10)
{
conv[0][0] =a1;
conv[0][1] =a2;
conv[0][2] =a3;
conv[1][0] =a4;
conv[1][1] =a5;
conv[1][2] =a6;
conv[2][0] =a7;
conv[2][1] =a8;
conv[2][2] =a9;
AllDivide =a10;
}
BOOL CGrayProcess::MakeJuanJi(LPBYTE lpbyBits32, int x, int y, int nWidth,
int nHeight, int nScanWidth, int nScanHeight)
{
ASSERT(lpbyBits32);
//第一步, 进行参数合法性检测
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;
//开始数据基索引
DWORD dwBaseIndex = y * dwWidthBytes + 4 * x;
//区间长度
BYTE b_a = m_byB - m_byA; // b - a
BYTE d_c = m_byD - m_byC; // d - c
if(b_a == 0)b_a = 1;
if(d_c == 0)d_c = 1;
//得到矩阵和灰度化
int NLen;
if(w>h)
NLen=w;
else
NLen=h;
int **mygray = new int*[NLen];
int **afterJuanji = new int*[NLen];
for(int tempnew1=0;tempnew1<NLen;tempnew1++)
mygray[tempnew1] = new int [NLen];
for(int tempnew2=0;tempnew2<NLen;tempnew2++)
afterJuanji[tempnew2] = new int [NLen];
for(int temp1=0;temp1<NLen;temp1++)
{
for(int temp2=0;temp2<NLen;temp2++)
{
mygray[temp1][temp2]=0;
afterJuanji[temp1][temp2]=0;
}
}
for(int i = 0;i < h;i++)
{
BYTE* pbyRsc = lpbyBits32 + dwBaseIndex;
for(int j = 0;j < w;j++)
{
//第一小步, 获取指针
BYTE* pbyBlue = pbyRsc++;
BYTE* pbyGreen = pbyRsc++;
BYTE* pbyRed = pbyRsc++;
pbyRsc++;
//第二小步, 获取值;
BYTE r = *pbyRed;
BYTE g = *pbyGreen;
BYTE b = *pbyBlue;
BYTE gray = (BYTE)(((WORD)r * 30 + (WORD)g * 59 + (WORD)b * 11) / 100);
mygray[j][i]=gray;
}
dwBaseIndex += dwWidthBytes;
}
//进行卷积
int sum=0;
for(int p=0;p<NLen;p++)
{
for(int q=0;q<NLen;q++)
{
if((p==0)&&(q==0))
sum= conv[0][0]*mygray[p][q];
else if((p==0)&&(q==1))
sum= conv[0][0]*mygray[p][q] + conv[0][1]*mygray[p][q-1];
else if((p==0)&&(q>=2))
sum= conv[0][0]*mygray[p][q] + conv[0][1]*mygray[p][q-1] + conv[0][2]*mygray[p][q-2];
else if((p==1)&&(q==0))
sum= conv[0][0]*mygray[p][q] + conv[1][0]*mygray[p-1][q];
else if((p==1)&&(q==1))
sum= conv[0][0]*mygray[p][q] + conv[0][1]*mygray[p][q-1] +
conv[1][0]*mygray[p-1][q] + conv[1][1]*mygray[p-1][q-1];
else if((p==1)&&(q>=2))
sum= conv[0][0]*mygray[p][q] + conv[0][1]*mygray[p][q-1] + conv[0][2]*mygray[p][q-2] +
conv[1][0]*mygray[p-1][q] + conv[1][1]*mygray[p-1][q-1] + conv[1][2]*mygray[p-1][q-2];
else if((p>=2)&&(q==0))
sum= conv[0][0]*mygray[p][q] + conv[1][0]*mygray[p-1][q] + conv[2][0]*mygray[p-2][q];
else if((p>=2)&&(q==1))
sum= conv[0][0]*mygray[p][q] + conv[0][1]*mygray[p][q-1] +
conv[1][0]*mygray[p-1][q] + conv[1][1]*mygray[p-1][q-1] +
conv[2][0]*mygray[p-2][q] + conv[2][1]*mygray[p-2][q-1] ;
else
sum= conv[0][0]*mygray[p][q] + conv[0][1]*mygray[p][q-1] + conv[0][2]*mygray[p][q-2] +
conv[1][0]*mygray[p-1][q] + conv[1][1]*mygray[p-1][q-1] + conv[1][2]*mygray[p-1][q-2] +
conv[2][0]*mygray[p-2][q] + conv[2][1]*mygray[p-2][q-1] + conv[2][2]*mygray[p-2][q-2] ;
afterJuanji[p][q] =sum/AllDivide;
if(afterJuanji[p][q]>255)
afterJuanji[p][q] = 255;
if(afterJuanji[p][q]<0)
afterJuanji[p][q] = 1;
}
}
//完成了卷积了.下面是灰度修改
dwBaseIndex = y * dwWidthBytes + 4 * x;
for(int ii = 0;ii < h;ii++)
{
BYTE* pbyRsc = lpbyBits32 + dwBaseIndex;
for(int jj = 0;jj < w;jj++)
{
*pbyRsc++ = afterJuanji[jj][ii];
*pbyRsc++ = afterJuanji[jj][ii];
*pbyRsc++ = afterJuanji[jj][ii];
*pbyRsc++ ;
}
dwBaseIndex += dwWidthBytes;
}
return true;
}
BOOL CGrayProcess::MakeLinearGray(LPBYTE lpbyBits32, int x, int y, int nWidth, int nHeight, int nScanWidth, int nScanHeight)
{
ASSERT(lpbyBits32);
//第一步, 进行参数合法性检测
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;
//开始数据基索引
DWORD dwBaseIndex = y * dwWidthBytes + 4 * x;
//区间长度
BYTE b_a = m_byB - m_byA; // b - a
BYTE d_c = m_byD - m_byC; // d - c
if(b_a == 0)b_a = 1;
if(d_c == 0)d_c = 1;
//第二步, 灰度修改
for(int i = 0;i < h;i++)
{
BYTE* pbyRsc = lpbyBits32 + dwBaseIndex;
for(int j = 0;j < w;j++)
{
//第一小步, 获取指针
BYTE* pbyBlue = pbyRsc++;
BYTE* pbyGreen = pbyRsc++;
BYTE* pbyRed = pbyRsc++;
pbyRsc++;
//第二小步, 获取值;
BYTE r = *pbyRed;
BYTE g = *pbyGreen;
BYTE b = *pbyBlue;
BYTE gray = (BYTE)(((WORD)r * 30 + (WORD)g * 59 + (WORD)b * 11) / 100);
//第三小步, 处理单个像素值
switch(m_dwOperation)
{
//将彩色转换为灰度图像
case IMAGE_GRAY_COLOR_GRAYED_OUT:
{
*pbyBlue = gray;
*pbyGreen = gray;
*pbyRed = gray;
break;
}
//灰度线性化:
case IMAGE_GRAY_LINEARIZE:
{
BYTE byResult = gray;
if((gray >= m_byA) && (gray <= m_byB))
byResult = (BYTE)((((WORD)d_c * (WORD)(gray - m_byA)) / (WORD)b_a) + m_byC);
else if(gray < m_byA)
byResult = (BYTE)((WORD)m_byC*(WORD)gray/(WORD)m_byA);
else
byResult = (BYTE)(((WORD)gray-(WORD)m_byB)*(255-(WORD)(m_byD))
/(255-(WORD)(m_byB))+(WORD)m_byD);
*pbyBlue = byResult;
*pbyGreen = byResult;
*pbyRed = byResult;
break;
}
default:break;
}
}
dwBaseIndex += dwWidthBytes;
}
return TRUE;
}
BOOL CGrayProcess::MakeHistogramBalance(LPBYTE lpbyBits32, int x, int y, int nWidth, int nHeight, int nScanWidth, int nScanHeight)
{
ASSERT(lpbyBits32);
//第一步, 进行参数合法性检测
if((x > (nScanWidth - 1)) || (y > (nScanHeight - 1))) return FALSE;
//有效区域的宽度和高度
int w = min(nWidth, nScanWidth - x);
int h = min(nHeight, nScanHeight - y);
if(w * h == 0)return FALSE;
//第二步, 灰度统计
//行字节数
DWORD dwWidthBytes = (DWORD)nScanWidth * 4;
//开始数据基索引
DWORD dwBaseIndex = y * dwWidthBytes + 4 * x;
//子区域像素个数
DWORD dwPixelSize = w * h;
int i = 0;
int j = 0;
//开辟一个内存区, 记录指定区域的灰度值
//存放被处理的子区域的灰度
BYTE* pbyGraySubArea = new BYTE[dwPixelSize];
if(pbyGraySubArea == NULL) return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -